且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iText setRotateContents(false)在Landscape PDF上的奇怪行为?

更新时间:2021-10-01 01:47:50

有3种不同的方法可以制作风景页面,并且全部使用了三种(我在这里以8.5x11为基础,YMMV):>

There are 3 different ways to do a landscape page, and all three are used (I'm using 8.5x11 as a basis here, YMMV):

  1. 11x8.5(这些页面看起来正确".
  2. 8.5x11 90顺时针
  3. 8.5x11 90逆时针

因此,您需要检查页面的旋转情况.

So you need to check the page's rotation.

int rot = PdfReader.getPageRotation(pageNum);

如果您想让印章呈现偶数",则需要基于该旋转对其进行变换.基本的转换是这样的:

If you want your stamp to come out "even" you then need to transform it based on that rotation. The basic transform goes something like this:

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

旋转围绕原点,即0,0.您需要xoffyoff才能将图章移回所需位置.因为可以组合转换,所以一个常见的技巧是:

Rotation takes place around the origin, 0,0. You need xoff and yoff to move the stamp back to where you want it. Because you can combine transformations, one common trick is:

  1. 移动对象,使其以原点为中心.
  2. 旋转
  3. 将其移回去.

以适当的顺序组合这三个单独的转换时,最终会得到一个可以满足您需要的转换.

When you combine those three individual transformations in the proper order, you end up with a single transform that does just what you want.

或者您可以作弊并使用AffineTransform.getRotateInstance( theta, centerX, centerY),但是这样做的乐趣在哪里? PdfContentByte.transform(AffineTransform)会为您吸取数组,但是所有元素都以正确的顺序简单地将它们传递给接受转换为六个浮点数的任何内容字节函数(这些参数通常称为"a,b之类的东西,c,d,h,v").

Or you could cheat and use AffineTransform.getRotateInstance( theta, centerX, centerY), but where's the fun in that? PdfContentByte.transform(AffineTransform) will suck the array out for you, but all the elements are in the proper order to simply pass them to any of the content byte functions that accepts a transformation as six floats (the parameters are usually called something like "a, b, c, d, h, v").