Jung
Jung

Reputation: 209

C# itext - How do I know if it went to the next page while writing it to new PDF?

I am trying to create PDF document using iText7 library. I am going to have a header and then keep adding new lines of text. But while adding new line of text, when it reaches the end of the page and the new line has to go to the next page, I want to add another header (different header). How do I know that the new line will go into the next page while I keep adding this line of text?

For example, my code would look like this:

static void Main(string[] args)
      {
         // Must have write permissions to the path folder
         PdfWriter writer = new PdfWriter("C:\\test\\demo.pdf");
         PdfDocument pdf = new PdfDocument(writer);
         Document document = new Document(pdf);

         // Header
         Paragraph header = new Paragraph("HEADER")
            .SetTextAlignment(TextAlignment.CENTER)
            .SetFontSize(20);

         // New line
         Paragraph newline = new Paragraph(new Text("\n"));

         document.Add(newline);
         document.Add(header);

         // Add sub-header
         Paragraph subheader = new Paragraph("SUB HEADER")
            .SetTextAlignment(TextAlignment.CENTER)
            .SetFontSize(15);
         document.Add(subheader);

         // Line separator
         LineSeparator ls = new LineSeparator(new SolidLine());
         document.Add(ls);

         // Add paragraphs - HOW DO I KNOW THE NEW Pharagraph WILL BE ON THE NEXT PAGE?
         for (int i = 0; i < 200; i++)
         {
            Paragraph paragraph1 = new Paragraph("Lorem ipsum " +
            "dolor sit amet, consectetur adipiscing elit, " +
            "sed do eiusmod tempor incididunt ut labore " +
            "et dolore magna aliqua.");

            document.Add(paragraph1);

         }
}

Thank you so much for the help in advance.

Upvotes: 0

Views: 533

Answers (1)

Avo Nappo
Avo Nappo

Reputation: 630

You can do this two ways:

  1. Add all your paragraphs and at that point you know how many pages you have and can add additional stuff, including headers, to those pages.
  2. Add an END_PAGE event handler. Something like this:
private void TestPdfHeader()
{
    string fileName = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\test.pdf";
    PdfWriter pdfWriter = new PdfWriter(fileName);
    PdfDocument pdf = new PdfDocument(pdfWriter);
    Document doc = new Document(pdf);            
    var font = PdfFontFactory.CreateFont(StandardFonts.COURIER);

    pdf.AddEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler(doc));


    for (int i = 0; i < 100; i++)
    {
        Paragraph p = new Paragraph("test test test");
        p.SetFont(font);
        doc.Add(p);
    }

    doc.Close();
}

private class TextFooterEventHandler : IEventHandler
{
    Document doc;
    PdfFont headerFont = null;

    public TextFooterEventHandler(Document doc)
    { 
        this.doc = doc;
        this.headerFont = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_OBLIQUE);
    }

    public void HandleEvent(Event currentEvent)
    {
        PdfDocumentEvent docEvent = (PdfDocumentEvent)currentEvent;
        int numPages = docEvent.GetDocument().GetNumberOfPages();

        // This event is fired after the next page has been added. 
        // In this case we want to skip header on page 1.
        // Thus, we start adding headers when the 3rd page has been added.
        if (numPages < 3) return;

        string pageHeader = "This is page header";
        iText.Kernel.Geom.Rectangle pageSize = docEvent.GetPage().GetPageSize();             
        float coordX = pageSize.GetLeft() + doc.GetLeftMargin() + 50;
        float headerY = pageSize.GetTop() - doc.GetTopMargin() + 10;
        Canvas c = new Canvas(docEvent.GetPage(), pageSize);
        c.SetFont(headerFont);
        c.ShowTextAligned(pageHeader, coordX, headerY, TextAlignment.CENTER);
        c.Close();
    }
}

Upvotes: 1

Related Questions