c# - 如何创建和应用密文?

标签 c# pdf itext

有没有办法使用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);
                }

        }

最佳答案

答案 1:创建密文注释

iText 是一个工具箱,让您能够创建任何您想要的对象。您正在使用便捷方法来创建文本注释。这只是表面现象。

您可以使用 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

enter image description here

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

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

enter image description here

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

关于c# - 如何创建和应用密文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24037282/

相关文章:

c# - 使用签名检测 PDF 更改

c# - 无法从 C# 代码访问 XAML 元素

c# - 能够重置使用 yield (C#) 生成的 IEnumerator

pdf - Jasperreports 标记属性和 PDF

c# - 使用 C# iText 7 拼合 XFA PDF

linux - 将 PDF 的内容打印到命令行

pdf - iTextSharp将图像缩放为整页

c# - 从网页中提取数据,针对特定部分进行解析并显示

c# - Entity Framework : Where the heck is it getting these columns from?

java - 使用 iText 创建 10000 多页 PDF