IText 编辑或删除 PDF 上的图层 [英] IText edit or remove the layer on PDF
问题描述
H 全部,
我正在使用 IText 在 PDF 上添加文本图层.现在我想编辑 PDF 上的现有图层,图层也仅由 IText 创建.IText好像没有这样的方法支持.
I am Using IText for adding text layers on PDF. Now i want to edit the existing layers on the PDF, layers are also created by IText only. Seems IText dont have such methods to suppport.
我想到的其他方法是删除现有图层并在其位置放置新图层.IText 似乎也不支持删除.有什么办法可以做到这些吗?
I thought of other way around is remove existing Layer and place new layer on its place. Seems remove also not supported by IText. Any way todo these?
提前致谢.
推荐答案
正如评论中所证明的,有问题的层实际上是 iText 所称的层,但实际上在 PDF 行话中被称为可选内容组.
As it turned out in comments, the layers in question are what iText indeed calls layers but what actually in PDF lingo is called optional content groups.
在 iText Xtra 包中确实有一个用于删除这些层的实用程序类(不是 extrajars,而是 itext-xtra.jar):com.itextpdf.text.pdf.ocg.OCGRemover
使用 OCGParser
在同一个包中.
There indeed is a utility class for removing such layers in the iText Xtra package (not the extrajars, but itext-xtra.jar): com.itextpdf.text.pdf.ocg.OCGRemover
which makes use of the class OCGParser
in the same package.
/**
* Class that knows how to remove OCG layers.
*/
public class OCGRemover
{
/**
* Removes layers from a PDF document
* @param reader a PdfReader containing a PDF document
* @param layers a sequence of names of OCG layers
* @throws IOException
*/
public void removeLayers(PdfReader reader, String... layers) throws IOException
[...]
}
将该方法应用于 PdfReader
后,您显然必须保存更改,例如通过 PdfStamper.
After applying that method to a PdfReader
you obviously have to save the changes, e.g. by means of a PdfStamper.
这篇关于IText 编辑或删除 PDF 上的图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!