0

I need to rotate a link rectangle using Java iText.

The original link rectangle appears in red. The rotated link rectangle appears in green.

My code:

PdfReader reader = new PdfReader( "input/blank.pdf" );
PdfStamper stamper = new PdfStamper( reader, new FileOutputStream(  "output/blank_stamped.pdf" ) );
Rectangle linkLocation = new Rectangle( 100, 700, 100 + 200, 700 + 25 );
PdfName highlight = PdfAnnotation.HIGHLIGHT_INVERT;
PdfAnnotation linkRed  = PdfAnnotation.createLink( stamper.getWriter(), linkLocation, highlight, "red" );
PdfAnnotation linkGreen = PdfAnnotation.createLink( stamper.getWriter(), linkLocation, highlight, "green" );
BaseColor baseColorRed = new BaseColor(255,0,0);
BaseColor baseColorGreen = new BaseColor(0,255,0);
linkRed.setColor(baseColorRed);
linkGreen.setColor(baseColorGreen);
double angleDegrees = 10;
double angleRadians = Math.PI*angleDegrees/180;
stamper.addAnnotation(linkRed, 1);
linkGreen.applyCTM(AffineTransform.getRotateInstance(angleRadians));
stamper.addAnnotation(linkGreen, 1);
stamper.close();

But this code does not rotate the recangle.

ZwoRmi
  • 1,093
  • 11
  • 30
  • What is the question exactly? – leigero Mar 27 '15 at 14:41
  • 1
    I'm afraid `linkGreen.applyCTM` merely applies the transformation to the lower left and the upper right corner and from the two resulting points creates a new rectangle with edges parallel to the page borders. – mkl Mar 27 '15 at 17:10
  • OK @mkl, but I still don't understand the question. This is a *link* annotation, why would you want to rotate it? Why wouldn't you just change the `linkLocation` so that the rectangle is in "portrait" rather than in "landcape"? Rotations of annotations makes sense in some case, but not in this case, doesn't it? I feel like there's something missing in the question. – Bruno Lowagie Mar 27 '15 at 18:24
  • @Bruno: Thank you for your response. Why would I want to rotate a link annotation? I have an existing PDF file with a line that represents a mechanical component. The line can be rotated at any angle. I need to stamp a link rectangle over the line. The link takes the viewer to a destination elsewhere in the PDF file. My main question is: Why does the applyCTM() method in the PdfAnnotation class not rotate the link rectangle? – Jonathan Dentch Mar 27 '15 at 19:47
  • I'll update my answer so that you stop saying stuff like "it doesn't work" ;-) – Bruno Lowagie Mar 27 '15 at 20:28

1 Answers1

0

Please take a look at the following screen shot:

enter image description here

I have added 5 annotations to a simple Hello World file.

The first two are link annotations. Their position is defined by the rectangles linkLocation1 and linkLocation2:

Rectangle linkLocation1 = new Rectangle(30, 770, 120, 800);
PdfAnnotation link1 = PdfAnnotation.createLink(stamper.getWriter(),
        linkLocation1, PdfAnnotation.HIGHLIGHT_INVERT, action);
link1.setColor(BaseColor.RED);
stamper.addAnnotation(link1, 1);
Rectangle linkLocation2 = new Rectangle(30, 670, 60, 760);
PdfAnnotation link2 = PdfAnnotation.createLink(stamper.getWriter(),
        linkLocation2, PdfAnnotation.HIGHLIGHT_INVERT, action);
 link2.setColor(BaseColor.GREEN);
 stamper.addAnnotation(link2, 1);

The green rectangle looks like a rotated version of the red rectangle, but that's not really true: we just defined the "clickable" area that way. I don't understand why you'd want to get this effect by introducing a rotation. Why? Because a rotation always needs a rotating point. Suppose that you would introduce a rotation, what would be your rotation point? The (0, 0) coordinate? That would lead to strange results, wouldn't it?

Introducing a rotation for does make sense for some types of annotations though. In my example, I introduced three stamp annotations:

Rectangle linkLocation3 = new Rectangle(150, 770, 240, 800);
PdfAnnotation stamp1 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation3, "Landscape", "Confidential");
stamper.addAnnotation(stamp1, 1);
Rectangle linkLocation4 = new Rectangle(150, 670, 240, 760);
PdfAnnotation stamp2 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation4, "Portrait", "Confidential");
stamp2.setRotate(90);
stamper.addAnnotation(stamp2, 1);
Rectangle linkLocation5 = new Rectangle(250, 670, 340, 760);
PdfAnnotation stamp3 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation5, "Portrait", "Confidential");
stamp3.setRotate(45);
stamper.addAnnotation(stamp3, 1);

In this case, I introduce a rotation angle using the setRotate() method. This rotates the CONFIDENTIAL stamp inside the rectangle we defined. As you can see, this makes sense because the annotation does have actual content: the rotation has an impact on the way you read the word CONFIDENTIAL. In the case of the clickable area of the link annotation, there is no such content to be rotated.

If this doesn't answer your question, please rephrase your question because I don't think anyone can answer it in its current state.

Update Please take a look at ISO-32000-1 aka the PDF specification. You'll discover that a rectangle is defined using 4 values: the x and y coordinate of the lower-left corner of the rectangle and the x and y coordinate of the upper-right corner of the rectangle. These are the two starting points of the horizontal and vertical sides. You want a rectangle that has sides that aren't horizontal/vertical. Obviously that isn't possible as you'd need the coordinates of 4 corner points to achieve that (8 values, not 4). You can achieve this using a polygon defined by QuadPoints. See ITextShape Clickable Polygon or path

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165