Pdfbox:在旋转页面中绘制图像

huangapple go评论97阅读模式
英文:

Pdfbox : Draw image in rotated page

问题

我有一个简单的A4 PDF文档,其中有一个属性 /Rotate 90 :我的原始PDF版本是横向的,但打印时是纵向的。

我正在尝试在纵向文档的左下方绘制一个小图像。

以下是我目前的代码:

  1. File file = new File("rotated90.pdf");
  2. try (final PDDocument doc = PDDocument.load(file)) {
  3. PDPage page = doc.getPage(0);
  4. PDImageXObject image = PDImageXObject.createFromFile("image.jpg", doc);
  5. PDPageContentStream contents = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, false, true);
  6. contents.drawImage(image, 0, 0);
  7. contents.close();
  8. doc.save(new File("newpdf.pdf"));
  9. }

以下是最终结果:正如您所看到的,图像被放置在左上角(这是旋转之前的 0,0 坐标),并且没有进行旋转。

我尝试使用 drawImage(PDImageXObject image, Matrix matrix) 进行操作,但没有成功。

以下是原始文档 pdf with 90° rotation

英文:

I have a simple A4 pdf document with a property /Rotate 90 : The original version of my pdf is landscape but printed portrait.

I am trying to draw a small image at the bottom left of the portait document.

Here is my code so far :

  1. File file = new File("rotated90.pdf");
  2. try (final PDDocument doc = PDDocument.load(file)) {
  3. PDPage page = doc.getPage(0);
  4. PDImageXObject image = PDImageXObject.createFromFile("image.jpg", doc);
  5. PDPageContentStream contents = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, false, true);
  6. contents.drawImage(image, 0, 0);
  7. contents.close();
  8. doc.save(new File("newpdf.pdf"));
  9. }

Here is the end result : As you can see the image was placed at the top left (which was the 0,0 coordinate before rotation) and was not rotated.

Pdfbox:在旋转页面中绘制图像

I tried playing with drawImage(PDImageXObject image, Matrix matrix) without success.

Here is the orignal document pdf with 90° rotation

答案1

得分: 5

这是一个针对页面旋转90度的解决方案:

  1. PDPageContentStream cs = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true);
  2. PDImageXObject image = ....
  3. cs.saveGraphicsState();
  4. cs.transform(Matrix.getRotateInstance(Math.toRadians(90), page.getCropBox().getWidth() + page.getCropBox().getLowerLeftX(), 0));
  5. cs.drawImage(image, 0, 0);
  6. cs.restoreGraphicsState();
  7. cs.close();

如果只是图像,则不需要保存/恢复图形状态。

旋转270度的页面解决方案:

  1. cs.transform(Matrix.getRotateInstance(Math.toRadians(270), 0, page.getCropBox().getHeight() + page.getCropBox().getLowerLeftY()));

对于180度的情况:

  1. cs.transform(Matrix.getRotateInstance(Math.toRadians(180), page.getCropBox().getWidth() + page.getCropBox().getLowerLeftX(), page.getCropBox().getHeight() + page.getCropBox().getLowerLeftY()));
英文:

Here's a solution for a page that is rotated 90°:

  1. PDPageContentStream cs = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true);
  2. PDImageXObject image = ....
  3. cs.saveGraphicsState();
  4. cs.transform(Matrix.getRotateInstance(Math.toRadians(90), page.getCropBox().getWidth() + page.getCropBox().getLowerLeftX(), 0));
  5. cs.drawImage(image, 0, 0);
  6. cs.restoreGraphicsState();
  7. cs.close();

If it is only the image, then you don't need the save/restore.

Solution for a page that is rotated 270°:

  1. cs.transform(Matrix.getRotateInstance(Math.toRadians(270), 0, page.getCropBox().getHeight() + page.getCropBox().getLowerLeftY()));

For 180°:

  1. cs.transform(Matrix.getRotateInstance(Math.toRadians(180), page.getCropBox().getWidth() + page.getCropBox().getLowerLeftX(), page.getCropBox().getHeight() + page.getCropBox().getLowerLeftY()));

huangapple
  • 本文由 发表于 2020年8月26日 20:08:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63597316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定