In PDFBox, how to change the origin (0,0) point of a PDRectangle object?

The Situation: In PDFBox, PDRectangle objects' default origin (0,0) seems to be the lower-left corner of a page.

For example, the following code gives you a square at the lower-left corner of a page, and each side is 100 units long.

PDRectangle rectangle = new PDRectangle(0, 0, 100, 100);

The Question: Is it possible to change the origin to the UPPER-LEFT corner, so that, for example, the code above will give you the same square at the UPPER-LEFT corner of the page instead?

The reason I ask: I was using PDFTextStripper to get the coordinates of the text (by using the getX() and getY() methods of the extracted TextPosition objects). The coordinates retrieved from TextPosition objects seem have an origin (0,0) at the UPPER-LEFT CORNER. I want the coordinates of my PDRectangle objects have the same origin as the coordinates of my TextPosition objects.

I have tried to adjust the Y-coordinates of my PDRectangle by "page height minus Y-coordinate". This gives me the desired result, but it's not elegant. I want an elegant solution.

Note: Someone has asked a similar question. The answer is what I tried, which is not the most elegant. how to change the coordiantes of a text in a pdf page from lower left to upper left

You can change coordinate systems somewhat but most likely things won't get more elegant in the end.

To start with...

First of all let's clear up some misconception:

You assume

In PDFBox, PDRectangle objects' default origin (0,0) seems to be the lower-left corner of a page.

This is not true for all cases, merely often.

The area containing the displayed page area (on paper or on screen) usually is defined by the CropBox entry of the page in question:

CropBox rectangle (Optional; inheritable) A rectangle, expressed in default user space units, that shall define the visible region of default user space. When the page is displayed or printed, its contents shall be clipped (cropped) to this rectangle and then shall be imposed on the output medium in some implementation-defined manner.

... The positive x axis extends horizontally to the right and the positive y axis vertically upward, as in standard mathematical practice (subject to alteration by the Rotate entry in the page dictionary).

... In PostScript, the origin of default user space always corresponds to the lower-left corner of the output medium. While this convention is common in PDF documents as well, it is not required; the page dictionary’s CropBox entry can specify any rectangle of default user space to be made visible on the medium.

Thus, the origin (0,0) can literally be anywhere, it may be at the lower left, at the upper left, in the middle of the page or even far outside the displayed page area.

And by means of the Rotate entry, that area can even be rotated (by 90°, 180°, or 270°).

Putting the origin (as you seem to have observed) in the lower left merely is done by convention.

Furthermore you seem to think that the coordinate system is constant. This also is not the case, there are operations by which you can transform the user space coordinate system drastically, you can translate, rotate, mirror, skew, and/or scale it!

Thus, even if at the beginning the coordinate system is the usual one, origin in lower left, x-axis going right, y-axis going up, it may be changed to something weird some way into the page content description. Drawing your rectangle new PDRectangle(0, 0, 100, 100) there might produce some rhomboid form just right of the page center.

What you can do...

As you see coordinates in PDF user space are a very dynamic matter. what you can do to tame the situation, depends on the context you use your rectangle in.

Unfortunately you were quite vague in the description of what you do. Thus, this will be somewhat vague, too.

Coordinates in the page content

If you want to draw some rectangle on an existing page, you first of all need a page content stream to write to, i.e. a PDPageContentStream instance, and it should be prepared in a manner guaranteeing that the original user space coordinate system has not been disturbed. You get such an instance by using the constructor with three boolean arguments setting all them to true:

PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true, true);

Then you can apply a transformation to the coordinate system. You want the top left to be the origin and the y-value increasing downwards. If the crop box of the page tells you the top left has coordinates (xtl, ytl), therefore, you apply

contentStream.concatenate2CTM(new AffineTransform(1, 0, 0, -1, xtl, ytl));

and from here on you have a coordinate system you wanted, origin top left and y coordinates mirrored.

Be aware of one thing, though: If you are going to draw text, too, not only the text insertion point y coordinate is mirrored but also the text itself unless you counteract that by adding an also mirroring text matrix! If you want to add much text, therefore, this may not be as elegant as you want.

Coordinates for annotations

If you don't want to use the rectangle in the content stream but instead for adding annotations, you are not subject to the transformations mentioned above but you can not make use of it, either.

Thus, in this context you have to take the crop box as it is and transform your rectangle accordingly.

Why PDFBox text extraction coordinates are as they are

Essentially for putting lines of text together in the right order and sorting the lines correctly, you don't want such a weird situation but instead a simple stable coordinate system. Some PDFBox developers chose the top-left-origin, y-increasing-downwards variant for that, and so the TextPosition coordinates have been normalized to that scheme.

In my opinion a better choice would have been to use the default user space coordinates for easier re-use of the coordinates. You might, therefore, want to try working with textPosition.getTextMatrix().getTranslateX(), textPosition.getTextMatrix().getTranslateY() for a TextPosition textPosition

PDRectangle (Apache PDFBox 1.8.13 API), public class PDRectangle extends Object implements COSObjectable. This represents a rectangle Method to determine if the x/y point is inside this rectangle. Dimension This will move the rectangle the given relative amount. Initializes to 0,0,0,0 Returns: A new rectangle that has been translated back to the origin. This will get the width of this rectangle as calculated by upperRightX - lowerLeftX.

The following seems to be the best way to "adjust" the TextPosition coordinates:

x_adjusted =  x_original + page.findCropBox().getLowerLeftX();
y_adjusted = -y_original + page.findCropBox().getUpperRightY();

where page is the PDPage on which the TextPosition object is located

PDRectangle (PDFBox reactor 2.0.11 API), public class PDRectangle extends Object implements COSObjectable Method to determine if the x/y point is inside this rectangle. this rectangle, such that the new rectangle retains the same dimensions(height/width), but the lower left x,y values are zero. This will set the upper right y coordinate. Initializes to 0,0,0,0​  This will get the width of this rectangle as calculated by upperRightX - lowerLeftX.

Add the height of the PDF (Easiest Solution)

Draw image at mid position using pdfbox Java, I'm trying to add an image to the center of pdf using pdfbox. how to change the origin (0,0) point of a PDRectangle object? to get the correct  The following are top voted examples for showing how to use org.apache.pdfbox.pdmodel.common.PDRectangle.These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

org.apache.pdfbox > pdfbox > 2.0.14 > org.apache.pdfbox.pdmodel , package org.apache.pdfbox.pdmodel.common; import java.awt.geom.​GeneralPath Initializes to 0,0,0,0 */ public PDRectangle() { this(0.0f, 0.0f, 0.0f, 0.0f); } /** * Constructor. @return A new rectangle that has been translated back to the origin. public void setLowerLeftX(float value) { rectArray.set(0, new COSFloat( value )  org.pdfbox.cos.COSName: Object: A wrapper to any of the other objects, this can be used to reference an object multiple times. An object is referenced by using two numbers, an object number an a generation number. Initially the generation number will be zero unless the object get replaced later in the stream. 12 0 obj << /Type /XObject >> endobj

org.apache.pdfbox.pdmodel.common: PDRectangle.java, 40 * 41 * Initializes to 0,0,0,0 42 */ 43 public PDRectangle() 44 { 45 rectArray = new array; 89 } 90 91 /** 92 * Method to determine if the x/y point is inside this rectangle. 114 * @return A new rectangle that has been translated back to the origin. floatValue(); 142 } 143 144 /** 145 * This will set the lower left x coordinate. In PDFBox, how to change the origin (0,0) point of a PDRectangle object? java,pdf,pdfbox. You can change coordinate systems somewhat but most likely things won't get more elegant in the end. To start with

pdfbox/PDRectangle.java at master · xiaojiahuo/pdfbox · GitHub, Contribute to xiaojiahuo/pdfbox development by creating an account on GitHub. /src/main/java/org/apache/pdfbox/pdmodel/common/PDRectangle.java / Jump to Method to determine if the x/y point is inside this rectangle. rectArray.set(0, new COSFloat( value ) ); Convert this standard java object to a COS object. java.lang.Object org.pdfbox.pdmodel.common.PDRectangle will be translated to 0,0,300,300 Returns: A new rectangle that has been translated back to the origin.

Comments
  • Thanks for the detailed response. Yes, I am indeed trying to add a link annotation, so the concatenate2CTM method is not applicable. When you said "you have to take the crop box as it is and transform your rectangle accordingly", what does that mean? Does that mean I need to transform the cropbox rectangle, or I need to transform the rectangle I am trying to draw?
  • what does that mean? - the rectangle you draw. If you changed the cropbox, you'd move all the page content. Or you retrieve your coordinates differently to begin with, c.f. the last paragraph of the answer.
  • Thanks for the detailed response.
  • I think there is a typo in your answer. You wrote You want the top left to be the origin and the x-value increasing downwards. I think you meant y-value increasing downward, not x-value.
  • That looks correct in case of pages without Rotation.
  • Nope. Consider the OP's example rectangle, by adding the height of the page you move it out of the page while he wants it in the upper left corner inside the page.
  • If you add Page height it will not go out of the PDF, it just become inverted :)
  • "it just become inverted" - that's incorrect. I assume by adding the height of the page to the OP's new PDRectangle(0, 0, 100, 100) you mean adding the height to the base point, i.e. new PDRectangle(0, pageHeight, 100, 100). This rectangle clearly is out of the page and not inverted. If you mean something different, your answer clearly lacks detail.