簡體   English   中英

PDFBox | 在線崩潰:PDDocument.load(file)

[英]PDFBox | Crash on line: PDDocument.load(file)

我有以下簡化代碼:

PDFTextStripper pdfStripper = new PDFTextStripper();
PDDocument doc;
String text = "";

try {
    File textFile = new File("C:/Users/user/Desktop/PDF-test.txt");
    doc = PDDocument.load(textFile);
    text = pdfStripper.getText(doc);
} finally {
   ...
}

...

PDPageContentStream content = new PDPageContentStream(doc, page);

content.setFont(font, 12);

content.beginText();
// Write to page using a text file
content.showText(text);
content.endText();
content.close();

問題

我收到以下錯誤: java.io.IOException: Error: End-of-File, expected line on the line:

doc = PDDocument.load(textFile); try塊中。


我試過的

我已經嘗試了這些解決方案,但都沒有奏效:


預期成績

我想正確加載文本文件並將其顯示為帶有 PDFBox 的 PDF。

PDDocument.load 需要 pdf 文件,而不是 txt 文件。

參見 PDDocument 的 javadoc: https://pdfbox.apache.org/docs/2.0.2/javadocs/org/apache/pdfbox/pdmodel/PDDocument.html#load(java.ZF98ED017A4D5F50F47DE17A4D5F50F47DE1)

在 PDFBox 3.0 中,您必須使用org.apache.pdfbox.Loader.loadPDF(new File(...))

更多信息,請查看https://pdfbox.apache.org/3.0/migration.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM