gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:25 25 4
gpt4 key购买 nike

有什么方法可以使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com