Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create mixed orientation PDF in iTextSharp

I am merging PDF buffers using this code at http://web.archive.org/web/20111012184438/http://alex.buayacorp.com/merge-pdf-files-with-itext-and-net.html [Mirror]

My PDFs have mixed page orientation, some are portrait and some are landscape (but all are A4)

The code does not maintain the orientation of each page and uses the orientation of the first page throughout the document. How do I go about creating a mixed orientation PDF using this code.

like image 949
Midhat Avatar asked Mar 11 '11 13:03

Midhat


1 Answers

The trick to using multiple page sizes is to call SetPageSize() just before calling NewPage(). Something like this should work (I didn't compile this but it should be pretty close):

PdfImportedPage importedPage = pdfWriter.GetImportedPage(pdfReader, page);
newDocument.SetPageSize(new iTextSharp.Text.Rectangle(0.0F, 0.0F, importedPage.Width, importedPage.Height));
newDocument.NewPage();
pdfContentByte.AddTemplate(importedPage, 0, 0);
like image 92
Chris Haas Avatar answered Nov 04 '22 13:11

Chris Haas