Home > Net >  iText 7 .net page 2 missing? 7.2.2
iText 7 .net page 2 missing? 7.2.2

Time:05-03

With the code i have currently, the PDF is getting 201 pages, the last is blank and there is no page 2

Page 1: Dear GUY JR LANDA, - Page Number:1

Page 2: Dear JOYCE JOHNSTON, - Page Number:3

Settings settings = SettingsFactory.GetSettings();
List<ILetter> letters = LetterFactory.GetLetters();
for (int i = 0; i < letters.Count/200; i  )
{
    var group = letters.Skip(i * 200).Take(200);
    using PdfWriter pdfWriter = new(Path.Combine(settings.OutputFolder, $"checks_{i   1}.pdf"));
    using PdfDocument pdfDocument = new(pdfWriter);
    using Document document = new(pdfDocument);
    pdfDocument.SetDefaultPageSize(iText.Kernel.Geom.PageSize.LEGAL);

    Resource Resources = new();

    foreach (var l in group)
    {
        l.SetPDF(pdfDocument, document, Resources);
        l.PrintPages();
    }
}

public override void PrintPages()
{
    CurrentPage = PdfDocument?.AddNewPage(iText.Kernel.Geom.PageSize.LEGAL);
    Greeting();
    Document?.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
}
public void Greeting()
{
    Paragraph P = new();
    P.Add(new Text($"Dear {Data?.Address?.Name},").SetFont(Resources?.Times_Roman));
    P.Add(new Text(" - Page Number:" PdfDocument?.GetNumberOfPages().ToString()));

    Document?.Add(P);
}

CodePudding user response:

Your code is not self-contained. Thus, I reduced it to the essentials:

using (PdfWriter pdfWriter = new PdfWriter(@"PageNumberFromNumberOfPages.pdf"))
using (PdfDocument pdfDocument = new PdfDocument(pdfWriter))
using (Document document = new Document(pdfDocument))
{
    for (int i = 1; i < 5; i  )
    {
        pdfDocument.AddNewPage(PageSize.LEGAL);

        Paragraph paragraph = new Paragraph();
        paragraph.Add(new Text($"This should be page {i}"));
        paragraph.Add(new Text($" and PdfDocument.GetNumberOfPages is {pdfDocument.GetNumberOfPages()}"));
        document.Add(paragraph);

        document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
    }
}

The result is similar to yours:

  1. This should be page 1 and PdfDocument.GetNumberOfPages is 1
  2. This should be page 2 and PdfDocument.GetNumberOfPages is 3
  3. This should be page 3 and PdfDocument.GetNumberOfPages is 4
  4. This should be page 4 and PdfDocument.GetNumberOfPages is 5
  5. [empty]

And this actually makes sense:

  • Initially the PdfDocument has 0 pages. The Document does not yet have a current page.
  • For i == 1
    • AddNewPage adds a page -> PdfDocument 1 page.
    • Thus, the paragraph contains "PdfDocument.GetNumberOfPages is 1".
    • Add(paragraph) uses the existing first page to write to. -> Document current page 1.
    • Add(new AreaBreak(AreaBreakType.NEXT_PAGE)) makes the Document target the next page. As there is none yet, it first generates a new page. -> PdfDocument 2 pages; Document current page 2.
  • For i == 2
    • AddNewPage adds a page -> PdfDocument 3 pages.
    • Thus, the paragraph contains "PdfDocument.GetNumberOfPages is 3".
    • Add(new AreaBreak(AreaBreakType.NEXT_PAGE)) makes the Document target the next page which is the already existing third page. -> PdfDocument 3 pages; Document current page 3.
  • For i == 3
    • AddNewPage adds a page -> PdfDocument 4 pages.
    • Thus, the paragraph contains "PdfDocument.GetNumberOfPages is 4".
    • Add(new AreaBreak(AreaBreakType.NEXT_PAGE)) makes the Document target the next page which is the already existing fourth page. -> PdfDocument 4 pages; Document current page 4.
  • ...

Thus, what you observe is exactly what is to be expected.


Probably you do not merely want to know why the observed behavior is correct, probably you also want to know how to retrieve the actual current page number.

First of all, using PdfDocument.GetNumberOfPages is not a bad idea per se, as long as pages only are created by adding to the Document class. As soon as you add a page via a different mechanism, though, the current Document page number and the number of pages in the PdfDocument diverge.

So if you need to change the number of pages via a different mechanism, you need an alternative way to query the current Document page number. You can do so by querying the associated DocumentRenderer, e.g. like this

paragraph.Add(new Text($" and CurrentArea.GetPageNumber is {document.GetRenderer().GetCurrentArea().GetPageNumber()}"));

(PageNumber test UsingNumberOfPages)

  • Related