我遇到座標問題。 PDFTextStripperByArea區域似乎被推得太高。Pdfbox PDFTextStripperByArea座標移位
考慮以下示例代碼段:
...
PDPage page = (PDPage) allPages.get(0);
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
// define region for extraction -- the coordinates and dimensions are x, y, width, height
Rectangle2D.Float region = new Rectangle2D.Float(x, y, width, height);
stripper.addRegion("test region", region);
// overlay the region with a cyan rectangle to check if I got the coordinates and dimensions right
PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true);
contentStream.setNonStrokingColor(Color.CYAN);
contentStream.fillRect(x, y, width, height);
contentStream.close();
// extract the text from the defined region
stripper.extractRegions(page);
String content = stripper.getTextForRegion("test region");
...
document.save(...); ...
青色矩形很好地覆蓋所希望的區域。另一方面,脫衣舞者錯過了矩形底部的幾條線,並且在矩形上方包含了幾條線 - 它看起來像是向上移動(通過y座標)。到底是怎麼回事?
嗨,我有幾乎相同的問題:我從PDF文檔中提取塊,並希望根據其座標繪製矩形。我得到正確的寬度和高度,但正如你所提到的,最後的矩形是垂直移動的:塊的第一行不包含在矩形中。 有什麼竅門可以避免這種情況? – 2012-07-16 12:22:57
@NicolasW。 Pdfbox在猜測文本的實際位置與開始位置相反時存在問題 - 因此,如果文本在矩形外部開始並在文本內部流動,則可能無法捕獲文本。訣竅是嘗試一個稍大的捕獲區域,以捕獲在當前捕獲區域之外開始的文本。我沒有找到比這更聰明的東西。 – ipavlic 2012-07-16 12:33:09
好的,謝謝,我會看看我能做些什麼:) – 2012-07-16 12:35:52