enigma969
enigma969

Reputation: 427

Change orientation of existing PDF document (portrait/landscape)

Is it possible to change the orientation of an existing document from 'portrait' to 'landscape' or vise versa?

I've tried copying a page (in portrait mode) to a newly created page (in landscape mode) using iText but it didn't work, the page orientation of the copied page was used.

Here's the code I used:

PdfReader originalFileReader = new PdfReader(src);

Document landscapeDoc = new Document(PageSize.A4.rotate());

PdfCopy copy = new PdfCopy(landscapeDoc, new FileOutputStream("/home/user/landscape.pdf"));

landscapeDoc.open();

for (int i = 1; i <= originalFileReader.getNumberOfPages(); i++) {
     copy.addPage(copy.getImportedPage(originalFileReader, i));
}

landscapeDoc.close();

Upvotes: 0

Views: 1575

Answers (1)

DavidW
DavidW

Reputation: 1421

Getting a page from the original file and adding it to the copy doesn't re-layout the page. If you get a landscape page at all, it will simply contain a copy of the original page clipped to the height of the landscape page.

Looking at the iText site it appears that the 2 closest use-cases to what you want are extracting data fields (marked up using a template) from a PDF to an XML structure (pdf2Data) and adding content (watermarks, images, annotations, etc.) to an existing PDF. (Lots of examples here.)

There's nothing there about intelligently pulling content and formatting from a PDF and re-laying it out in a different PDF. (Which would be an extremely hard problem anyway.)

Upvotes: 1

Related Questions