【发布时间】:2016-06-08 21:20:01
【问题描述】:
我已按照步骤创建注释并使用 iText 5.5.9 应用编辑。这是我的代码:
using (var stamper = new PdfStamper(pdfReader, new FileStream(newFilePath, FileMode.Create)))
{
// Redact the values.
var pdfAnot1 = new PdfAnnotation(stamper.Writer, new Rectangle(165f, 685f, 320f, 702f));
pdfAnot1.Title = "First Page";
pdfAnot1.Put(PdfName.SUBTYPE, PdfName.REDACT);
pdfAnot1.Put(PdfName.IC, new PdfArray(new[] { 0f, 0f, 0f }));
pdfAnot1.Put(PdfName.OC, new PdfArray(new[] { 1f, 0f, 0f })); // red outline
stamper.AddAnnotation(pdfAnot1, 1);
for (var i = 1; i <= pdfReader.NumberOfPages; i++)
{
var pdfAnot2 = new PdfAnnotation(stamper.Writer, new Rectangle(220f, 752f, 420f, 768f));
pdfAnot2.Title = "Header";
pdfAnot2.Put(PdfName.SUBTYPE, PdfName.REDACT);
pdfAnot2.Put(PdfName.IC, new PdfArray(new[] { 0f, 0f, 0f }));
pdfAnot2.Put(PdfName.OC, new PdfArray(new[] { 1f, 0f, 0f })); // red outline
stamper.AddAnnotation(pdfAnot2, i);
}
var cleaner = new PdfCleanUpProcessor(stamper);
cleaner.CleanUp();
}
但是,我总是在 PdfCleanUpProcessor 构造中收到以下异常:
对象引用未设置为对象的实例。 在 iTextSharp.xtra.iTextSharp.text.pdf.pdfcleanup.PdfCleanUpProcessor.ExtractLocationsFromRedactAnnots(Int32 页面,PdfDictionary pageDict) 在 iTextSharp.xtra.iTextSharp.text.pdf.pdfcleanup.PdfCleanUpProcessor.ExtractLocationsFromRedactAnnots() 在 iTextSharp.xtra.iTextSharp.text.pdf.pdfcleanup.PdfCleanUpProcessor..ctor(PdfStamper pdfStamper)
在annotDict的赋值上,extractLocationsFromRedactAnnots中似乎产生了一个空引用,所以下一行抛出异常:
/**
* Extracts locations from the redact annotations contained in the document and applied to the given page.
*/
private IList<PdfCleanUpLocation> ExtractLocationsFromRedactAnnots(int page, PdfDictionary pageDict) {
List<PdfCleanUpLocation> locations = new List<PdfCleanUpLocation>();
if (pageDict.Contains(PdfName.ANNOTS)) {
PdfArray annotsArray = pageDict.GetAsArray(PdfName.ANNOTS);
for (int i = 0; i < annotsArray.Size; ++i) {
PdfIndirectReference annotIndirRef = annotsArray.GetAsIndirectObject(i);
PdfDictionary annotDict = annotsArray.GetAsDict(i);
PdfName annotSubtype = annotDict.GetAsName(PdfName.SUBTYPE);
if (annotSubtype.Equals(PdfName.REDACT)) {
SaveRedactAnnotIndirRef(page, annotIndirRef.ToString());
locations.AddRange(ExtractLocationsFromRedactAnnot(page, i, annotDict));
}
}
}
return locations;
}
知道为什么会这样吗?一个示例 PDF 是here。
【问题讨论】:
-
如果您需要 PDF 示例,请告诉我。 - 是的,请提供示例以重现该问题。这通常通过在 Dropbox 或 googledrive 上公开共享文档并在此处发布 URL 来完成。
-
我添加了一个指向示例 PDF 的链接。
标签: itextsharp redaction