Pdf in Landscape using pdfBox

18,782

Solution 1

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));
(...)

Solution 2

Another solution would be

PDPage page = new PDPage(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
Share:
18,782
Raushan
Author by

Raushan

Updated on July 27, 2022

Comments

  • Raushan
    Raushan almost 2 years

    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

  • Almir Campos
    Almir Campos over 7 years
    IMHO this should be the accepted answer. Simpler and more elegant.
  • gkbstar
    gkbstar almost 6 years
    great, but after changing to the landscape my already written content is lost i.e. header which contains some images. Also, after changing to landscape does the coordinate also changes. If yes can you please suggest how.
  • Tilman Hausherr
    Tilman Hausherr almost 6 years
    Does "my already written content" mean you want to change an existing PDF? Better create a new question, post your code and share your PDF. No coordinate system doesn't change (0,0 is at bottom left) but of course your maximums will. The top would be cut off, obviously, and at the right you'd have more space. The question here is about creating a PDF from scratch. Changing an existing PDF from portrait to landscape doesn't make much sense.
  • gkbstar
    gkbstar almost 6 years
    Thanks for the response. Basically my objective was to increase the number of columns in the existing pdf. Anyways the strategy 1 worked for me and i have updated the existing code. Thanks again. Vote +1
  • Tomek
    Tomek over 3 years
    This is a good solution also for another reason - when a third-party library automatically adds another page to your document (for whatever reason), basing the new page on the landscape one, then the new page is also in landscape orientation. With the page rotation solution that isn't the case.
  • Karl.S
    Karl.S about 2 years
    by far the best solution, it resolved every problems I had, the official example code is not working because even if the page is rotated in landscape with the content, the resulting MediaBox remains in portrait within the landscape page...