Ok, so I need to open an existing PDF, copy its pages to another PDF and add a top margin space. This all works well with the below code, except on some PDFs I see coming through our system, which have orientation = portrait and rotation = 270.
When the below output document is created in these instances, the top of the content in the portrait page is cut off.
It seems that the 'window' that is used to snap the image for import into the XPdfForm automatically rotates to 270 (i.e. landscape), and takes its shot thereby missing the PDF content at the top of the page in Portrait orientation. I don't seem to be able to do anything about this, I have tried a number of variations regarding the output doc, but the issue seems to be with the Input form whose PixelHeight = 612 & PixelWidth = 792. I would have expected these to be the opposite for a page with a Portrait orientation.
Playing around with widths, heights, setting orientations, rotations, none of these seemed to matter, the output doc is still chopped. The issue seems to be with how the Form object is getting set up. I have reviewed numerous similar threads, which are related but do not seem to hit on this issue, and am not able to find a resolution
I have the files but they are a few kB too big, 256 is a very small file limit. I'm using PDFsharp-MigraDoc-gdi 1.50.4790-beta5a
Help would be greatly appreciated, thank you in advance!
Here's the code I am using
Code:
form = XPdfForm.FromFile(destination);
PdfSharp.Pdf.PdfDocument outputDocument = new PdfSharp.Pdf.PdfDocument();
XGraphics gfx;
XRect box;
for (int idx = 0; idx < form.PageCount; idx++)
{
// Add a new page to the output document
PdfSharp.Pdf.PdfPage page = outputDocument.AddPage();
XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.Letter);
//double width = 0d;
//double height = 0d;
if (form.PixelWidth > form.PixelHeight)
{
page.Width = size.Height;
page.Height = size.Width;
}
else
{
page.Width = size.Width;
page.Height = size.Height;
}
int rotate = page.Elements.GetInteger("/Rotate");
gfx = XGraphics.FromPdfPage(page);
box = new XRect(0, 25, page.Width, (page.Height - 25));
// Draw the page identified by the page number like an image
gfx.DrawImage(form, box);
form.PageNumber++;
}
I have also tried simply opening the existing doc and adding the pages to a new document and that works fine, but as I understand I cannot add margins that way, I have to redraw the existing pages onto a new page using the gfx object if I wish to do this.