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();
But in the 2.0 version of PDFBox, the processStream
method has been removed.
How can I achieve the same with PDFBox 2.0?
I've tried the following:
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();
The processTextPosition(TextPosition text)
method does not get called.
Any suggestions would be very welcome.