java PDFBOX旋转矩形

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

java PDFBOX rotate rect

问题

我正在尝试使用Apache PDFBOX库旋转一个矩形,但我已经在Google上搜索了,没有任何结果显示。下面是部分代码:

PDPage page = document.getPage(i-1);
PDPageContentStream contentStream = new PDPageContentStream(document, page, true, false, false);
contentStream.setNonStrokingColor(Color.BLACK);
contentStream.addRect(dto.getLeft(), dto.getTop() - factY, dto.getWidth(), dto.getHeight());
contentStream.fill();
contentStream.close();
英文:

I'm trying to rotate a rectangle with the Apache PDFBOX Library but I've googled it and nothing shows up. Here's some of the code:

PDPage page = document.getPage(i-1);
PDPageContentStream contentStream = new PDPageContentStream(document,page, true, false, false);
contentStream.setNonStrokingColor(Color.BLACK);
contentStream.addRect(dto.getLeft(), dto.getTop() - factY, dto.getWidth(), dto.getHeight());
contentStream.fill();
contentStream.close();

答案1

得分: 1

这里有一个针对 PDFBox 2.0.* 的答案,它绘制一个围绕其左下角原点旋转的框:

// 绘制一个填充的框,矩形 x=200,y=500,宽度=200,高度=100
contents.saveGraphicsState();
contents.transform(Matrix.getRotateInstance(Math.toRadians(105), 200, 500));
contents.addRect(0, 0, 200, 100);
contents.fill();
contents.restoreGraphicsState();

现在将 Math.toRadians(105) 更改为所需的角度,您就可以得到旋转后的矩形。

您似乎正在使用较旧版本的 PDFBox。我强烈建议使用 2.0.* 版本。

英文:

Here's an answer for PDFBox 2.0.* which draws a box rotated around its bottom left origin:

// draw a filled box with rect x=200, y=500, w=200, h=100
contents.saveGraphicsState();
contents.transform(Matrix.getRotateInstance(Math.toRadians(105), 200, 500));
contents.addRect(0, 0, 200, 100);
contents.fill();
contents.restoreGraphicsState();

Now change Math.toRadians(105) to the desired angle and you have your rotated rectangle.

You seem to be using an older version of PDFBox. I strongly recommend to use 2.0.* instead.

huangapple
  • 本文由 发表于 2020年8月14日 23:39:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63415895.html
匿名

发表评论

匿名网友

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

确定