My code takes pages, one by one, from an existing PDF, and adds them to a new PDF, drawing text on each page as it goes. (There's more, but that the part that's having an issue.)
When the original PDF has a Landscape format page, the text comes up sideways, and positioned incorrectly. To illustrate, I would get the same result if I added text to a Portrait page and rotated the lot 90 degrees anti-clockwise.
I've tried rotating the XGraphics object with which I'm drawing the text, but that only fixes orientation, not positioning. Some text is thrown off the page due to the XGraphics taking a Portrait shape on a Landscape page.
Code:
//This code puts the text onto Landscape pages sideways (vertical)
//And in the wrong position
//Exactly as if the page was turned portrait, then text added correctly, then the whole lot returned to portrait
XGraphics gfx;
XRect box;
XStringFormat formatTitle = new XStringFormat();
XStringFormat formatTopLeft = new XStringFormat();
XStringFormat formatTopCenter = new XStringFormat();
XStringFormat formatTopRight = new XStringFormat();
XStringFormat formatBottomLeft = new XStringFormat();
XStringFormat formatBottomCenter = new XStringFormat();
XStringFormat formatBottomRight = new XStringFormat();
formatTitle.Alignment = XStringAlignment.Center;
formatTitle.LineAlignment = XLineAlignment.Center;
formatTopLeft.Alignment = XStringAlignment.Near;
formatTopLeft.LineAlignment = XLineAlignment.Near;
formatTopCenter.Alignment = XStringAlignment.Center;
formatTopCenter.LineAlignment = XLineAlignment.Near;
formatTopRight.Alignment = XStringAlignment.Far;
formatTopRight.LineAlignment = XLineAlignment.Near;
formatBottomLeft.Alignment = XStringAlignment.Near;
formatBottomLeft.LineAlignment = XLineAlignment.Far;
formatBottomCenter.Alignment = XStringAlignment.Center;
formatBottomCenter.LineAlignment = XLineAlignment.Far;
formatBottomRight.Alignment = XStringAlignment.Far;
formatBottomRight.LineAlignment = XLineAlignment.Far;
for (int idx = 0; idx < count; idx++)
{
PdfPage nextPage = inputDocument.PageCount > idx ? inputDocument.Pages[idx] : new PdfPage();
nextPage = outputDocument.AddPage(nextPage);
gfx = XGraphics.FromPdfPage(nextPage);
box = nextPage.MediaBox.ToXRect();
box.Inflate(-10, -10);
gfx.DrawString(text1, font, XBrushes.Black, box, formatTopLeft);
gfx.DrawString(text2, font, XBrushes.Black, box, formatTopCenter);
gfx.DrawString(text3, font, XBrushes.Black, box, formatTopRight);
gfx.DrawString(text4, font, XBrushes.Black, box, formatBottomLeft);
gfx.DrawString(text5, font, XBrushes.Black, box, formatBottomCenter);
gfx.DrawString(text6, font, XBrushes.Black, box, formatBottomRight);
}
Tested on PdfSharp v1.50, and on v1.51