使用writeSelectedRows的iText PdfPTable

时间:2017-03-17 01:31:16

标签: c# itext

我创建了一个PdfPTable并使用WriteSelectedRows编写表,每当HTML内容很大并占用两个页面时,该表就会写入第二页而不是第一页。如果内容本身是一页,则表格在第一页中正确写入。

无论页数如何,请帮我写第1页的表格。

PdfPCell c = new PdfPCell(ImageHeader, true);
c.HorizontalAlignment = Element.ALIGN_LEFT;
c.FixedHeight = cellHeight;
c.Border = PdfPCell.NO_BORDER;
head.AddCell(c);
c = new PdfPCell(new Phrase("somePhrase", fontintestazione));
c.Border = PdfPCell.NO_BORDER;
head.AddCell(c);
c = new PdfPCell(new Phrase("someTextBlah", fontRight));
c.Border = PdfPCell.NO_BORDER;
c.HorizontalAlignment = 1;
c.BackgroundColor = new BaseColor(70, 130, 180);
head.AddCell(c);
head.WriteSelectedRows(0, -1, 10, page.Height - cellHeight + head.TotalHeight -30, writer.DirectContent);

上面的代码是我想要添加的示例表。

1 个答案:

答案 0 :(得分:1)

您可以附加一个eventhelper类,您可以将pdftable写入生成的html的第一页。以下是您可以使用的示例

public class PdfEventHelper : PdfPageEventHelper
{
    public PdfEventHelper()
    {

    }
    public override void OnOpenDocument(PdfWriter writer, Document document)
    {
        base.OnOpenDocument(writer, document);
    }
    public override void OnStartPage(PdfWriter writer, Document document)
    {
        base.OnStartPage(writer, document);
    }
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        if (writer.PageNumber == 1)
        {
            //replace this table with the table that you want to write on the first page
            PdfPTable pdfTable = new PdfPTable(new[] { 1f, 1f, 1f, 1f });
            pdfTable.WriteSelectedRows(0, -1, 10, pdfTable.TotalHeight + 10, writer.DirectContent);
            base.OnEndPage(writer, document);
        }

    }
    public override void OnCloseDocument(PdfWriter writer, Document document)
    {
        base.OnCloseDocument(writer, document);
    }
}

您可以在代码建议中添加您的PdfPTable作为评论。会对你好吗