Read/Extract Text from Pdf in Java
Tags: java, pdf, readpdf, extracttext
Published: 2018-11-28 06:12:17

---
title: Read/Extract Text from Pdf in Java
published: true
description: In this article, we’re going to explain how to read/extract text from a Pdf file in Java.
tags: Java, pdf, read pdf, extract text
---
In this article, we’re going to explain how to read/extract text from a Pdf file in Java.

An overview of content:
1. Read/Extract All Text from a Pdf
2. Read/Extract Text from a Specific Rectangle Area in a Pdf Page
3. Read/Extract Text using SimpleTextExtractionStrategy

<strong>The Pdf library we need:</strong>

Spire.PDF for Java (<strong>Spire.Pdf.jar</strong> download link: https://www.e-iceblue.com/Download/pdf-for-java.html)

<blockquote>
<p><em><strong>Spire.PDF for Java info:</strong> Spire.PDF for Java is a professional Java component that enables developers to create Pdf files from scratch or process existing Pdf files in Java application without having Adobe Acrobat to be installed.</em></p>
</blockquote>

The example Pdf file:
![alt text](https://thepracticaldev.s3.amazonaws.com/i/mgmqkn921g57254xbrx6.png "Example Pdf file")
 
# Sample Code

<strong>Imported Namespaces</strong>

```java
import com.spire.pdf.*;
import com.spire.pdf.exporting.text.SimpleTextExtractionStrategy;

import java.awt.geom.Rectangle2D;
import java.io.*;
```

<strong>Read/Extract All Text from a Pdf</strong>

```java
//Instantiate a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load the Pdf file
pdf.loadFromFile("Additional.pdf");

StringBuilder sb= new StringBuilder();

//Extract text from every page of the Pdf
for (PdfPageBase page: (Iterable<PdfPageBase>) pdf.getPages()) {
    sb.append(page.extractText(true));
}

try {
    //Write the text into a .txt file 
    FileWriter writer = new FileWriter("ExtractText.txt");
    writer.write(sb.toString());
    writer.flush();
} catch (IOException e) {
    e.printStackTrace();
}

//Close the PdfDocument object
pdf.close();
``` 
Output: ![alt text](https://thepracticaldev.s3.amazonaws.com/i/9yk5ewcr1e1dzo88drow.png "Extract all text from Pdf using Spire.PDF for Java")


<strong>Read/Extract Text from a Specific Rectangle Area in a Pdf Page</strong>

```java
//Instantiate a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load the Pdf file
pdf.loadFromFile("Additional.pdf");

//Get the first page of the Pdf
PdfPageBase page = pdf.getPages().get(0);

//Instantiate a Rectangle2D object 
Rectangle2D rect = new Rectangle2D.Float();
//Set location and size
rect.setFrame( 50, 50, 500, 100);

//Extract text from the given rectangle area in the first page
StringBuilder sb= new StringBuilder();
StringBuilder append = sb.append(page.extractText(rect));

try {
    //Write the text into a .txt file 
    FileWriter writer = new FileWriter("ExtractText.txt");
    writer.write(sb.toString());
    writer.flush();
} catch (IOException e) {
    e.printStackTrace();
}

//Close the PdfDocument object
pdf.close();
``` 
Output: ![alt text](https://thepracticaldev.s3.amazonaws.com/i/ydl0b7smpc40dno63d1e.png "Extract text from a specific rectangle area using Spire.PDF for Java")

<strong>Read/Extract Text using SimpleTextExtractionStrategy</strong>

```java
//Instantiate a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load the Pdf file
pdf.loadFromFile("Additional.pdf");

//Get the first page of the Pdf
PdfPageBase page = pdf.getPages().get(0);

//Extract text from the first page using SimpleTextExtractionStrategy
SimpleTextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
StringBuilder sb= new StringBuilder();
StringBuilder append = sb.append(page.extractText(strategy));

try {
    //Write the text into a .txt file 
    FileWriter writer = new FileWriter("ExtractText.txt");
    writer.write(sb.toString());
    writer.flush();
} catch (IOException e) {
    e.printStackTrace();
}

//Close the PdfDocument object
pdf.close();

```
Output: ![alt text](https://thepracticaldev.s3.amazonaws.com/i/dhvvut9r53y7zvmbdau7.png "Extract Text using SimpleTextExtractionStrategy using Spire.PDF for Java")