我有一个简单的带有属性/Rotate 90
的A4 pdf文档:我的pdf的原始版本是横向的,但打印了肖像。
我试图在肖像文档的左下角绘制一个小图像。
到目前为止,我的代码如下:
File file = new File("rotated90.pdf");
try (final PDDocument doc = PDDocument.load(file)) {
PDPage page = doc.getPage(0);
PDImageXObject image = PDImageXObject.createFromFile("image.jpg", doc);
PDPageContentStream contents = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, false, true);
contents.drawImage(image, 0, 0);
contents.close();
doc.save(new File("newpdf.pdf"));
}
这是最终结果:正如您所看到的,图像被放置在左上角(这是旋转前的0,0坐标),并且没有旋转。
我试着玩drawImage(PDImageXObject image, Matrix matrix)
,但没有成功。
这是原始文档pdf with 90° rotation
发布于 2020-08-27 16:23:25
对于旋转90度的页面,这里有一个解决方案:
PDPageContentStream cs = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true);
PDImageXObject image = ....
cs.saveGraphicsState();
cs.transform(Matrix.getRotateInstance(Math.toRadians(90), page.getCropBox().getWidth() + page.getCropBox().getLowerLeftX(), 0));
cs.drawImage(image, 0, 0);
cs.restoreGraphicsState();
cs.close();
如果只是映像,则不需要保存/恢复。
将页面旋转270°后的解决方案:
cs.transform(Matrix.getRotateInstance(Math.toRadians(270), 0, page.getCropBox().getHeight() + page.getCropBox().getLowerLeftY()));
对于180°:
cs.transform(Matrix.getRotateInstance(Math.toRadians(180), page.getCropBox().getWidth() + page.getCropBox().getLowerLeftX(), page.getCropBox().getHeight() + page.getCropBox().getLowerLeftY()));
https://stackoverflow.com/questions/63597316
复制相似问题