iText setRotateContents(false)在Landscape PDF上的奇怪行为? [英] iText setRotateContents(false) strange behaviour on Landscape PDF's?

查看:206
本文介绍了iText setRotateContents(false)在Landscape PDF上的奇怪行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我串联了几个PDF文件,其中一些是横向文件.我现在要在最终PDF的每一页上加盖印章.

I have concatenated several PDF files, some of which were landscape. I am now stamping each page of the final PDF.

我已经意识到,不使用setRotateContents(false)将导致您的修改以-90度添加到您期望的位置.除了我的一些风景页面,在此阶段实际上将修改正确地定位在0度.

I have realised that not using setRotateContents(false) will result in your modification being added at -90 degrees to where you expect. Except some of my landscape pages actually correctly position the modification at this stage at 0 degrees.

如果我使用setRotateContents(false),则所有这些修改都将顺时针旋转90度,这意味着我对横向页面上的大多数页面修改现在都可以正确地设置为0度,但是我的某些横向页面现在可以+90度.

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

问题是我无法弄清楚是什么导致我的页面出现这种奇怪的行为?他们俩都使用非常相似的代码来生成报告.以前有没有人见过类似的问题?

The problem is I can't work out what is causing my pages to give this strange behaviour? They both are using very similar code to generate the reports. Has anyone seen a similar problem before?

推荐答案

有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").

这篇关于iText setRotateContents(false)在Landscape PDF上的奇怪行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆