itext 7.0.2 Image顺时针旋转
Itext 7.0.2 Clockwise rotation of Image
图片元素(com.itextpdf.layout.element.Image)支持逆时针旋转。
同一张图片可以顺时针旋转吗?
PdfPage page = iTextPdfDoc.getLastPage();
PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamAfter(), page.getResources(), iTextPdfDoc);
Canvas canvas = new Canvas(pdfCanvas, iTextPdfDoc, page.getPageSize());
Image img = new Image(ImageDataFactory.create(path));
img.scaleAbsolute(525.58203, 737.0079)
img.setFixedPosition(30.785828, 34.66619)
// The following block of code does not affect the center point of rotation.
// I tried a lot of different values for rotation point. No change!
{
img.setProperty(Property.ROTATION_POINT_X, 30.785828);
img.setProperty(Property.ROTATION_POINT_Y, 34.66619);
}
img.setProperty(Property.ROTATION_ANGLE, Math.toRadians(90)); //img.setRotationAngle(Math.toRadians(90));
canvas.add(img);
更新:
这就是图像逆时针旋转 90 度时发生的情况。
这就是图像逆时针旋转 -90 或 270 度时发生的情况。
怎么样:
img.setRotationAngle(Math.toRadians(270));
您为什么要为只能用一个函数完成的事情创建两个函数来使事情复杂化?
(后面这句话是受Venkat Subramaniam今天早上在Great Indian Developer Summit的主题演讲的影响,主题演讲的题目是:"Do Not Walk Away from Complexity, Run!")
更新:
在您最初发表评论后(我也尝试使用 270。由于我不明白的原因,图像已顺时针旋转但位于 pdf 页面底部下方。), 我制作了这张图片:
在您的第二条评论中,您写道:您是对的!这意味着我必须再次设置位置,以便将图像显示到 pdf 页面中。如何移动图片,使其移动到页面底部上方?
这可能首先取决于您如何定位图像。您目前使用什么?您使用的是 setFixedPosition()
还是 setRelativePosition()
方法?或者您只是将图像添加到文档中而没有定义位置?
我发现问题是由于:
img.scaleAbsolute(525.58203, 737.0079)
该行缩放要装入容器的图像
width = 525.58203 and height = 737.0079.
下面的代码块满足了我的需要!
PdfPage page = iTextPdfDoc.getLastPage();
PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamAfter(),
page.getResources(), iTextPdfDoc);
Canvas canvas = new Canvas(pdfCanvas, iTextPdfDoc, page.getPageSize());
Image img = new Image(ImageDataFactory.create(path));
float width = img.getXObject().getWidth();
float widthContainer = 525.58203;
float heightContainer = 737.0079;
float horizontalScaling = widthContainer / width;
img.scaleAbsolute(widthContainer, heightContainer);
img.setProperty(Property.ROTATION_ANGLE, Math.toRadians(270));
img.setFixedPosition(imageLlx, imageLly + width * horizontalScaling);
canvas.add(img);
结果如下:
在页面中设置一张图片,不同的旋转角度显示相同的结果。
为示例,您可以据此设置x
, y
.
itextpdf 版本 7.0.2
public Image imageWithPosition(int pageNum, PdfPage pdfPage, ImageData imageData) {
val pageHeight = pdfPage.getPageSizeWithRotation().getHeight();
val pageWidth = pdfPage.getPageSizeWithRotation().getWidth();
Image image = new Image(imageData).scaleAbsolute(pageWidth, pageHeight);
float x = 0f;
float y = 0f;
val rotation = pdfPage.getRotation();
// rotation 180, 270 need to handle specially
if (rotation == 180) {
y = pageHeight;
} else if (rotation == 270) {
y = pageWidth;
}
// set fixed position
image.setFixedPosition(pageNum, x, y);
// toRadians
image.setRotationAngle(Math.toRadians(pdfPage.getRotation()));
return image;
}
图片元素(com.itextpdf.layout.element.Image)支持逆时针旋转。 同一张图片可以顺时针旋转吗?
PdfPage page = iTextPdfDoc.getLastPage();
PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamAfter(), page.getResources(), iTextPdfDoc);
Canvas canvas = new Canvas(pdfCanvas, iTextPdfDoc, page.getPageSize());
Image img = new Image(ImageDataFactory.create(path));
img.scaleAbsolute(525.58203, 737.0079)
img.setFixedPosition(30.785828, 34.66619)
// The following block of code does not affect the center point of rotation.
// I tried a lot of different values for rotation point. No change!
{
img.setProperty(Property.ROTATION_POINT_X, 30.785828);
img.setProperty(Property.ROTATION_POINT_Y, 34.66619);
}
img.setProperty(Property.ROTATION_ANGLE, Math.toRadians(90)); //img.setRotationAngle(Math.toRadians(90));
canvas.add(img);
更新:
这就是图像逆时针旋转 90 度时发生的情况。
这就是图像逆时针旋转 -90 或 270 度时发生的情况。
怎么样:
img.setRotationAngle(Math.toRadians(270));
您为什么要为只能用一个函数完成的事情创建两个函数来使事情复杂化?
(后面这句话是受Venkat Subramaniam今天早上在Great Indian Developer Summit的主题演讲的影响,主题演讲的题目是:"Do Not Walk Away from Complexity, Run!")
更新:
在您最初发表评论后(我也尝试使用 270。由于我不明白的原因,图像已顺时针旋转但位于 pdf 页面底部下方。), 我制作了这张图片:
在您的第二条评论中,您写道:您是对的!这意味着我必须再次设置位置,以便将图像显示到 pdf 页面中。如何移动图片,使其移动到页面底部上方?
这可能首先取决于您如何定位图像。您目前使用什么?您使用的是 setFixedPosition()
还是 setRelativePosition()
方法?或者您只是将图像添加到文档中而没有定义位置?
我发现问题是由于:
img.scaleAbsolute(525.58203, 737.0079)
该行缩放要装入容器的图像
width = 525.58203 and height = 737.0079.
下面的代码块满足了我的需要!
PdfPage page = iTextPdfDoc.getLastPage();
PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamAfter(),
page.getResources(), iTextPdfDoc);
Canvas canvas = new Canvas(pdfCanvas, iTextPdfDoc, page.getPageSize());
Image img = new Image(ImageDataFactory.create(path));
float width = img.getXObject().getWidth();
float widthContainer = 525.58203;
float heightContainer = 737.0079;
float horizontalScaling = widthContainer / width;
img.scaleAbsolute(widthContainer, heightContainer);
img.setProperty(Property.ROTATION_ANGLE, Math.toRadians(270));
img.setFixedPosition(imageLlx, imageLly + width * horizontalScaling);
canvas.add(img);
结果如下:
在页面中设置一张图片,不同的旋转角度显示相同的结果。
为示例,您可以据此设置x
, y
.
itextpdf 版本 7.0.2
public Image imageWithPosition(int pageNum, PdfPage pdfPage, ImageData imageData) {
val pageHeight = pdfPage.getPageSizeWithRotation().getHeight();
val pageWidth = pdfPage.getPageSizeWithRotation().getWidth();
Image image = new Image(imageData).scaleAbsolute(pageWidth, pageHeight);
float x = 0f;
float y = 0f;
val rotation = pdfPage.getRotation();
// rotation 180, 270 need to handle specially
if (rotation == 180) {
y = pageHeight;
} else if (rotation == 270) {
y = pageWidth;
}
// set fixed position
image.setFixedPosition(pageNum, x, y);
// toRadians
image.setRotationAngle(Math.toRadians(pdfPage.getRotation()));
return image;
}