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
.