【发布时间】:2017-10-11 06:39:28
【问题描述】:
我使用 ITextSharp 处理 PDF 注释。我能够非常顺利地添加注释。 但现在我正在尝试编辑它们。看起来我的 PdfReader 对象实际上已更新。但由于某种原因,我无法保存它。如下面的 sn-p 所示,我尝试使用压模获取字节数组。不管注解多长,字节数组只比之前的版本长1个字节。而且当我打开保存在文件系统上的PDF时,我仍然有旧的注释......
private void UpdatePDFAnnotation(string title, string body)
{
byte[] newBuffer;
using (PdfReader pdfReader = new PdfReader(dataBuffer))
{
int pageIndex = 1;
int annotIndex = 0;
PdfDictionary pageDict = pdfReader.GetPageN(pageIndex);
var annots = pageDict.GetAsArray(PdfName.ANNOTS);
if (annots != null)
{
PdfDictionary annot = annots.GetAsDict(annotIndex);
annot.Put(PdfName.T, new PdfString(title));
annot.Put(PdfName.CONTENTS, new PdfString(body));
}
// ********************************
// this line shows the new annotation is in here. Just have to save it somehow !!
var updatedBody = pdfReader.GetPageN(pageIndex).GetAsArray(PdfName.ANNOTS).GetAsDict(0).GetAsString(PdfName.CONTENTS);
Debug.Assert(newBody == updatedBody.ToString(), "Annotation body should be equal");
using (MemoryStream outStream = new MemoryStream())
{
using (PdfStamper stamp = new PdfStamper(pdfReader, outStream, '\0', true))
{
newBuffer = outStream.ToArray();
}
}
}
File.WriteAllBytes( @"Assets\Documents\AnnotedPdf.pdf", newBuffer);
}
知道我的代码有什么问题吗?
【问题讨论】:
-
尝试插入 stamp.Close();将 outStream 缓冲区写入 newBuffer 后。
-
@SoumenMukherjee “尝试插入 stamp.Close(); 在将 outStream 缓冲区写入 newBuffer 之后。” - 不是 after 而是 before .