PDFBox - Comment lire un fichier PDF en Java

PDFBox - Comment lire un fichier PDF en Java

Cet article vous montre comment utiliserApache PDFBox pour lire un fichier PDF en Java.

1. Obtenir PDFBox

pom.xml


        org.apache.pdfbox
        pdfbox
        2.0.6

2. Imprimer le fichier PDF

Exemple pour extraire tout le texte d'un fichier PDF.

ReadPdf.java

package com.example;

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

import java.io.File;
import java.io.IOException;

public class ReadPdf {

    public static void main(String[] args) throws IOException {

        try (PDDocument document = PDDocument.load(new File("/path-to/abc.pdf"))) {

            document.getClass();

            if (!document.isEncrypted()) {

                PDFTextStripperByArea stripper = new PDFTextStripperByArea();
                stripper.setSortByPosition(true);

                PDFTextStripper tStripper = new PDFTextStripper();

                String pdfFileInText = tStripper.getText(document);
                //System.out.println("Text:" + st);

                // split by whitespace
                String lines[] = pdfFileInText.split("\\r?\\n");
                for (String line : lines) {
                    System.out.println(line);
                }

            }

        }

    }
}

Note
Veuillez vous référer à cepdfbox svn pour plus d'exemples