Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pdf in Landscape using pdfBox

Currently I am working with PDFBox of Apache to generate pdf. It is working perfectly fine in portrait mode but then my requirement is that 1st two page should be in landscape mode and afterwards all other pages in portrait.

So can anyone please help me on how to create pdf in landscape and achieve this functionality??

Note:I cannot switch from PDFBox to other libraries

like image 244
Raushan Avatar asked May 31 '16 16:05

Raushan


People also ask

Is PDFBox free for commercial use?

Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative ...

What is the use of PDFBox?

It allows the creation of new PDF documents, manipulation of existing documents, bookmarking PDF and the ability to extract content from PDF documents. We can also use it to digitally sign, print and validate files against the PDF/A-1b standard. PDFBox library was originally developed in 2002 by Ben Litchfield.


2 Answers

Another solution would be

PDPage page = new PDPage(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
like image 104
wutzebaer Avatar answered Oct 17 '22 01:10

wutzebaer


There are two strategies:

1) assign a landscape mediabox (this is for A4):

float POINTS_PER_INCH = 72;
float POINTS_PER_MM = 1 / (10 * 2.54f) * POINTS_PER_INCH;
new PDPage(new PDRectangle(297 * POINTS_PER_MM, 210 * POINTS_PER_MM));

2) assign a portrait mediabox, rotate the page and rotate the CTM, as shown in the official example:

PDPage page = new PDPage(PDRectangle.A4);
page.setRotation(90);
doc.addPage(page);
PDRectangle pageSize = page.getMediaBox();
float pageWidth = pageSize.getWidth();
PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
// add the rotation using the current transformation matrix
// including a translation of pageWidth to use the lower left corner as 0,0 reference
contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0));
(...)
like image 38
Tilman Hausherr Avatar answered Oct 17 '22 02:10

Tilman Hausherr