pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Samuel Sandläufer" <sml...@gmx.de>
Subject Coordinate translation problem using overlayPDF
Date Mon, 22 Dec 2014 17:10:42 GMT
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
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();
                try (PDPageContentStream contentstream = new PDPageContentStream(pddoc,
page, true, false)) {
                    for (Glyph glyph : glyphsByPage.get(pageIndex)) {
                        TextPosition tpos = glyph.internal();
                        float glyphBottomY = tpos.getTextMatrix().getValue(2,
1) - glyph.getYDirAdj();  //flipping y-coord
                        contentstream.addRect(tpos.getXDirAdj(), glyphBottomY,
tpos.getWidth(), tpos.getHeight());
                        contentstream.fillRect(tpos.getXDirAdj(), glyphBottomY,
tpos.getWidth(), tpos.getHeight());
        // Overlay src-PDF with Rectangles-File now. ...
        Overlay overlay = new Overlay();
        overlay.overlay(new HashMap<Integer, String>(), true);

View raw message