【发布时间】: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