我试图使用iTextSharp更改某些PDF注释中的文本。这是我的代码:无法更改/注释的内容
void changeAnnotations(string inputPath, string outputPath)
{
PdfReader pdfReader = new PdfReader(inputPath);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(outputPath, FileMode.Create));
//get the PdfDictionary of the 1st page
PdfDictionary pageDict = pdfReader.GetPageN(1);
//get annotation array
PdfArray annotArray = pageDict.GetAsArray(PdfName.ANNOTS);
//iterate through annotation array
int size = annotArray.Size;
for (int i = 0; i < size; i++)
{
//get value of /Contents
PdfDictionary dict = annotArray.GetAsDict(i);
PdfString contents = dict.GetAsString(PdfName.CONTENTS);
//check if /Contents key exists
if (contents != null)
{
//set new value
dict.Put(PdfName.CONTENTS, new PdfString("value has been changed"));
}
}
pdfStamper.Close();
}
当我在Adobe Reader中打开输出文件时,任何注释中的文本都没有改变。我应该如何在注释中设置新值?
更新:我发现在单击注释时出现的弹出框中更改了值。在某些情况下,当我在弹出框中修改此值时,更改将应用于注释。
您不检查相关注释是否有外观流。如果确实如此,则PDF查看器可以选择仅在该流中预先定义注释,而不是生成自己的外观。此外,**内容**可能会或可能不会显示为注释文本,参见参考资料。规范:*应为注释显示的文本,或者,如果此类注释不显示文本,则以人类可读的形式*替代描述注释的内容。 – mkl
@mkl,我无法弄清楚如何确定是否有外观流。那是'/ AP'属性吗?我检查了其中一个注释,它是一个单个条目的值为'28 0 R'的字典,但是'annotArray'中没有任何项目具有该值。这个注释是一个'FreeText',我如何查找和更改显示在这个文本框中的文本? – sigil