13

Started playing with PDFBox

PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage( page );

PDFont font = PDType1Font.HELVETICA_BOLD;
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( 100, 700 );
contentStream.drawString( "Hello World" );
contentStream.endText();
contentStream.close();

document.save("Page.pdf");
document.close();

but I want to set the file size to be PDPage.PAGE_SIZE_A5. I've tried setting all the setXXXBox(PDRectangle mediaBox) method signatures but I can't get the expected output.

page.setArtBox(PDPage.PAGE_SIZE_A5); // ??
page.setMediaBox(PDPage.PAGE_SIZE_A5); // ??

Any ideas?

zmf
  • 9,095
  • 2
  • 26
  • 28
emeraldjava
  • 10,894
  • 26
  • 97
  • 170

2 Answers2

32

Quick note: in PDFBox 2 replace PDPage.PAGE_SIZE_A5 with PDRectangle.A5, i.e.

PDPage page = new PDPage(PDRectangle.A5);
Alok P
  • 994
  • 1
  • 9
  • 18
17

Use PDPage.PAGE_SIZE_A5 to change size to A5

PDPage page = new PDPage(PDPage.PAGE_SIZE_A5);
Bruce
  • 8,609
  • 8
  • 54
  • 83
cedarsoft
  • 216
  • 2
  • 5