Itext 7:无法删除注释或展平文档 [英] Itext 7: Cannot remove annotations or flatten document

查看:30
本文介绍了Itext 7:无法删除注释或展平文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从让冲压过程在大多数情况下工作以来,我一直在玩更多.现在我试图从 PDF 中删除所有注释.我已经尝试了多种方法,如下所示:

I've been playing around more since getting the stamping process to work for the most part. Now I am trying to delete all annotations from a PDF. I have tried it multiple ways as shown below:

public void ClearStamps()
{
    IList<PdfAnnotation> annotList = pdfDoc.GetFirstPage().GetAnnotations();
    int listCount = annotList.Count;

    for (int i = 0; i < listCount; i++)
    {
        annotList.RemoveAt(i);
    }

    pdfDoc.Close();

    if (Overwrite)
    {
        File.Delete(pdfFilePath);
        File.Move(pdfFileDest, pdfFilePath);
    }
}

IList<PdfAnnotation> annotList = pdfDoc.GetFirstPage().GetAnnotations();
int listCount = annotList.Count;

for (int i = 0; i < listCount; i++)
{
    annotList[i].Remove(PdfName.Annots);
}

pdfDoc.Close();

经过上述操作后,生成的 PDF 仍然完好无损.

The resulting PDFs still remain intact after the operation above.

我还尝试遍历所有类似于注释的 PdfName 对象(Annot、Annots、Annotation 等)

I also tried cycling through all the PdfName objects that resemble annotations (Annot, Annots, Annotation, etc.)

我用来获取注释的方法不正确吗?这正是我为我的邮票操作获取邮票属性的方式.

Is the method I am using to acquire the annotations incorrect? That is exactly how I acquired stamp properties for my stamp operation.

此外,在注释操作方面,我似乎找不到任何类似于 iText5 中的 Flattening bool 的方法 - 我能得到的最接近的是将注释标志设置为 LOCKED...不是最理想的扁平化方式.

Additionally, when it comes to annotation manipulation, I can't seem to find any method that resembles the Flattening bool from iText5 - the closest I can get was setting the annotation flags to LOCKED...not the most ideal way to flatten.

推荐答案

移除注解

方法 1.遍历所有注释并一一删除:

Removing annotations

Approach 1. Iterate over all the annotations and remove one-by-one:

private void clearAnnotations(PdfPage page) {
    Collection<PdfAnnotation> annotations = new ArrayList<>(page.getAnnotations());
    for (PdfAnnotation annotation : annotations) {
        page.removeAnnotation(annotation);
    }
}

方法 2.更底层的操作:从页面字典中删除/Annots:

Approach 2. More low-level manipulation: remove /Annots from page dictionary:

private void clearAnnotations(PdfPage page) {
    page.getPdfObject().remove(PdfName.Annots);
}

为每个要清除注释的页面调用该方法,例如:

Call the method for every page you want to clear the annotations from, e.g.:

clearAnnotations(pdfDocument.getFirstPage());

代码是用 Java 编写的,但应该很容易翻译成 C#.

The code is in Java, but should be very easily translated to C#.

iText5 仅支持扁平化具有外观流的注释(与表单字段无关).该功能的范围非常有限.此功能将来可以添加到 iText7 中,但目前没有.

iText5 supported only flattening of annotations (that are not related to form fields) which have appearance streams in them. The scope of that functionality was really limited. This functionality can be added to iText7 in the future, but currently it is absent.

同时,您可以尝试手动实现相同的功能.这个想法是在注释中找到一个外观流(/AP 键是起点),然后创建一个 PdfFormXObject 来包装该外观流,然后添加反对任何你喜欢的 PdfCanvas.

Meanwhile, you can try to achieve the same functionality manually. The idea is to find an appearance stream in the annotation (/AP key is the starting point), then create a PdfFormXObject that would wrap that appearance stream, and then add that object to any PdfCanvas you like.

这篇关于Itext 7:无法删除注释或展平文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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