【问题标题】:Problems creating a header in itextsharp with multiply calls to the table在 itextsharp 中通过对表的多次调用创建标题时出现问题
【发布时间】:2012-01-14 02:09:35
【问题描述】:

我有三个静态 PdfPTable 方法,它们从用于创建 PDF 文档的 main 方法调用。

在每种方法中,我使用 PdfPCells' 将数据和结构添加到表中,因此第一个 PdfPTable 方法是为每个页面创建标题,第二个是创建该页面的正文,第三个是创建每页的页脚。然后在 main 方法中调用表格后将表格添加到 PdfPDocument

我尝试使用 table.HeaderRows = 1 为每个页面添加标题,但是当添加到标题的 PdfPTable 方法时,它会删除该表中的所有内容。当我将它添加到正文的 PdfPTable 时,它​​会将第二页上的内容移动到第一页的底部,并将第一页的内容复制到第二页。

 //table method for call header
 PdfPTable table = CreateTable(textUpperData/*, document, writer*/);
 document.Add(table);

 //table method call for body
 table = CreateTable1(imgInfoData, posData, sizeData, document);
 document.Add(table);

 //table method call for footer
 table = CreateTable2(textLowerData);
 document.Add(table);

 document.Close();



//header table static method                
    private static PdfPTable CreateTable(List<KeyValuePair<string, string>> textUpper/*, Document document, PdfWriter writer*/)
    {
        //SiteDB sitedb = new SiteDB();
        //sitedb.GetEmailText();

        iTextSharp.text.Font headerFont = FontFactory.GetFont("Time New Roman", 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
        iTextSharp.text.Font bodyFont = FontFactory.GetFont("Time New Roman", 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

        PdfPTable table = new PdfPTable(3);
        table.HorizontalAlignment = Element.ALIGN_CENTER;
        table.WidthPercentage = 100;
        table.TotalWidth = 597.6f;
        table.LockedWidth = true;
        table.SetWidths(new float[] { 1, 2, 1 });

        iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("C:\\Users\\User\\Documents\\Images\\image0.tiff");
        logo.ScaleToFit(150, 235);
        logo.SetAbsolutePosition(595.2f - 150f, 0);


        PdfPCell logoCell = new PdfPCell(logo);
        logoCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
        logoCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
        table.AddCell(logoCell);

        Chunk logoChunk = new Chunk(System.Environment.NewLine);
        Phrase logoPhrase = new Phrase(logoChunk);

        //Chunk headerChunk = new Chunk(sitedb.header_lit.ToString() + System.Environment.NewLine, headerFont);
        //Phrase headerPhrase = new Phrase(headerChunk);
        Chunk headerChunk = new Chunk(textUpper[0].Key.ToString() + System.Environment.NewLine, headerFont);
        Phrase headerPhrase = new Phrase(headerChunk);

        //Chunk bodyChunk = new Chunk(sitedb.body_lit.ToString() + System.Environment.NewLine, bodyFont);
        //Phrase bodyPhrase = new Phrase(bodyChunk);
        Chunk bodyChunk = new Chunk(textUpper[0].Value.ToString() + System.Environment.NewLine, bodyFont);
        Phrase bodyPhrase = new Phrase(bodyChunk);


        Paragraph paragraph = new Paragraph();
        paragraph.Alignment = Element.ALIGN_MIDDLE;
        paragraph.Alignment = Element.ALIGN_TOP;
        paragraph.Add(headerPhrase);
        paragraph.Add(bodyPhrase);


        PdfPCell headerBodyCell = new PdfPCell(paragraph);
        headerBodyCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
        headerBodyCell.VerticalAlignment = Element.ALIGN_TOP;
        headerBodyCell.Colspan = 2;
        headerBodyCell.PaddingLeft = 5f;
        headerBodyCell.PaddingRight = 5f;
        headerBodyCell.PaddingTop = 8f;
        headerBodyCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
        table.AddCell(headerBodyCell);

        return table;
    }

【问题讨论】:

    标签: c# pdf itextsharp


    【解决方案1】:

    好吧,似乎无法将标题添加到由多个表格构建的 pdf 中的每个新页面,只有带有单元格的单个表格!

    我很可能会创建一个为每个新页面不断添加特定表格的方法!

    【讨论】:

    • 你想显示什么类型的标题:[1] page;文档中的每一页通用,或 [2] table;对表格来说是唯一的,当表格的内容超过一页时显示。
    • 我试图显示页眉,我发现上面的帖子有详细信息
    • 你应该使用PdfPageEventHelper。这里有一些sample code
    • 嘿 kuujinbo,我对你提到的代码有问题。标题没有出现,任何想法。要我发布新代码吗?
    • 刚刚发布您的示例不包含第二个表格来保存段落 p,所以我又回到了开头。感谢九神宝的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2021-10-06
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    • 2021-11-05
    相关资源
    最近更新 更多