IText setRotateContents (false) weird behavior in landscape PDF files?

I combined several PDF files, some of which were landscapes. Now I print every page of the final PDF.

I realized that not using setRotateContents(false) would add your -90 degree modification to where you expect it to. Except for some of my landscape pages, it’s actually correct to position the modification at this stage at 0 degrees.

If I use setRotateContents(false) , then all these modifications are rotated clockwise 90 degrees, which means that most of the modifications to my page on landscape pages are now correct at 0 degrees, but some of my landscape pages are now +90 degrees.

The problem is that I can’t understand what makes my pages set this strange behavior? Both of them use very similar code to generate reports. Has anyone seen a similar problem before?

+3
source share
1 answer

There are 3 different ways to make a landscape page, and all three are used (I use 8.5x11 as the basis here, YMMV):

  • 11x8.5 (these are pages that "look right."
  • 8.5x11 90 clockwise
  • 8.5x11 90 counterclockwise

So you need to check the rotation of the page.

 int rot = PdfReader.getPageRotation(pageNum); 

If you want your brand to come out β€œexactly”, you need to convert it based on this rotation. The main conversion looks something like this:

 cos(rot), sin(rot), -sin(rot), cos(rot), xoff, yoff 

The rotation occurs around the origin , 0,0. You need xoff and yoff to return the stamp to where you want. Since you can combine conversions, one common trick:

  • Move the object so that it is centered at the beginning.
  • Rotate
  • Bring him back.

When you combine these three separate transforms in the correct order, you get one transform that does exactly what you want.

Or you could trick and use AffineTransform.getRotateInstance( theta, centerX, centerY) , but where is the fun? PdfContentByte.transform(AffineTransform) will suck the array for you, but all the elements are in the correct order to just pass them to any of the byte content functions that accepts the conversion as six floats (the parameters are usually called "a, b, c, d , h, v ").

+2
source

All Articles