如何创建和应用编辑?
有没有办法使用iText实现PDF编辑? 使用Acrobat SDK API我发现redactions似乎只是带有子类型“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);
我发现的唯一其他选项是创建黑色矩形,如此处所述,但不会删除文本(仍然可以选择)。 我想创建编校注释并最终应用编辑。
//更新:
当我最终创建一个工作示例时,我想在此处分享它。 它最终不会应用redaction,但会创建有效的redaction,这些redaction在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是说明此function的示例。
如果我们将此示例移植到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
如您所见,红色矩形边框由填充的黑色矩形替换。 如果您尝试选择原始文本,您会发现它已不再存在。
- SetUnhandledExceptionFilter如何在.NET WinForms应用程序中工作?
- C#,位和字节 – 如何从字节中检索位值?
- Update-Plugin CRM 2011上的实体前和实体后图像
- 找不到方法:’System.Type System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid)’
- 如何在C#中增加控制台窗口的大小?
- 用户帮助的所有控件的全局事件处理程序
- 为C#中的所有事件和委托创建一个catch-all处理程序
- C#:如何将winforms按钮的文本绑定到资源
- automapper – 如果属性类型与同一属性名称不同,则忽略映射 – C#