使用PDFBox从PDF页面裁剪区域 [英] Cropping a region from a PDF page with PDFBox

查看:349
本文介绍了使用PDFBox从PDF页面裁剪区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式从PDF页面中裁剪区域.具体来说,我的输入将是单页PDF和页面上的边框.输出将是一个PDF,其中包含原始PDF的字符,图形路径和图像,并且外观应与原始PDF相似.换句话说,我想要一个类似于从图像中裁剪区域但具有PDF的功能.

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.

三个问题:

  1. 这有可能吗?据我对PDF的了解,这似乎是可能的.但是我不是专家,所以我想先知道这里是否缺少某些东西.

  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.

为此有任何开源软件吗?

Is there any open source software for this?

PDFBox当前可以执行此操作吗?我找不到这样的功能,但是我可能会错过它.有人知道这样做的任何尝试吗?

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-是的,这称为 2-是,例如PDFBox.

2- Yes, e.g. PDFBox.

3-是的,只需打开PDF,设置一个裁剪框并保存即可:

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();

PDRectangle中的数字为用户空间单位. 1个单位= 1/72英寸.

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.

这篇关于使用PDFBox从PDF页面裁剪区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆