我正在尝试使用PDFBox 2.0进行文本提取。我想获得有关特定字符的字体大小和页面上该字符的位置矩形的信息。 我使用PDFTextStripper在PDFBox的1.6实现了这个:使用PDFBox 2.0从PDF中提取文本
PDFParser parser = new PDFParser(is);
try{
parser.parse();
}catch(IOException e){
}
COSDocument cosDoc = parser.getDocument();
PDDocument pdd = new PDDocument(cosDoc);
final StringBuffer extractedText = new StringBuffer();
PDFTextStripper textStripper = new PDFTextStripper(){
@Override
protected void processTextPosition(TextPosition text) {
extractedText.append(text.getCharacter());
logger.debug("text position: "+text.toString());
}
};
textStripper.setSuppressDuplicateOverlappingText(false);
for(int pageNum = 0;pageNum<pdd.getNumberOfPages();pageNum++){
PDPage page = (PDPage) pdd.getDocumentCatalog().getAllPages().get(pageNum);
textStripper.processStream(page, page.findResources(), page.getContents().getStream());
}
pdd.close();
但是在2.0版本PDFBox的中,processStream
方法已被删除。 我怎样才能达到与PDFBox 2.0相同?
我已经试过如下:
PDDocument pdd = PDDocument.load(inputStream);
PDFTextStripper textStripper = new PDFTextStripper(){
@Override
protected void processTextPosition(TextPosition text){
int pos = PDFdocument.length();
String textadded = text.getUnicode();
Range range = new Range(pos,pos+textadded.length());
int pagenr = this.getCurrentPageNo();
Rectangle2D rect = new Rectangle2D.Float(text.getX(),text.getY(),text.getWidth(),text.getHeight());
}
};
textStripper.setSuppressDuplicateOverlappingText(false);
for(int pageNum = 0;pageNum<pdd.getNumberOfPages();pageNum++){
PDPage page = (PDPage) pdd.getDocumentCatalog().getPages().get(pageNum);
textStripper.processPage(page);
}
pdd.close();
的processTextPosition(TextPosition text)
方法不会被调用。 任何建议将非常受欢迎。
P请看源代码中的DrawPrintTextLocations示例,这就是您显然想要做的。它使用writeString()调用。 –
谢谢,那个例子完全是我在找的东西。 –