问题描述
我正在尝试使用PDFBox 2.0进行文本提取。我想获得有关特定字符的字体大小和页面上该字符的位置矩形的信息。
我在PDFBox 1.6中使用PDFTextStripper实现了这个:
I'm trying to use PDFBox 2.0 for text extraction. I would like to get information on the font size of specific characters and the position rectangle of that character on the page.I've implemented this in PDFBox 1.6 using a PDFTextStripper:
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实现相同目标?
But in the 2.0 version of PDFBox, the processStream
method has been removed.How can I achieve the same with 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)
方法不被打电话。
任何建议都会非常受欢迎。
The processTextPosition(TextPosition text)
method does not get called.Any suggestions would be very welcome.
推荐答案
为我的问题提供了解决方案。
The DrawPrintTextLocations
example, suggested by @tilmanhausherr, provided the solution to my problem.
使用以下代码启动解析器( inputStream
是来自PDF文件URL的输入流):
The parser is started using the following code (the inputStream
is the input stream from the URL of the PDF file):
PDDocument pdd = null;
try {
pdd = PDDocument.load(inputStream);
PDFParserTextStripper stripper = new PDFParserTextStripper(PDFdocument,pdd);
stripper.setSortByPosition(true);
for (int i=0;i<pdd.getNumberOfPages();i++){
stripper.stripPage(i);
}
} catch (IOException e) {
// throw error
} finally {
if (pdd!=null) {
try {
pdd.close();
} catch (IOException e) {
}
}
}
此代码使用自定义子类 PDFTextStripper
:
This code uses a custom subclass of PDFTextStripper
:
class PDFParserTextStripper extends PDFTextStripper {
public PDFParserTextStripper() throws IOException {
super();
}
public void stripPage(int pageNr) throws IOException {
this.setStartPage(pageNr+1);
this.setEndPage(pageNr+1);
Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
writeText(document,dummy); // This call starts the parsing process and calls writeString repeatedly.
}
@Override
protected void writeString(String string,List<TextPosition> textPositions) throws IOException {
for (TextPosition text : textPositions) {
System.out.println("String[" + text.getXDirAdj()+","+text.getYDirAdj()+" fs="+text.getFontSizeInPt()+" xscale="+text.getXScale()+" height="+text.getHeightDir()+" space="+text.getWidthOfSpace()+" width="+text.getWidthDirAdj()+" ] "+text.getUnicode());
}
}
}
这篇关于使用PDFBox 2.0从PDF中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!