4

UPDATE:

The question below is actually very similar to this question on SO which has been perfectly answered by mkl. Short answer: Either live with a bottom-left origin or translate your coordinates to these. There is a workaround but that's a mess.

ORIGINAL QUESTION:

I want to create a PDF with coordinates 0,0 in the upper left and 210, 297 in the bottom right. Approaches with calculations like y = 297 - y seem to be a bit messy.

This question is similar to this question on SO but refers to PdfBox 2 where the the provided solution doesn't work.

The docs say I should use a Matrix to transform the coordinatesystem. I tried this approach:

    PDRectangle currentPageRectangle = new PDRectangle(210 * POINTS_PER_MM, 297 * POINTS_PER_MM);
    PDPage currentPage = new PDPage(currentPageRectangle);
    currentDocument.addPage(currentPage);
    contentStream = new PDPageContentStream(currentDocument, currentPage);

    AffineTransform a = new AffineTransform(1, 0, 0, -1, 210 * POINTS_PER_MM, 297 * POINTS_PER_MM);
    Matrix m = new Matrix(a);
    contentStream.transform(m);

Unfortunately the page stays blank. Any clues?

Kai
  • 871
  • 2
  • 18
  • 39
  • did you close the page content stream? And why the x transformation? – Tilman Hausherr Jun 23 '17 at 17:45
  • I did. Current full code produces a page with text and images with the origin bottom left. I'd like to translate that code to topleft coordinates. Not sure about your question regarding x transform. – Kai Jun 23 '17 at 18:21
  • replace the x translation with 0. No x translation is needed, as the x scale is unchanged. – Tilman Hausherr Jun 23 '17 at 18:23
  • Guessing it's one of the Constructor arguments of AffineTransform class. Right? – Kai Jun 23 '17 at 18:27
  • Yes. The x translation (= move) is the second to last parameter. I.e. try `new AffineTransform(1, 0, 0, -1, 0, 297 * POINTS_PER_MM);` – Tilman Hausherr Jun 23 '17 at 18:28
  • Tried that - page doesn't stay blank but the result is not as desired. The text seems to be mirrored on the horizontal axis. – Kai Jun 23 '17 at 19:43
  • Aaargh, sorry. I see mkl warned about just that in his answer. I don't have any good idea now, except rotating again. – Tilman Hausherr Jun 23 '17 at 20:19
  • 2
    Maybe I should go with that simple calculation mentioned in the question. Seams to be a lot easier now. Thank you for help Tilman. – Kai Jun 24 '17 at 14:43
  • 3
    *"Approaches with calculations like y = 297 - y seem to be a bit messy."* - in my opinion they are the *least messy* solution. The warning from the answer you referred to, i.e. that in particular text becomes mirrored unless you counteract that by an appropriate mirroring in the text matrix, remains valid, no matter which PDFBox version you use. That been said, the really optimal solution would be an application which from the start gets along with standard coordinate systems (**y** value rising upwards). – mkl Jun 25 '17 at 13:29

1 Answers1

1

Your Affinetransform is not correct, see Javadoc. The last two parameters are translation values, now you translate the page really outside of the physical page because you translate x coordinates wrong.

This code works better (I used height as a placeholder for your page height):

        contentStream = new PDPageContentStream(document, page);
        contentStream.transform(new Matrix(new java.awt.geom.AffineTransform(1, 0, 0, -1, 0,height)));

Also consider getting the actual page height instead of using hardcoded values, this way your code works on any page dimensions:

float height=page.getMediaBox().getHeight();
Gee Bee
  • 1,794
  • 15
  • 17