【问题标题】:Itext 7: Cannot remove annotations or flatten documentItext 7:无法删除注释或拼合文档
【发布时间】:2017-04-25 13:59:40
【问题描述】:

自从让冲压过程大部分时间发挥作用以来,我一直在玩更多。现在我正在尝试从 PDF 中删除所有注释。我已经尝试了多种方法,如下所示:

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 仍然保持不变。

我还尝试循环浏览所有类似于注释的 PdfName 对象(Annot、Annots、Annotation 等)

我用来获取注解的方法不正确吗?这正是我为我的印章操作获取印章属性的方式。

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

【问题讨论】:

    标签: c# pdf annotations itext7 stamp


    【解决方案1】:

    删除注释

    方法 1。遍历所有注解并一个一个删除:

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

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

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

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

    clearAnnotations(pdfDocument.getFirstPage());
    

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

    扁平化注释

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

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

    【讨论】:

    • 就这么简单,感谢您消除我的误解!
    猜你喜欢
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    相关资源
    最近更新 更多