I try to find some special chars position in pdf and then crop as new pdf. I tried some codes to find char position However it doesn't work. Founded position not seem true. It cropped different point from I want to find.
It is example code of pdfBox which I used.
@Override
protected void writeString(String string, List<TextPosition> textPositions) throws IOException {
for (TextPosition text : textPositions) {
if (text.getUnicode().equals("?")) {
System.out.println();
System.out.print("String[" + text.getXDirAdj() + "," + text.getYDirAdj() + " fs=" + text.getFontSize()
+ " xscale=" + text.getXScale() + " height=" + text.getHeightDir() + " space="
+ text.getWidthOfSpace() + " width=" + text.getWidthDirAdj() + "]" + text.getUnicode());
}
}
}
This code return something like that: String[393.169,261.75677 fs=1.0 xscale=9.0 height=6.4440002 space=2.5020003 width=5.498993]?
I suppose that 393.169, 261.75677 values should be x and y. Then I try to crop pdf with these coordinates. Like this:
File file = new File(path);
PDDocument document = PDDocument.load(file);
PDPage page = document.getPage(2);
PDRectangle a = new PDRectangle();
page.setCropBox(new PDRectangle(393, 261, 40, 50));
page.setCropBox(a);
PDDocument newDocumennt = new PDDocument();
newDocumennt.addPage(page);
newDocumennt.save(new File("..\\newFile.pdf"));
newDocumennt.close();
document.close();
But it cropped different position from founded character. Thanks for all answers.