-3

I'm attempting to write page numbers into my PDF document with itextsharp. I've followed the example here. This answer points me in the direction of this implementation in C#.

Now, all works fine - assuming the page orientation is A4. In my case, it's not. I'm using a landscape A3 page. Because I want to nicely position the page number, I need the dimensions of the page I'm working on.

stamper.GetOverContent().PdfDocument.PageSize seems to always return the dimensions of an A4 page.

Here's a reproducible example:

using (var ms = new MemoryStream())
{
    using (var doc = new Document(PageSize.A3.Rotate()))
    {
        Debug.WriteLine(doc.PageSize);
        var writer = PdfWriter.GetInstance(doc, ms);

        doc.Open();
        doc.Add(new Paragraph("Hello!"));
    }

    byte[] firstPass = ms.ToArray();

    PdfReader reader = new PdfReader(firstPass);
    using (var fs = new FileStream("out2.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
    {
        using (PdfStamper stamper = new PdfStamper(reader, fs))
        {
            int totalPages = reader.NumberOfPages;
            for (var i = 1; i <= totalPages; i++)
            {
                var under = stamper.GetUnderContent(i); 
                var over = stamper.GetOverContent(i);

                Debug.WriteLine(under.PdfDocument.PageSize);
                Debug.WriteLine(over.PdfDocument.PageSize);
            }
        }
    }
}

The output of which is:

Rectangle: 1191x842 (rot: 90 degrees)
RectangleReadOnly: 595x842 (rot: 0 degrees)
RectangleReadOnly: 595x842 (rot: 0 degrees)

How does one properly get the page size of documents with the PdfStamper?

Please note, this question is not about generating page numbers with iTextSharp. There are various workaround. This question is particularly about reading the correct dimensions of a document via PdfStamper.

Rob
  • 26,989
  • 16
  • 82
  • 98

1 Answers1

3

I haven't got an explanation for why stamper.GetUnderContent(i).PdfDocument defaults to A4, however, the correct way to get the page size is:

var pageSize = reader.GetPageSizeWithRotation(i);

Note that this is the full page size, including margins.

Rob
  • 26,989
  • 16
  • 82
  • 98
  • 2
    There is no reason to get the `PdfDocument` instance of the `PdfContentByte` of a `PdfStamper` page. We should never have exposed that object because by exposing it, people think they can use it. We have been dragging along design errors like that since the first versions of iText (I was a self-taught developer; I released the first version of iText in 2000; I still feel bad when I see my code from those days). In 2016, we finally had the means to rewrite iText from scratch. You won't see design errors like that in iText 7 (which you should use if you want your code to be future-proof). – Bruno Lowagie Nov 22 '17 at 08:19
  • 3
    You are using an iText with the old (pre-7.0) API. That API has grown a lot, sometimes wildly, which resulted in some weirdness. The `PageSize` of a `PdfDocument` makes only sense when that class is used for pdf creation from scratch using a genuine `PdfWriter`. In a `PdfStamper` use case it contains a default value which is of no interest. – mkl Nov 22 '17 at 08:58
  • @BrunoLowagie Well now I feel a bit stupid - I didn't actually realize iText7 was a separate NuGet package from iTextSharp. Taking a look now; feels a lot nicer. Cheers for the tip – Rob Nov 22 '17 at 09:57
  • 3
    No need to feel stupid. That's how I feel when I look at code I wrote 10 years ago (and probably how I'll feel when I look at code I write today). – Bruno Lowagie Nov 22 '17 at 10:42