【问题标题】:How to create and apply redactions?如何创建和应用修订?
【发布时间】:2014-06-04 12:19:21
【问题描述】:

有没有办法使用 iText 实现 PDF 编辑?使用 Acrobat SDK API,我发现编辑似乎也只是带有子类型“Redact”的注释。所以我想知道是否也可以在 iTextSharp 中创建它们?

使用 Acrobat SDK,代码就像这样:

AcroPDAnnot annot = page.AddNewAnnot(-1, "Redact", rect) as AcroPDAnnot;

(尽管annot.Perform(avDoc) 似乎不起作用,但我无法应用它们。想法?)

在 iTextSharp 中,我可以像这样创建简单的文本注释

PdfAnnotation annotation = PdfAnnotation.CreateText(stamper.Writer, rect, "Title", "Content", false, null);

我发现的唯一其他选项是按照here 的说明创建黑色矩形,但这不会删除文本(仍然可以选择)。我想创建编校注释并最终应用编校。

// 更新:

当我终于有时间创建一个工作示例时,我想在这里分享它。它最终不会应用编校,但会创建有效的编校,这些编校会在 Acrobat 中正确显示,然后可以手动应用。

            using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            PdfReader pdfReader = new PdfReader(stream);
            // Create a stamper
            using (PdfStamper stamper = new PdfStamper(pdfReader, new FileStream(newFileName, FileMode.OpenOrCreate)))
                {
                    // Add the annotations
                    int page = 1;
                    iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(500, 50, 200, 300);

                    PdfAnnotation annotation = new PdfAnnotation(stamper.Writer, rect);
                    annotation.Put(PdfName.SUBTYPE, new PdfName("Redact"));

                    annotation.Title = "My Author"; // Title = author
                    annotation.Put(new PdfName("Subj"), new PdfName("Redact")); // Redaction "Subject". When created in Acrobat, this is always set to "Redact"

                    float[] fillColor = { 0, 0, 0 }; // Black
                    annotation.Put(new PdfName("IC"), new PdfArray(fillColor)); // Interior color

                    float[] fillColorRed = { 1, 0, 0 }; // Red
                    annotation.Put(new PdfName("OC"), new PdfArray(fillColorRed)); // Outline color

                    stamper.AddAnnotation(annotation, page);
                }

        }

【问题讨论】:

  • 您好,很抱歉回复晚了,但我们已经在 iText 5.5.4 中实施了编辑,所以我更新了我的答案。
  • 哦,太好了!感谢更新!一旦你把这个移植到 C# 我会吻你;)
  • 你去吧! :) 刚刚在 5.5.5 的更新日志中看到,已将编辑移植到 iTextSharp!

标签: c# pdf itextsharp


【解决方案1】:

答案 1:创建密文注释

iText 是一个工具箱,可让您创建所需的任何对象。您正在使用 便捷方法 来创建 Text 注释。这只是表面上的问题。

您可以使用 iText 创建您想要的任何类型的注释,因为 PdfAnnotation 类扩展了 PdfDictionary 类。

这在我的书 "iText in Action - Second edition" 的第 7 章中有解释。 GenericAnnotations 是说明此功能的示例。

如果我们将此示例移植到 C#,我们有:

PdfAnnotation annotation = new PdfAnnotation(writer, rect);
annotation.Title = "Text annotation";
annotation.Put(PdfName.SUBTYPE, PdfName.TEXT);
annotation.Put(PdfName.OPEN, PdfBoolean.PDFFALSE);
annotation.Put(PdfName.CONTENTS,
  new PdfString(string.Format("Icon: {0}", text))
);
annotation.Put(PdfName.NAME, new PdfName(text));
writer.AddAnnotation(annotation);

这是创建文本注释的手动方式。你想要一个Redact 注释,所以你需要这样的东西:

PdfAnnotation annotation = new PdfAnnotation(writer, rect);
annotation.Put(PdfName.SUBTYPE, new PdfName("Redact"));
writer.AddAnnotation(annotation);

您可以使用Put() 方法添加注释所需的所有其他键。

答案 2:如何“应用”密文注释

第二个问题需要 itext-xtra.jar(iText 附带的一个额外 jar),并且您至少需要 iText 5.5.4。添加不透明矩形的方法不应用编辑:编辑的内容只是被覆盖,而不是被删除。您仍然可以选择文本并复制/粘贴它。如果您不小心,您可能会遇到所谓的PDF Blackout Folly。例如,参见 NSA / AT&T 丑闻。

假设您有一个添加了一些编辑注释的文件:page229_redacted.pdf

我们现在可以使用此代码来删除由密文注释标记的内容:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(stamper);
    cleaner.cleanUp();
    stamper.close();
    reader.close();
}

这会生成以下 PDF:page229_apply_redacted.pdf

如您所见,红色矩形边框被填充的黑色矩形取代。如果您尝试选择原始文本,您会发现它不再存在。

【讨论】:

  • 谢谢,这似乎工作到目前为止。但是还有一种方法可以应用编辑吗?
  • “申请”是什么意思。当您执行writer.AddAnnotation(annotation) 时,您将注释添加到文档中。 “应用修订”与将其添加到文档有何不同?
  • 他的意思是对编校注释所覆盖的区域进行编校,并使用编校注释外观将注释展平。
  • 完全正确。在应用编辑之前,它只是一个可以再次删除的注释,即它下面的内容仍然可见
  • 如果您将注释展平,则它不再是注释。这不是原始问题中所问的:找到AcroPDAnnot annot = page.AddNewAnnot() 的等价物。现在问题完全不同了:您想使用PdfStamper 标记现有 PDF 文档上的任何内容。
猜你喜欢
  • 2020-11-25
  • 2012-10-25
  • 2021-03-30
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 2019-03-09
相关资源
最近更新 更多