I've got some code in vb.net using itext7 to loop through a source pdf and add each page to a new pdf with a larger page size - effectively n-uping.
This much I have mostly working, but I'm having difficulty when one of the pages in the source doc is rotated (ie it's a landscape page someone has added amongst the rest of the portrait pages in the source file)
I can detect rotation with .getrotation, so I can find when this happens, but I can't figure out how to rotate the page so it's the right way around in the new file. Adding it as it stands means it's 90 degrees rotated.
Here's some sample code - I've cut out all the irrelevant stuff for clarity, so I'm just adding a single page from the source doc to the output:
Function rotate_pdf()
Dim inputpdf As String = "c:\test\A4.pdf"
Dim outputpdf As String = "c:\test\rotated.pdf"
Dim newsize As iText.Kernel.Geom.Rectangle = New iText.Kernel.Geom.Rectangle(1190, 850)
Dim pdfDoc As New PdfDocument((New PdfWriter(outputPDF)).SetSmartMode(True))
Dim srcDoc As New PdfDocument(New PdfReader(inputpdf))
pdfDoc.SetDefaultPageSize(New PageSize(newsize))
Dim page1 As PdfPage = srcDoc.GetPage(1)
Dim pagecopy1 As PdfFormXObject = page1.CopyAsFormXObject(pdfDoc)
Dim page As PdfPage = pdfDoc.AddNewPage()
Dim canvas As New PdfCanvas(page)
canvas.AddXObject(pagecopy1, 20, 20)
pdfDoc.Close()
srcDoc.Close()
MsgBox("Done")
End Function
Can anyone tell me how to rotate the page from the source doc so it's correctly oriented in my output doc?
Thanks!
As per comment, use:
canvas.AddXObjectWithTransformationMatrix(pagecopy1, 0, -1.0F, 1.0F, 0, 0, A4height)