0

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.

MRK
  • 1
  • 1
  • `get?DirAdj` gives you coordinates in a custom coordinate system PDFBox uses just for text extraction. More normal coordinates you get using `getTextMatrix().getTranslate?()`. But even then you might get into troubles, see e.g. [this answer](https://stackoverflow.com/a/46113333/1729265). – mkl Jun 09 '21 at 17:34
  • @mkl thanks for answer. These methods solved my problem. – MRK Jun 09 '21 at 18:15
  • OK. I'll close your message as duplicate of the question i referred to. – mkl Jun 09 '21 at 19:33

0 Answers0