How to search some specific string or a word and there coordinates from a pdf document in java

Viewed 12970

I am using Pdfbox to search a word(or String) from a pdf file and I also want to know the coordinates of that word. For example :- in a pdf file there is a string like "${abc}". I want to know the coordinates of this string. I Tried some couple of examples but didn't get the result according to me. in result it is displaying the coordinates of character.

Here is the Code

@Override
protected void writeString(String string, List<TextPosition> textPositions) throws IOException {
    for(TextPosition text : textPositions) {


        System.out.println( "String[" + text.getXDirAdj() + "," +
                text.getYDirAdj() + " fs=" + text.getFontSize() + " xscale=" +
                text.getXScale() + " height=" + text.getHeightDir() + " space=" +
                text.getWidthOfSpace() + " width=" +
                text.getWidthDirAdj() + "]" + text.getUnicode());

    }
}

I am using pdfbox 2.0

4 Answers

I was looking for highlighting different words in a PDF file. For doing this, I need to know properly the word coordinates, so what I'm doing is getting the (x, y) coordinate from the top-left, from the first letter, and the (x, y) coordinate from the last letter from the top-right.

Later on, save the points in one array. Keep in mind that for getting properly the y coordinate you need the relative position in respect to the page size, because of the coordinate given. But the getYDirAdj() method is absolute and lots of time does not match with the one in the page.

protected void writeString(String string, List<TextPosition> textPositions) throws IOException {
    boolean isFound = false;
    float posXInit  = 0, 
          posXEnd   = 0, 
          posYInit  = 0,
          posYEnd   = 0,
          width     = 0, 
          height    = 0, 
          fontHeight = 0;
    String[] criteria = {"Word1", "Word2", "Word3", ....};

    for (int i = 0; i < criteria.length; i++) {
        if (string.contains(criteria[i])) {
            isFound = true;
        } 
    }
    if (isFound) {
        posXInit = textPositions.get(0).getXDirAdj();
        posXEnd  = textPositions.get(textPositions.size() - 1).getXDirAdj() + textPositions.get(textPositions.size() - 1).getWidth();
        posYInit = textPositions.get(0).getPageHeight() - textPositions.get(0).getYDirAdj();
        posYEnd  = textPositions.get(0).getPageHeight() - textPositions.get(textPositions.size() - 1).getYDirAdj();
        width    = textPositions.get(0).getWidthDirAdj();
        height   = textPositions.get(0).getHeightDir();

        System.out.println(string + "X-Init = " + posXInit + "; Y-Init = " + posYInit + "; X-End = " + posXEnd + "; Y-End = " + posYEnd + "; Font-Height = " + fontHeight);

        float quadPoints[] = {posXInit, posYEnd + height + 2, posXEnd, posYEnd + height + 2, posXInit, posYInit - 2, posXEnd, posYEnd - 2};

        List<PDAnnotation> annotations = document.getPage(this.getCurrentPageNo() - 1).getAnnotations();
        PDAnnotationTextMarkup highlight = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);

        PDRectangle position = new PDRectangle();
        position.setLowerLeftX(posXInit);
        position.setLowerLeftY(posYEnd);
        position.setUpperRightX(posXEnd);
        position.setUpperRightY(posYEnd + height);

        highlight.setRectangle(position);

        // quadPoints is array of x,y coordinates in Z-like order (top-left, top-right, bottom-left,bottom-right) 
        // of the area to be highlighted

        highlight.setQuadPoints(quadPoints);

        PDColor yellow = new PDColor(new float[]{1, 1, 1 / 255F}, PDDeviceRGB.INSTANCE);
        highlight.setColor(yellow);
        annotations.add(highlight);
    }
}

you can try this

@Override
protected void writeString(String str, List<TextPosition> textPositions) throws IOException {
    TextPosition startPos = textPositions.get(0);
    TextPosition endPos = textPositions.get(textPositions.size() - 1);

    System.out.println(str + " [(" + startPos.getXDirAdj() + "," + startPos.getYDirAdj() + ") ,("
            + endPos.getXDirAdj() + "," + endPos.getYDirAdj() + ")]");

}

Output will look something like this 'String [(54.0,746.08) ,(99.71,746.08)]'

Related