如果我们有一个Image图像,并且坐标为x,y,则可以在不旋转的情况下绘制图像,其左下角位于给定的坐标处,如下所示

contentByte.addImage(image, image.getWidth(), 0, 0, image.getHeight(), x, y);

资源中的位图图像的大小为1×1,坐标原点位于其左下角.因此,此操作将图像拉伸到正确的大小并移动它,使其左下角位于给定的坐标处.

如果我们要绘制相同的图像,就像上面绘制的图像围绕其中心旋转了一个角度旋转一样,因此,我们可以通过移动1×1图像以使原点位于其中心来进行此操作,将其拉伸到正确的大小,旋转它,然后将原点(仍在旋转图像的中心)移动到未旋转图像的中心.使用AffineTransform实例(来自com.itextpdf.awt.geom包)而不是数字tupel,可以更轻松地表达这些操作.从而:

// Draw image as if the previous image was rotated around its center
// Image starts out being 1x1 with origin in lower left
// Move origin to center of image
AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5);
// Stretch it to its dimensions
AffineTransform B = AffineTransform.getScaleInstance(image.getWidth(), image.getHeight());
// Rotate it
AffineTransform C = AffineTransform.getRotateInstance(rotate);
// Move it to have the same center as above
AffineTransform D = AffineTransform.getTranslateInstance(x + image.getWidth()/2, y + image.getHeight()/2);
// Concatenate
AffineTransform M = (AffineTransform) A.clone();
M.preConcatenate(B);
M.preConcatenate(C);
M.preConcatenate(D);
//Draw
contentByte.addImage(image, M);
(AddRotatedImage.java测试方法testAddRotatedImage)
例如使用绘制两个图像
int x = 200;
int y = 300;
float rotate = (float) Math.PI / 3;
结果是这样的:

翻转

OP在评论中要求

how to add rotate and flip image?
为此,您只需在上面的转换序列中插入一个仿射仿射转换.
不幸的是,OP没有提到他的意思是水平翻转或垂直翻转.但是,随着改变旋转角度相应地彼此转换,这也不是必须的.
// Draw image as if the previous image was flipped and rotated around its center
// Image starts out being 1x1 with origin in lower left
// Move origin to center of image
AffineTransform A = AffineTransform.getTranslateInstance(-0.5, -0.5);
// Flip it horizontally
AffineTransform B = new AffineTransform(-1, 0, 0, 1, 0, 0);
// Stretch it to its dimensions
AffineTransform C = AffineTransform.getScaleInstance(image.getWidth(), image.getHeight());
// Rotate it
AffineTransform D = AffineTransform.getRotateInstance(rotate);
// Move it to have the same center as above
AffineTransform E = AffineTransform.getTranslateInstance(x + image.getWidth()/2, y + image.getHeight()/2);
// Concatenate
AffineTransform M = (AffineTransform) A.clone();
M.preConcatenate(B);
M.preConcatenate(C);
M.preConcatenate(D);
M.preConcatenate(E);
//Draw
contentByte.addImage(image, M);
(AddRotatedImage.java测试方法testAddRotatedFlippedImage)

与上图相同的结果:

带插值

OP在另一个评论中询问

How anti aliasing ?
iText Image类知道Interpolation属性.通过将其设置为true(显然,在将图像添加到文档之前),
image.setInterpolation(true);

低分辨率图像在绘制时会进行插值.

例如.使用具有不同颜色像素的2×2图像而不是Willi图像,您将获得以下结果,首先不进行插值,然后进行插值:

授予AddRotatedImage.java测试testAddRotatedInterpolatedImage,以添加此图像:

当心:iText Image属性Interpolation有效地设置了PDF图像字典中的Interpolate项. PDF规范在这种情况下说明:

NOTE A conforming Reader may choose to not implement this feature of PDF, or may use any specific implementation of interpolation that it wishes.

因此,在某些查看器上,插值可能与您的查看器中的插值不同,甚至根本没有.如果需要在每个查看器上使用特定种类的插值,请在将图像加载到iText图像之前,以所需的插值/抗锯齿量放大图像.