0
votes

I have created a PdfPTable and writing the table using WriteSelectedRows, whenever the HTML content is large and taking two pages, the table getting written in the 2nd page rather than 1st. If the content itself is one page then the table is getting written correctly in 1st page.

Please help me in writing the table in page 1 irrespective of the number of page.

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);

The above code is the sample table I'm trying to add.

1

1 Answers

1
votes

You can attach an eventhelper class where you could write a pdftable to only first page of your generated html. Here is an example you may work with

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);
    }
}

You may add your PdfPTable as the comment in the code suggests. Will do fine for you