2012-07-17 212 views
2

我有一個要求,用超鏈接替換現有PDF中的某些短語。例如,「使用Java語言的一個優點是人力資源的可用性」應該被處理爲「使用the Java language的一個優點是人力資源的可用性。」PDFBox:如何從TextPosition獲取PDRectangle

爲此,我創建了一個處理器類,擴展爲PDFTextStripper。通過覆蓋processTextPosition,我能夠獲得所有字符的TextPosition。但我無法知道如何從TextPosition獲取PDRectangle,這將幫助我構建PDAnnotationLink。我嘗試了x,y屬性的組合,但我錯過了位置。需要一些幫助。謝謝。

桑傑

回答

1

我這樣做:

rectangle.setLowerLeftX(position.getTextPos().getXPosition()); 
rectangle.setLowerLeftY(position.getTextPos().getYPosition() - 2); // 2 as cushion 
rectangle.setUpperRightX(rectangle.getLowerLeftX() + position.getWidthDirAdj()); 
rectangle.setUpperRightY(rectangle.getLowerLeftY() + 12 /* 
    ideally it should be position.getHeightDir()? 
    but my sample PDF yielded a negative figure for this */);