Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFBox setting A5 page size

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?

like image 202
emeraldjava Avatar asked Jun 28 '10 16:06

emeraldjava


People also ask

Which is better iText or PDFBox?

One major difference is that PDFBox always processes text glyph by glyph while iText normally processes it chunk (i.e. single string parameter of text drawing operation) by chunk; that reduces the required resources in iText quite a lot.

What is PDRectangle?

public class PDRectangle extends Object implements COSObjectable. A rectangle in a PDF document.

Is PDFBox thread safe?

Is PDFBox thread safe? No! Only one thread may access a single document at a time. You can have multiple threads each accessing their own PDDocument object.


2 Answers

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

PDPage page = new PDPage(PDRectangle.A5);
like image 91
Alok P Avatar answered Oct 03 '22 04:10

Alok P


Use PDPage.PAGE_SIZE_A5 to change size to A5

PDPage page = new PDPage(PDPage.PAGE_SIZE_A5);
like image 43
cedarsoft Avatar answered Oct 03 '22 04:10

cedarsoft