Signup/Sign In

How to Read and Write PDF file in Java

In this post, we are going to learn to read and write a pdf file using Java code. The pdf is a portable document file that is used to transport informative documents. It is one of the widely used file formats.

To deal with pdf file in Java, we use pdfbox library which is the design and developed by the apache foundation. It is used to create, read, write, append the pdf file in an efficient way.

You must download this library before reading the pdf file. You can download it here. The downloaded files are basically JARs files so import them into your project and start working with pdf.

There are some important classes PDDocument, PDPage, PDPageContentStream, etc are required to load and fetch data.

If you are working with the maven project then use the following dependencies in your pom.xml file.

// pom.xml

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
  <!-- -->

It will automatically download all the required JARs in you project. So, you don't need to download the JARs manually. After adding these dependencies use the below example code in you Java project to read and write PDF file.

Time for an Example:

Let's start with an example to read a pdf file using the PDFBox library. Here, we have a pdf file test.pdf that we are loading with load() method and reading using getText() method.

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class Main {
	public static void main(String[] args) throws IOException{  
		try {
			// Creating file instance
			File file = new File("test.pdf");
			// Loading pdf file
			PDDocument document = PDDocument.load(file);
			PDFTextStripper pdfStripper = new PDFTextStripper();
			// Fetching PDF document
			String text = pdfStripper.getText(document);
			// Closing the document
		}catch(Exception e) {



Since PDFBox is made for pdf handling, then we can write data to a pdf file. Here, we are writing data to test.pdf file in append mode, for append mode we used APPEND constant in the PDPageContentStream so that the file data will not replace. We used setFont() method to set the font and save() method to save the changes to the file.

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;

public class Main {
	public static void main(String[] args) throws IOException{  
	      PDDocument document = PDDocument.load(new File("test.pdf")); 
	      PDPage page = document.getPage(0);
	      PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND,true,true);
	      //Setting the font  
	      contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);

	      //Setting the text position 
	      contentStream.newLineAtOffset(25, 500);

	      String text = "This message is writtern to the pdf file."; 
	      // Saving file after writing File("test.pdf"));

After executing this program, it will create a PDF file test.pdf in the current directory. You can open that file and see it contains the text that we saved with the code.

About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.