Cropping a region from a PDF page with PDFBox

天涯浪子 提交于 2019-12-22 09:59:41

问题


I am trying to crop a region out of a PDF page programmatically. Specifically, my input is going to be a single page PDF and a bounding box on the page. Output is going to be a PDF that contains the characters, graphics paths and images from the original PDF, and it should look like the original PDF. In other words, I want a function that is similar to cropping a region out of an image, but with PDFs.

Three questions:

  1. Is it at all possible to do? From my knowledge of PDFs, it seems possible. But I'm no expert, so I would like to know first if there are some things I'm missing here.

  2. Is there any open source software for this?

  3. Can PDFBox do this currently? I couldn't find such a functionality but I might have missed it. Does anybody know of any attempt of doing this?


回答1:


1- Yes, this is called the crop box.

2- Yes, e.g. PDFBox.

3- Yes, just open a PDF, set a crop box, and save it:

PDDocument doc = PDDocument.load(new File(...));
PDPage page = doc.getPage(0);
page.setCropBox(new PDRectangle(20, 20, 200, 400));
doc.save(...);
doc.close();

The numbers in PDRectangle are user space units. 1 unit = 1/72 inches.

Note that the contents outside the cropbox are not gone, they are just hidden.



来源:https://stackoverflow.com/questions/36126390/cropping-a-region-from-a-pdf-page-with-pdfbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!