英文:
Crop page with PDFBox and fill outside of rectangle with white
问题
我正在开发一个自定义工具,并使用以下代码裁剪页面。
PDPage page = document.getPage(i-1);
PDRectangle cropBox = new PDRectangle();
cropBox.setLowerLeftX(llx);
cropBox.setLowerLeftY(lly);
cropBox.setUpperRightX(urx);
cropBox.setUpperRightY(ury);
page.setCropBox(cropBox);
PDPageContentStream contentStream = new PDPageContentStream(document, page, true, false, false);
contentStream.close();
在工具中裁剪时,效果如下图所示。
但是当我打开PDF时,显示不同,我希望它居中显示,并且边缘与第一张图片中的一样。
英文:
i'm developing a custom tool and cutting out the pages with this code.
PDPage page = document.getPage(i-1);
PDRectangle cropBox = new PDRectangle();
cropBox.setLowerLeftX(llx);
cropBox.setLowerLeftY(lly);
cropBox.setUpperRightX(urx);
cropBox.setUpperRightY(ury);
page.setCropBox(cropBox);
PDPageContentStream contentStream = new PDPageContentStream(document,page, true, false, false);
contentStream.close();
In the tool when I trim it looks like this.
but when I open the pdf it looks different, I would like it to look centered and with the edges as in the first image
答案1
得分: 3
第二张图是正确的。裁剪框定义了页面画布上观看者应该显示的框。如果您希望可见的页面尺寸保持不变,将裁剪框保留原样,并将除内部矩形以外的所有部分填充为白色。
例如,像这样:
PDDocument document = ... // 要操作的文档
PDRectangle box = ... // 要保留可见部分的矩形
for (PDPage page : document.getPages()) {
PDRectangle cropBox = page.getCropBox();
try (PDPageContentStream canvas = new PDPageContentStream(document, page, AppendMode.APPEND, false, true)) {
canvas.setNonStrokingColor(1);
canvas.addRect(cropBox.getLowerLeftX(), cropBox.getLowerLeftY(), cropBox.getWidth(), cropBox.getHeight());
canvas.addRect(box.getLowerLeftX(), box.getLowerLeftY(), box.getWidth(), box.getHeight());
canvas.fillEvenOdd();
}
}
(TrimContent 测试 testTrimCengage1
)
在一条评论中,您问道:
> 是否可以将裁剪的部分居中显示在页面中央?
是的,可以通过相应地调整裁剪框来实现:
for (PDPage page : document.getPages()) {
PDRectangle cropBox = page.getCropBox();
cropBox = centerBoxAroundBox(box, cropBox.getWidth(), cropBox.getHeight());
try (PDPageContentStream canvas = new PDPageContentStream(document, page, AppendMode.APPEND, false, true)) {
canvas.setNonStrokingColor(1);
canvas.addRect(cropBox.getLowerLeftX(), cropBox.getLowerLeftY(), cropBox.getWidth(), cropBox.getHeight());
canvas.addRect(box.getLowerLeftX(), box.getLowerLeftY(), box.getWidth(), box.getHeight());
canvas.fillEvenOdd();
}
page.setMediaBox(cropBox);
page.setCropBox(cropBox);
}
(TrimContent 测试 testTrimAndCenterCengage1
)
使用这个辅助方法:
PDRectangle centerBoxAroundBox(PDRectangle box, float width, float height) {
float horitontalMargins = (width - box.getWidth()) / 2;
float verticalMargins = (height - box.getHeight()) / 2;
return new PDRectangle(box.getLowerLeftX() - horitontalMargins, box.getLowerLeftY() - verticalMargins, width, height);
}
(TrimContent 辅助方法 centerBoxAroundBox
)
英文:
The second image is correct. The crop box defines the box on the page canvas which viewers shall display. If you want the visible page dimensions to remain, leave the crop box as is and fill all of it except the inner rectangle with white.
E.g. like this:
PDDocument document = ... the document to manipulate ...;
PDRectangle box = ... the rectangle to remain visible ...;
for (PDPage page : document.getPages()) {
PDRectangle cropBox = page.getCropBox();
try (PDPageContentStream canvas = new PDPageContentStream(document, page, AppendMode.APPEND, false, true)) {
canvas.setNonStrokingColor(1);
canvas.addRect(cropBox.getLowerLeftX(), cropBox.getLowerLeftY(), cropBox.getWidth(), cropBox.getHeight());
canvas.addRect(box.getLowerLeftX(), box.getLowerLeftY(), box.getWidth(), box.getHeight());
canvas.fillEvenOdd();
}
}
(TrimContent test testTrimCengage1
)
In a comment you asked
> Could you take the trimmed piece and center it in the middle of the page?
Yes, by adapting the crop box accordingly:
for (PDPage page : document.getPages()) {
PDRectangle cropBox = page.getCropBox();
cropBox = centerBoxAroundBox(box, cropBox.getWidth(), cropBox.getHeight());
try (PDPageContentStream canvas = new PDPageContentStream(document, page, AppendMode.APPEND, false, true)) {
canvas.setNonStrokingColor(1);
canvas.addRect(cropBox.getLowerLeftX(), cropBox.getLowerLeftY(), cropBox.getWidth(), cropBox.getHeight());
canvas.addRect(box.getLowerLeftX(), box.getLowerLeftY(), box.getWidth(), box.getHeight());
canvas.fillEvenOdd();
}
page.setMediaBox(cropBox);
page.setCropBox(cropBox);
}
(TrimContent test testTrimAndCenterCengage1
)
using this helper method:
PDRectangle centerBoxAroundBox(PDRectangle box, float width, float height) {
float horitontalMargins = (width - box.getWidth()) / 2;
float verticalMargins = (height - box.getHeight()) / 2;
return new PDRectangle(box.getLowerLeftX() - horitontalMargins, box.getLowerLeftY() - verticalMargins, width, height);
}
(TrimContent helper method centerBoxAroundBox
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论