pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stahle, Patrick" <patrick.sta...@te.com>
Subject Issue with PDPageContentStream setTextMatrix with PDF pages rotated
Date Thu, 18 Feb 2016 15:21:16 GMT
Hi,

I am not sure if this is a bug or is working as designed. I am applying a text stamp to existing
PDF's at a particular location on the PDF. The problem only occurs when the pdf page(s) are
rotated. I have even tried taking a PDF that was originally rotated and using PDF Editor and
rotated it back to 0, but the same issue still occurs. It seems like my Matrix I am setting
is being scaled. Here is some example code that reproduces the issue:

// Note: this is using PDFBox 2.x. Tried both lates SNAPSHOT and RC3

                                PDDocument doc = null;
                                PDFont font = PDType1Font.HELVETICA_BOLD;
                                float fontSize = 60.0F;

                                try {
                                                doc = PDDocument.load(pdfIn);
                                                PDPage page = null;
                                                for (int i = 0; i < doc.getNumberOfPages();
i++) {
                                                                page = doc.getPage(i);
                                                                logger.debug(pdfOut.getAbsolutePath()
+ " - page " + i + " rotation=" + page.getRotation() + ", mediaBox=" + page.getMediaBox());
                                                                Matrix posMatrix = new Matrix(1.0F,
0.0F, 0.0F, 1.0F, x, y);
                                                                logger.debug("Stamp matrix="
+ posMatrix);
                                                                PDPageContentStream canvas
= new PDPageContentStream(doc, page, true, true);
                                                                canvas.saveGraphicsState();
                                                                canvas.setFont(font, fontSize);
                                                                canvas.beginText();
                                                                canvas.setNonStrokingColor(Color.RED);
                                                                canvas.setTextMatrix(posMatrix);
                                                                canvas.showText(text);
                                                                canvas.endText();
                                                                canvas.restoreGraphicsState();
                                                                canvas.close();
                                                }
                                                doc.save(pdfOut);

// Output
130 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - /home/us083485/PDFBoxWork/ENG_PD_1725700-SAMT_F_1725700StampedPDFBox.pdf
- page 0 rotation=270, mediaBox=[0.0,0.0,792.0,1224.0]
130 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - Stamp matrix=[1.0,0.0,0.0,1.0,333.66,122.0]
166 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - /home/us083485/PDFBoxWork/ENG_PD_1725700-SAMT_F_1725700StampedPDFBox.pdf
- page 1 rotation=270, mediaBox=[0.0,0.0,792.0,1224.0]
166 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - Stamp matrix=[1.0,0.0,0.0,1.0,333.66,122.0]
167 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - /home/us083485/PDFBoxWork/ENG_PD_1725700-SAMT_F_1725700StampedPDFBox.pdf
- page 2 rotation=270, mediaBox=[0.0,0.0,792.0,1224.0]
167 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - Stamp matrix=[1.0,0.0,0.0,1.0,333.66,122.0]
168 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - /home/us083485/PDFBoxWork/ENG_PD_1725700-SAMT_F_1725700StampedPDFBox.pdf
- page 3 rotation=270, mediaBox=[0.0,0.0,792.0,1224.0]
168 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - Stamp matrix=[1.0,0.0,0.0,1.0,333.66,122.0]
169 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - /home/us083485/PDFBoxWork/ENG_PD_1725700-SAMT_F_1725700StampedPDFBox.pdf
- page 4 rotation=270, mediaBox=[0.0,0.0,792.0,1224.0]
169 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - Stamp matrix=[1.0,0.0,0.0,1.0,333.66,122.0]
170 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - /home/us083485/PDFBoxWork/ENG_PD_1725700-SAMT_F_1725700StampedPDFBox.pdf
- page 5 rotation=270, mediaBox=[0.0,0.0,792.0,1224.0]
170 [main] DEBUG com.tycoelectronics.emcs.stamppdf.PDFBoxExamples.StampText  - Stamp matrix=[1.0,0.0,0.0,1.0,333.66,122.0]

Now When I look at the stamped PDF the text is not in the position I would expect (x=333.66,
y=122.0), instead it is in the lower right hand corner and the font size has been scaled down
significantly. When I load it into a PDF Editor to look at it the matrix for the text has
been changed to the following:
Matrix=[0, 0.12, -0.12, 0, 1178.35999, 56.0392]
Font name and size is what it is supposed to be.

So it looks like some kind of scaling is going on. Note that the text is rotated properly,
just that the text size and position does not look correct to me. If this is working as designed
then any suggestion on how to mathematically to get the text to show up not scaled would be
appreciated. Also, I have some iText code basically doing the same thing that does not show
this behavior.

Thanks All for any help / suggestions,
Patrick









Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message