0

我尝试从此页面运行 Pdfbox 示例:http: //www.printmyfolders.com/Home/PDFBox-Tutorial 并从 PDF 文件中提取文本。当我尝试运行它时,出现错误:

org.apache.pdfbox.exceptions.WrappedIOException
   at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:245)
   at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1192)
   at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1159)
   at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1130)
   at GetPos.main(GetPos.java:14)
Caused by: java.lang.ArrayIndexOutOfBoundsException
   at java.lang.System.arraycopy(libgcj.so.10)
   at java.io.ByteArrayOutputStream.write(libgcj.so.10)
   at org.apache.pdfbox.filter.FlateFilter.decompress(FlateFilter.java:172)
   at org.apache.pdfbox.filter.FlateFilter.decode(FlateFilter.java:98)
   at org.apache.pdfbox.cos.COSStream.doDecode(COSStream.java:295)
   at org.apache.pdfbox.cos.COSStream.doDecode(COSStream.java:237)
   at org.apache.pdfbox.cos.COSStream.getUnfilteredStream(COSStream.java:172)
   at org.apache.pdfbox.pdfparser.PDFXrefStreamParser.<init>(PDFXrefStreamParser.java:61)
   at org.apache.pdfbox.pdfparser.PDFParser.parseXrefStream(PDFParser.java:848)
   at org.apache.pdfbox.pdfparser.PDFParser.parseObject(PDFParser.java:576)
   at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:188)
   ...4 more

这是什么意思?第一个带有空白 pdf 的示例效果很好。

4

2 回答 2

0

使用示例生成带有文本的 PDF,然后使用相关教程阅读该文本

package com.mycompany.mavenproject;

import java.io.File;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.util.PDFTextStripper;

/**
 * Unit test for simple App.
*/
public class AppTest
    extends TestCase {

public static Test suite() {
    return new TestSuite(AppTest.class);
}

public void test() throws Exception {
    final String fileName = "PDFWithText.pdf";
    writeDocument(fileName);
    PDDocument pd = PDDocument.load(new File(fileName));
    PDFTextStripper stripper = new PDFTextStripper();
    String text = stripper.getText(pd);
    assertEquals("Hello from www.printmyfolders.com", text.trim());
}

private void writeDocument(String fileName) throws Exception {
    PDDocument doc = new PDDocument();
    PDPage page = new PDPage();

    doc.addPage(page);
    PDFont font = PDType1Font.HELVETICA_BOLD;

    PDPageContentStream content = new PDPageContentStream(doc, page);
    content.beginText();
    content.setFont(font, 12);
    content.moveTextPositionByAmount(100, 700);
    content.drawString("Hello from www.printmyfolders.com");

    content.endText();
    content.close();
    doc.save(fileName);
    doc.close();
}
}

无一例外地工作。考虑到异常从加载方法中冒出,确保 PDF 没有格式错误。

于 2013-09-27T22:43:49.500 回答
0

使用临时目录:

parser.setTempDirectory(new File(directoryPath));

例子:

File in = new File("somefile.pdf");
InputStream fin = new FileInputStream(in);
PDFParser parser = new PDFParser(fin);
parser.setTempDirectory(new File(tempDirectoryPath));
parser.parse();
PDDocument document = parser.getPDDocument();
于 2015-08-25T13:15:00.700 回答