PdfPig
PdfPig copied to clipboard
'Hard' rotate a pdf with PdfPig
Hi @EliotJones ,
I'm a long time user of PdfPig and absolutely love the library. I was wondering if there is a way to rotate an existing PDF with PdfPig, while also rotating the coordinate system with it. To give you some more context: sometimes we receive landscape pdf's (e.g. exports from excel) that have the rotation property set to 90, but in these documents, the origin of the coordinate system is in the top left (or bottom left when viewed as a portrait).
As a lot of processes and renderers further in our pipeline depend on the origin being in the bottom left, a solution would be to preprocess these pdfs where we set the rotation back to 0, and then perform a 'hard' rotation where all the elements inside the pdf are rotated 90 degrees around the origin, translated into view, and then change the dimensions of the crop/media box.
I'm not sure if this is the best solution, but it prevents us changing rendering logic further down the pipeline that we may or may not have control over. Another potential solution I read about is exporting the pdf as PDF/A, but I'm not sure.
I'm wondering if PdfPig exposes any functionalities that would solve this problem.
Hi, @EliotJones is kind of unavailable right now. But, I would see what I can do to help you with this problem :)
I have two question that could determine how fast we fix this problem
- Do you want to rotate pages element, like with a transformation? Or do you want to rotate the way is presented to you. Like, when you open the pdf in Adobe Reader DC.
- Do you have any sample pdf, that you can provide to use? Or even some images that can illustrate what you are trying to accomplish.
Thanks.
Hi @InusualZ ,
Thanks for offering your help! Much appreciated.
To answer your question: ideally, the document shouldn't visually change at all. The document should appear the exact same way as before the transformation occurred; all pages shown in landscape.
I've included an example with a landscape pdf that has the rotation property set to 90 degrees. I have programmatically drawn a string at approximately point (1, 1). As you can see, it shows up in the top left corner (with the wrong orientation), instead of the bottom left.
One logical way to go forward would be to make the drawing code respect the rotation. However, as I said before, this currently isn't an option as there are many viewers/renderers down the line that we have no control over (and that do not respect this rotation). So the idea is to add a normalization step to all incoming pdfs that set the Rotation property to 0, but also 'hard' rotate the content so the document visually appears unchanged.
Update: I found the solution using another library by copying each page and adding Form XObjects with an affine transformation to rotate the content that way.
In iTextSharp it would look something like this:
page.DirectContent.AddTemplate(pageCopy, 0, -1f, 1f, 0, 0, pageHeight)
I am also looking to do this with pdfpig.
The latest nightly build includes this functionality on PdfPageBuilder.SetRotation