如何使用Apache PDFBox从PDF文件中提取文本
我想用Apache PDFBox从给定的PDF文件中提取文本。
我写了这段代码:
PDFTextStripper pdfStripper = null; PDDocument pdDoc = null; COSDocument cosDoc = null; File file = new File(filepath); PDFParser parser = new PDFParser(new FileInputStream(file)); parser.parse(); cosDoc = parser.getDocument(); pdfStripper = new PDFTextStripper(); pdDoc = new PDDocument(cosDoc); pdfStripper.setStartPage(1); pdfStripper.setEndPage(5); String parsedText = pdfStripper.getText(pdDoc); System.out.println(parsedText);
但是,我收到以下错误:
Exception in thread "main" java.lang.NullPointerException at org.apache.fontbox.afm.AFMParser.main(AFMParser.java:304)
我将pdfbox-1.8.5.jar和fontbox-1.8.5.jar添加到类路径中。
编辑
我添加了System.out.println("program starts");
到程序的开头。
我运行它,然后我得到了与上面提到的相同的错误, program starts
没有出现在控制台中。
因此,我认为我的课程路径有问题。
谢谢。
我执行了你的代码,它运行正常。 也许您的问题与您提交给文件的FilePath
有关。 我将我的pdf放在C盘中并硬编码文件路径。我的代码是:
// PDFBox 2.0.8 require org.apache.pdfbox.io.RandomAccessRead // import org.apache.pdfbox.io.RandomAccessFile; public class PDFReader{ public static void main(String args[]) { PDFTextStripper pdfStripper = null; PDDocument pdDoc = null; COSDocument cosDoc = null; File file = new File("C:/my.pdf"); try { // PDFBox 2.0.8 require org.apache.pdfbox.io.RandomAccessRead // RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); // PDFParser parser = new PDFParser(randomAccessFile); PDFParser parser = new PDFParser(new FileInputStream(file)); parser.parse(); cosDoc = parser.getDocument(); pdfStripper = new PDFTextStripper(); pdDoc = new PDDocument(cosDoc); pdfStripper.setStartPage(1); pdfStripper.setEndPage(5); String parsedText = pdfStripper.getText(pdDoc); System.out.println(parsedText); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
使用PDFBox 2.0.7,这就是我获取PDF文本的方法:
static String getText(File pdfFile) throws IOException { PDDocument doc = PDDocument.load(pdfFile); return new PDFTextStripper().getText(doc); }
这样叫:
try { String text = getText(new File("/home/me/test.pdf")); System.out.println("Text in PDF: " + text); } catch (IOException e) { e.printStackTrace(); }
由于用户oivemaria在评论中提到:
您可以在应用程序中使用PDFBox,将其添加到build.gradle
的依赖build.gradle
:
dependencies { compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.7' }
以下是使用Gradle进行依赖关系管理的更多信息。
如果要将PDF格式保留在已分析的文本中, 请尝试使用PDFLayoutTextStripper 。
PdfBox 2.0.3也有一个命令行工具。
- 下载jar文件
-
java -jar pdfbox-app-2.0.3.jar ExtractText [OPTIONS]
[output-text-file]
Options: -password
: Password to decrypt document -encoding
Maven dep:
org.apache.pdfbox pdfbox 2.0.9
然后将文件作为字符串获取pdf文本。
private static String readPDF(File pdf) throws InvalidPasswordException, IOException { try (PDDocument document = PDDocument.load(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"); List pdfLines = new ArrayList<>(); StringBuilder sb = new StringBuilder(); for (String line : lines) { System.out.println(line); pdfLines.add(line); sb.append(line + "\n"); } return sb.toString(); } } return null; }
这适用于使用pdfbox 2.0.6从具有文本内容的PDF文件中提取数据
import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.pdfbox.text.PDFTextStripperByArea; public class PDFTextExtractor { public static void main(String[] args) throws IOException { System.out.println(readParaFromPDF("C:\\sample1.pdf",3, "Enter Start Text Here", "Enter Ending Text Here")); //Enter FilePath, Page Number, StartsWith, EndsWith } public static String readParaFromPDF(String pdfPath, int pageNo, String strStartIndentifier, String strEndIdentifier) { String returnString = ""; try { PDDocument document = PDDocument.load(new File(pdfPath)); document.getClass(); if (!document.isEncrypted()) { PDFTextStripperByArea stripper = new PDFTextStripperByArea(); stripper.setSortByPosition(true); PDFTextStripper tStripper = new PDFTextStripper(); tStripper.setStartPage(pageNo); tStripper.setEndPage(pageNo); String pdfFileInText = tStripper.getText(document); String strStart = strStartIndentifier; String strEnd = strEndIdentifier; int startInddex = pdfFileInText.indexOf(strStart); int endInddex = pdfFileInText.indexOf(strEnd); returnString = pdfFileInText.substring(startInddex, endInddex) + strEnd; } } catch (Exception e) { returnString = "No ParaGraph Found"; } return returnString; } }