To summarize, I'm using Itextsharp to import specific pages from a PDF, possibly rotate, resize or otherwise alter that page, and exporting it into a new PDF. To this end I'm using the PDFWriter class from Itext. The problem I'm running into is that when using the writer class, it doesn't appear to be including the visible annotations that appear on the source page (in my case it's a comment) in the output page. Interestingly it does include the embedded OCR without issue.
Additionally, when using the Itext PDFcopy class it does work just as I want it to (copying comments properly to source), unfortunately PDFcopy doesn't have a lot of easily accessible functionality for the sorts of things I need to do with the page (e.g. resizing pages).
So I am looking for one of two solutions:
-Preferably I'd like to continue to use the writer class, but need it to copy/duplicate any visible annotations etc. from the source pages and include them in the output.
-Need some example code of using the PDFCopy class to resize a page. There is a pdfcopy.Setpagesize function (which doesn't work, which I suspect means I'm doing it wrong), but I have basically no idea how to properly scale the source page when it needs to be resized.
Here's some pseudo code to give you an idea of where I'm at in terms of using the PDFWriter class:
'...
Dim MS As New MemoryStream()
Dim document As New Document
Dim WriterPDF As PdfWriter = PdfWriter.GetInstance(document, MS)
Dim reader As PdfReader = Nothing
Dim cb As PdfContentByte = WriterPDF.DirectContent
reader = New PdfReader(New MemoryStream(File.ReadAllBytes(FilePathList.Item(ItemNum))))
For Each PageItem As Integer In PageNumList
Dim page As PdfImportedPage = WriterPDF.GetImportedPage(reader, PageItem)
Dim PageSizeStandard As Rectangle = PageSize.LETTER
document.SetPageSize(PageSizeStandard)
Dim tm = New System.Drawing.Drawing2D.Matrix()
'code to resize, rotate etc... tm.scale, tm.rotate, etc.
cb.AddTemplate(page, tm)
document.NewPage()
next
As the alternative, the PDFCopy code I'm using for rotation involves:
Dim MasterCopy As PdfCopy = New PdfCopy(document, New FileStream(outputPath, FileMode.Create))
'...
Dim PageDict As PdfDictionary = reader.GetPageN(PageItem)
' can get current rotation with this...
' Dim Rot As PdfNumber = PageDict.Get(PdfName.ROTATE)
'...
Dim RotatedPageSizeHeight As Single = reader.GetPageSizeWithRotation(PageItem).Height
Dim RotatedPageSizeWidth As Single = reader.GetPageSizeWithRotation(PageItem).Width
If RotatedPageSizeWidth > RotatedPageSizeHeight Then
PageDict.Put(PdfName.ROTATE, New PdfNumber(90))
'there is a Pdfname.Size, but no idea if that's even what I need or how to use it.
'with the writer class I use a matrix to scale the page, works fine.
End If
'...
MasterCopy.AddPage(page)
Sorry the pseudo code is a bit fragmented, trying to keep it brief. Please let me know if I can provide any additional information. And thanks in advance!
PdfWriter. If you only want to rotate pages in an existing PDF, the answer is really simple. If you also want to scale a PDF, you're in for a huge deal of programming work (and StackOverflow subscribers won't do your work in your place). - Bruno Lowagie