- PDFBox - Home
- PDFBox - Overview
- PDFBox - Environment
- PDFBox - Creating a PDF Document
- PDFBox - Adding Pages
- PDFBox - Loading a Document
- PDFBox - Removing Pages
- PDFBox - Document Properties
- PDFBox - Adding Text
- PDFBox - Adding Multiple Lines
- PDFBox - Reading Text
- PDFBox - Inserting Image
- Encrypting a PDF Document
- JavaScript in PDF Document
- PDFBox - Splitting a PDF Document
- Merging Multiple PDF Documents
- PDFBox - Converting PDF To Image
- PDFBox - Adding Rectangles
PDFBox - Useful Resources
PDFBox - Adding Text
In the previous chapter, we discussed how to add pages to a PDF document. In this chapter, we will discuss how to add text to an existing PDF document.
Adding Text to an Existing PDF Document
You can add contents to a document using the PDFBox library, this provides you a class named PDPageContentStream which contains the required methods to insert text, images, and other types of contents in a page of a PDFDocument.
Following are the steps to create an empty document and add contents to a page in it.
Loading an Existing PDF Document
The load() method of the PDDocument class is used to load an existing PDF document. Follow the steps given below to load an existing PDF document.
Step 1: Loading an Existing PDF Document
Load an existing PDF document using the static method loadPDF() of the Loader class. This method accepts a RandomAccessReadBufferedFile object as a parameter, since this is a static method you can invoke it using class name as shown below.
// Loading an existing document
PDDocument document = Loader.loadPDF(
new RandomAccessReadBufferedFile("D:/Projects/PDFBox/PdfBox_Examples/my_doc.pdf"));
Step 2: Getting the Required Page
You can get the required page in a document using the getPage() method. Retrieve the object of the required page by passing its index to this method as shown below.
PDPage page = doc.getPage(1);
Step 3: Preparing the Content Stream
You can insert various kinds of data elements using the object of the class PDPageContentStream. You need to pass the document object and the page object to the constructor of this class therefore, instantiate this class by passing these two objects created in the previous steps as shown below.
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
Step 4: Beginning the Text
While inserting text in a PDF document, you can specify the start and end points of the text using the beginText() and endText() methods of the PDPageContentStream class as shown below.
contentStream.beginText(); .. code to add text content .. contentStream.endText();
Therefore, begin the text using the beginText() method as shown below.
contentStream.beginText();
Step 5: Setting the Position of the Text
Using the newLineAtOffset() method, you can set the position on the content stream in the page.
//Setting the position for the line contentStream.newLineAtOffset(25, 700);
Step 6: Setting the Font
You can set the font of the text to the required style using the setFont() method of the PDPageContentStream class as shown below. To this method you need to pass the type and size of the font.
contentStream.setFont( font_type, font_size );
Step 7: Inserting the Text
You can insert the text into the page using the ShowText() method of the PDPageContentStream class as shown below. This method accepts the required text in the form of string.
contentStream.showText(text);
Step 8: Ending the Text
After inserting the text, you need to end the text using the endText() method of the PDPageContentStream class as shown below.
contentStream.endText();
Step 9: Closing the PDPageContentStream
Close the PDPageContentStream object using the close() method as shown below.
contentstream.close();
Step 10: Saving the Document
After adding the required content, save the PDF document using the save() method of the PDDocument class as shown in the following code block.
doc.save("Path");
Step 11: Closing the Document
Finally, close the document using the close() method of the PDDocument class as shown below.
doc.close();
Example - Adding Text to PDF Document
This example demonstrates how to add contents to a page in a document. Here, we will create a Java program to load the PDF document named my_doc.pdf, which is saved in the path D:/Projects/PDFBox/PdfBox_Examples/, and add some text to it. Save this code in a file with name PDFBoxDemo.java.
package com.tutorialspoint.pdfbox;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName;
public class PDFBoxDemo {
public static void main(String args[]) throws IOException {
// Loading an existing document
PDDocument document = Loader.loadPDF(
new RandomAccessReadBufferedFile("D:/Projects/PDFBox/PdfBox_Examples/my_doc.pdf"));
//Retrieving the pages of the document
PDPage page = document.getPage(1);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
//Begin the Content stream
contentStream.beginText();
//Setting the font to the Content stream
contentStream.setFont(new PDType1Font(FontName.TIMES_ROMAN), 12);
//Setting the position for the line
contentStream.newLineAtOffset(25, 500);
String text = "This is the sample document and we are adding content to it.";
//Adding text in the form of string
contentStream.showText(text);
//Ending the content stream
contentStream.endText();
System.out.println("Content added");
//Closing the content stream
contentStream.close();
//Saving the document
document.save(new File("D:/Projects/PDFBox/PdfBox_Examples/new.pdf"));
//Closing the document
document.close();
}
}
Output
Compile and run the code to verify the following output −
Content added
If you verify the PDF Document new.pdf in the specified path, you can observe that the given content is added to the document as shown below.