英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论