Extract/Read Text from PDF in Java

PDF is a file format that cannot be eaisly edited, and it’s diffcult for us to directly extract the content inside it. This article will demonstrate how to extract text from a PDF file with a free Java API (Free Spire.PDF for JAVA).

Method 1: Download the free API and unzip it.Then add the Spire.Pdf.jar file to your project as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.


Java Code

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import java.io.*;

public class Extract_Text {

public static void main(String[] args) {

//Create a PdfDocument instance
PdfDocument doc=new PdfDocument();
//Load the PDF file

//Create a StringBuilder instance
StringBuilder sb=new StringBuilder();

PdfPageBase page;

//Loop through PDF pages and get text of each page
for(int i=0;i<doc.getPages().getCount();i++){
FileWriter writer;
try {
//Write text into a .txt file
writer = new FileWriter("ExtractText.txt");
} catch (IOException e) {


The output .txt file:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store