我正在使用 itextsharp 以编程方式生成对 PDF 注释的回复。一切似乎都很好,只是在 Acrobat DC 中生成的回复没有按时间顺序排序。它出现在手动创建的回复上方,即使它有一个较晚的时间戳。请参阅下图,显示它在 Acrobat DC 上的显示方式。
尽管有较晚的时间戳,但自动生成的回复出现在手动回复上方:
以下是我的代码:
PdfArray originalRect = originalAnnot.GetAsArray(PdfName.RECT);
Rectangle replyRect = new Rectangle(
originalRect.GetAsNumber(0).FloatValue, originalRect.GetAsNumber(1).FloatValue,
originalRect.GetAsNumber(2).FloatValue, originalRect.GetAsNumber(3).FloatValue
);
PdfAnnotation replySticky = PdfAnnotation.CreateText(stamper.Writer, replyRect, author, change.ReplyToAdd, true, "Comment");
replySticky.Put(PdfName.IRT, annots.getAsIndirectObject(i));
replySticky.Put(PdfName.CREATIONDATE, new PdfDate(DateTime.Now));
replySticky.Put(PdfName.M, new PdfDate(DateTime.Now));
replySticky.Put(PdfName.AUTHOR, new PdfString(author));
replySticky.Put(PdfName.NM, new PdfString(Guid.NewGuid().ToString()));
replySticky.Put(new PdfName("Subj"), new PdfString("Sticky Note"));
PdfNumber n = originalAnnot.GetAsNumber(PdfName.F);
replySticky.Put(PdfName.F, new PdfNumber(n.IntValue | PdfAnnotation.FLAGS_HIDDEN));
stamper.AddAnnotation(replySticky, page);
我是否缺少某些确定 Acrobat 中显示顺序的属性?谢谢!
更新:关于我如何获得 originalAnnot 的价值存在一个问题。在写入 PDF 之前,我将检索所有现有注释,并将它们存储在字典中,使用的键是页码和注释的 NM 值的组合。这是代码片段:
Dictionary<string, Annotation> annotationMap = new Dictionary<string, Annotation>();
pdfReader = new PdfReader(inputPath, password);
// First pass to read annotations
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
//get the PdfDictionary of the 1st page
PdfDictionary pageDict = pdfReader.GetPageN(page);
//get annotation array
PdfArray annotArray = pageDict.GetAsArray(PdfName.ANNOTS);
if (annotArray == null)
{
continue;
}
//iterate through annotation array
int size = annotArray.Size;
for (int i = 0; i < size; i++)
{
PdfDictionary dict = annotArray.GetAsDict(i);
string name = getStringFromPDFString(dict.GetAsString(PdfName.NM));
string author = getStringFromPDFString(dict.GetAsString(PdfName.T));
if (name != string.Empty && author != string.Empty)
{
string contents = getStringFromPDFString(dict.GetAsString(PdfName.CONTENTS));
string subType = getSubType((PdfName)dict.GetAsName(PdfName.SUBTYPE));
string moddate = getStringFromPDFString(dict.GetAsString(PdfName.M));
string state = getStringFromPDFString(dict.GetAsString(PdfName.STATE));
string inreplyto = getStringFromPDFString(pdfDictionary.GetAsString(PdfName.NM));
string key = string.Format("{0}={1}", page, name);
if (annotationMap.ContainsKey(key))
{
Console.WriteLine(string.Format("Annotation with duplicate key {0} ignored", key));
continue;
}
string context = string.Empty;
string highlightedtext = getHighlightText(dict, page, pdfReader, out context);
Annotation ann = new Annotation(name, moddate, subType, author, page, inputPath, contents, inreplyto, state, context, highlightedtext, dict, annotArray.GetAsIndirectObject(i));
annotationMap.Add(ann.Identifier, ann);
}
} // Each annotation
} // Each page