使用iText删除指定区域中包含的文本引用 [英] Remove text occurrences contained in a specified area with iText

查看:1278
本文介绍了使用iText删除指定区域中包含的文本引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以删除pdf文档指定区域(红色矩形区域)中包含的所有文本事件?使用iText?

Is it possible to remove all text occurrences contained in a specified area(red color rectangle area) of ​​a pdf document Using iText?

推荐答案

请查看 RemoveContentInRectangle 示例。

假设我们有以下页面:

现在我们要删除坐标定义的矩形中的所有文本: llx = 97,lly = 405,urx = 480,ury = 445] (其中 ll 代表左下角和 ur 代表右上角。

Now we want to remove all the text in the rectangle defined by the coordinates: llx = 97, lly = 405, urx = 480, ury = 445] (where ll stands for lower-left and ur stands for upper-right).

我们现在可以使用以下代码:

We can now use the following code:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
    cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(97f, 405f, 480f, 445f), BaseColor.GRAY));
    PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper);
    cleaner.cleanUp();
    stamper.close();
    reader.close();
}

如您所见,我们定义了一个列表对象。在这个列表中,我们添加一个 PdfCleanUpLocation 传递页码,一个 Rectangle 定义我们想要清理的区域,以及显示已删除内容的区域的颜色。

As you see, we define a list of PdfCleanUpLocation objects. To this list, we add a PdfCleanUpLocation passing the page number, a Rectangle defining the area we want to clean up, and a color that will show the area where content has been removed.

然后我们传递 PdfCleanUpLocation 的列表到 PdfCleanUpProcessor 以及 PdfStamper 实例。我们调用 cleanUp()方法,当我们关闭 PdfStamper 实例时,我们得到以下结果:

We then pass this list of PdfCleanUpLocations to the PdfCleanUpProcessor along with the PdfStamper instance. We invoke the cleanUp() method and when we close the PdfStamper instance, we get the following result:

您可以检查此文件:您将无法再选择灰色区域中的任何文本。该矩形内的所有文本都已删除。

You can inspect this file: you will no longer be able to select any text in the gray area. All the text inside that rectangle has been removed.

请注意,只有将itext-xtra.jar添加到CLASSPATH时,此代码示例才有效(itext-xtra是与iText核心一起发货)。它仅适用于等于或高于iText 5.5.4的版本。

Note that this code sample will only work if you add the itext-xtra.jar to your CLASSPATH (itext-xtra is shipped with iText core). It will only work with versions equal to or higher than iText 5.5.4.

这篇关于使用iText删除指定区域中包含的文本引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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