How to Extract Text from PDF Files Effortlessly
Tags: programming, java, pdf, txt
Published: 2023-06-29 09:08:36

Extracting text from a PDF file means converting the readable content within the PDF into plain text format. By utilizing dedicated text extraction tools or libraries, PDF files can be parsed, and the text content can be extracted accordingly. This enables convenient searching, analysis, and further processing of the extracted text. Text extraction typically entails removing non-text elements like formatting, layout, and images from the PDF, resulting in the retention of pure textual information. This process proves highly valuable across numerous scenarios, including tasks that require text analysis, data mining, or automated processing.

### Step 1, Download [Free Spire.PDF for Java](https://www.e-iceblue.com/Introduce/free-pdf-for-java.html) and unzip it.
This a the community version of Spire.PDF for Java and there are some limitations on pages during the process. If you want to get rid of the limitations, please download the commercial product from [this link](https://www.e-iceblue.com/Introduce/pdf-for-java.html). 
It supports create or edit PDF files independently in Java applications. What’s more, you can also [convert PDF to Word](https://www.e-iceblue.com/Tutorials/Java/Spire.PDF-for-Java/Program-Guide/Conversion/Convert-PDF-to-Word-in-Java.html), [merge PDF files](https://www.e-iceblue.com/Tutorials/JAVA/Spire.PDF-for-JAVA/Program-Guide/Document-Operation/Merge-PDF-documents-in-JAVA.html), [print PDF files](https://www.e-iceblue.com/Tutorials/Java/Spire.PDF-for-Java/Program-Guide/Print/How-to-print-PDF-document-in-Java.html) using this library.


### Step 2, Create a new Java project.


### Step 3, Import “Spire.Pdf.jar” to your project
Take IntelliJ IDEA 2018 (jdk 1.8.0) for example.

- Click “File”- “Project Structure”- “Modules”- “Dependencies” in turn.
- Choose the “JARs or Directories” under the green plus.
- Find the “Spire.Pdf.jar” in the lib folder of the decompressed package and import it to the project.


### Step 4, Write code
Or, you can refer to the sample come below:
```java
import com.spire.pdf.*;
import java.io.*;

public class ExtractTextFromPage {
    public static void main(String[] args) throws Exception{
        //Create a Pdf file
        PdfDocument doc = new PdfDocument();

        //Load the file from disk
        doc.loadFromFile("sample.pdf");

        //Create a new txt file to save the extracted text
        String result = "extractTextFromPage.txt";
        File file=new File(result);
        if(!file.exists()){
            file.delete();
        }
        file.createNewFile();
        FileWriter fw=new FileWriter(file,true);
        BufferedWriter bw=new BufferedWriter(fw);

        //Get the first page
        PdfPageBase page = doc.getPages().get(0);

        //Extract text from page keeping white space
        String text = page.extractText(true);

        bw.write(text);

        bw.flush();
        bw.close();
        fw.close();
    }
}
```
In this code snippet, we begin by creating a **PdfDocument** object and loading a PDF file. Subsequently, a new text file named "extractTextFromPage.txt" is generated to hold the extracted text content. The **getPages().get()** method is then employed to retrieve the first page of the PDF document. By utilizing the **extractText(true)** method on the **PdfPageBase** object, we can extract the text from the page while retaining whitespace. Lastly, the extracted text is written to the text file "extractTextFromPage.txt" using the **write()** method of the BufferedWriter object.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t7nw7b7hta8ewq4e5ef6.png)