This small sample shows how to create a new PDF document using PDFBox.
First we need a new empty document:
document = new PDDocument();
As every document needs at least one page we have to add a blank page to the newly created document:
PDPage blankPage = new PDPage(); document.addPage( blankPage );
we can save the newly created document:
document.save("BlankPage.pdf");
finally we have to ensure that the document is properly closed:
document.close();
Full source code at CreateBlankPDF
This small sample shows how to create a new document and print the text "Hello World" using one of the PDF base fonts.
First we need a new empty document and add a page to it:
document = new PDDocument(); PDPage page = new PDPage(); document.addPage( page );
Next we have to create a new font object selecting one of the PDF base fonts:
PDFont font = PDType1Font.HELVETICA_BOLD;
Next we start a new content stream which will "hold" the to be created content:
PDPageContentStream contentStream = new PDPageContentStream(document, page);
Next we define a text content stream using the selected font, moving the cursor and drawing the text "Hello World":
contentStream.beginText(); contentStream.setFont( font, 12 ); contentStream.moveTextPositionByAmount( 100, 700 ); contentStream.drawString( "Hello World" ); contentStream.endText();
We have to make sure that the content stream is closed:
contentStream.close();
Finally we save the results and ensure that the document is properly closed:
document.save( "Hello World.pdf");
document.close();
Full source code at HelloWorld
This small sample shows how to create a new document and print the text "Hello World" using using a TrueType Font.
First we need a new empty document and add a page to it:
document = new PDDocument(); PDPage page = new PDPage(); document.addPage( page );
Next we have to create a new font object loading a TrueType font into the document:
PDFont font = PDTrueTypeFont.loadTTF(document, "Arial.ttf");
Next we start a new content stream which will "hold" the to be created content:
PDPageContentStream contentStream = new PDPageContentStream(document, page);
Next we define a text content stream using the selected font, moving the cursor and drawing the text "Hello World":
contentStream.beginText(); contentStream.setFont( font, 12 ); contentStream.moveTextPositionByAmount( 100, 700 ); contentStream.drawString( "Hello World" ); contentStream.endText();
We have to make sure that the content stream is closed:
contentStream.close();
Finally we save the results and ensure that the document is properly closed:
document.save( "Hello World.pdf");
document.close();
Full source code at HelloWorldTTF