java - 学习PDFBox;示例代码有问题

标签 java ios apache import

我正在尝试学习如何使用 PDFBox 并找到了一些我正在使用的示例代码 here .

我已在后脚本中附加了代码。

当我在Dr. Java中编译代码时,出现以下错误:

File: C:\Users\Dick Hurtz from Hold\Desktop\Java Programs\JavaStuff\PDFManager.java  [line: 30]
Error: The constructor org.apache.pdfbox.pdfparser.PDFParser(org.apache.pdfbox.io.RandomAccessFile) is undefined

我不知道该怎么办,任何帮助将不胜感激。感谢大家!

以下是类(class):

主要:

import java.io.IOException;

public class JavaPDFTest {

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

   PDFManager pdfManager = new PDFManager();
   pdfManager.setFilePath("E:\test.pdf");
   System.out.println(pdfManager.ToText());       

}    
}

PDFManager:

import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class PDFManager {

private PDFParser parser;
private PDFTextStripper pdfStripper;
private PDDocument pdDoc;
private COSDocument cosDoc;

private String Text;
private String filePath;
private File file;

public PDFManager() {

}
public String ToText() throws IOException
{
   this.pdfStripper = null;
   this.pdDoc = null;
   this.cosDoc = null;

   file = new File(filePath);
   parser = new PDFParser(new RandomAccessFile(file,"r")); // update for      PDFBox V 2.0

   parser.parse();
   cosDoc = parser.getDocument();
   pdfStripper = new PDFTextStripper();
   pdDoc = new PDDocument(cosDoc);
   pdDoc.getNumberOfPages();
   pdfStripper.setStartPage(1);
   pdfStripper.setEndPage(10);

   // reading text from page 1 to 10
   // if you want to get text from full pdf file use this code
   // pdfStripper.setEndPage(pdDoc.getNumberOfPages());

   Text = pdfStripper.getText(pdDoc);
   return Text;
}

public void setFilePath(String filePath) {
    this.filePath = filePath;
}

}

最佳答案

直接使用获取PDDocument

PDDocument pdDoc = PDDocument.load(file);

是从文件加载 PDF 文档的推荐方法。

关于java - 学习PDFBox;示例代码有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37649831/

相关文章:

wordpress - 如何使用我的 Ubuntu 12.04 服务器暂时禁用 Wordpress 站点并启用另一个站点?

java - 实例化二叉树中的根的问题

ios - UICollectionView 弹回后刷新时消失

ios - 在 iOS 中以编程方式更改应用程序中的语言

java - 如何从apache poi读取word文档中的注释?

php - 在我的 linux 服务器上,我收到一个 php 错误,但我看不到错误,为什么?

java - 如何关闭所有由 selenium 打开的空闲网络浏览器?

java - protobuf 消息中的自引用字段

java - Maven Central Repository 中是否可能缺少 jar?

ios - 在iphone App中播放背景视频