Hello,
 
i have a problem with highlighting some text pieces in an existing pdf file. I 
wanted to create some colored Rectangles in the background of an existing pdf 
(portrait layout).
Using the Overlay class, i need to generate a seperate overlay.pdf file, which 
can be merged afterwards.
 
I have used the PDFTextStripper class to extract coordinates of some glyphs 
(TextPosition.class).
The problem is, that these coodinates do not match, when i create and write 
some rectangles to the overlay.pdf and then try to merge via the Overlay.class .
 
The rectangles seem to be shifted slightly to the left and the bottom.
 
I assume, there is some additional coordinate translation required. But i don't 
know what translations are used by the PDFTextStripper.
Transformation of the y-coordinate ( lower-left /upper-left coord system) 
cannot be sufficient, since the x coordinate is not correct either.
 
Inspecting a sample glyph from the TextStripper shows there is a textMatrix 
containing x,y-scale factors and some x,y-translation.  ==> [10, 0 , 0, 0, 10, 
0, 0, 289, 796, 1]
 
Any ideas how to transform the coords that they match the same text piece in 
the pdf?
 
 
 
Below is an except of what i was trying:
 
        String pdfin, pdfoverlay, pdfout = "...";
 
        //parse glyphs from PDF using TextStripper ... (Glyph is a wrapper of 
TextPosition.class)
        List<List<Glyph>> glyphsByPage;
        try (PDDocument pddoc = PDDocument.load(pdfin)) {
            glyphsByPage = new GlyphStripper(pddoc).stripGlyphsByPage();
        }
 
        //generate Overlayfile with Rectangles for glyphs. ...
        try (PDDocument pddoc = new PDDocument()) {
            for (int pageIndex = 0; pageIndex < glyphsByPage.size(); 
++pageIndex) {
                PDPage page = new PDPage();
                pddoc.addPage(page);
                try (PDPageContentStream contentstream = new 
PDPageContentStream(pddoc, page, true, false)) {
                    for (Glyph glyph : glyphsByPage.get(pageIndex)) {
                        TextPosition tpos = glyph.internal();
                        contentstream.setStrokingColor(Color.RED);
                        float glyphBottomY = tpos.getTextMatrix().getValue(2, 
1) - glyph.getYDirAdj();  //flipping y-coord
                        contentstream.addRect(tpos.getXDirAdj(), glyphBottomY, 
tpos.getWidth(), tpos.getHeight());
                        contentstream.closeAndStroke();
                         contentstream.setNonStrokingColor(Color.GRAY);
                        contentstream.fillRect(tpos.getXDirAdj(), glyphBottomY, 
tpos.getWidth(), tpos.getHeight());
                        contentstream.closeAndStroke();
                    }
                }
            }
            pddoc.save(pdfoverlay);
        }
 
        // Overlay src-PDF with Rectangles-File now. ...
        Overlay overlay = new Overlay();
        overlay.setInputFile(pdfin);
        overlay.setOutputFile(pdfout);
        overlay.setAllPagesOverlayFile(pdfoverlay);
        overlay.setOverlayPosition(Overlay.Position.BACKGROUND);
        overlay.overlay(new HashMap<Integer, String>(), true);

Reply via email to