无法更改/注释的内容

Can't change /Contents of annotation

我正在尝试使用 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();
    }

当我在 Adob​​e Reader 中打开输出文件时,none 文本在任何注释中都发生了变化。我应该如何在注释中设置新值?

更新:我发现单击注释时出现的弹出框中的值正在更改。在某些情况下,当我在弹出框中修改此值时,更改会应用到注释中。

正如 OP 在评论中阐明的那样:

This annotation is a FreeText, how do I find and change the text that's displayed in this text box?

自由文本注释允许使用多种机制来设置显示的文本:

  • 预先格式化的外观流,由AP字典
  • 中的N条目引用
  • 具有默认样式字符串的富文本字符串分别在 RCDS 中给出
  • 分别应用于DAContents中给出的内容的默认外观字符串

(有关详细信息,请参阅 PDF 规范 ISO 32000-1 第 12.5.6.6 节 自由文本注释

如果您想使用其中一种机制更改文本,请确保删除或调整其他机制的条目内容;否则,您的更改可能对某些查看者不可见,甚至对其他查看者不可见。

I can't figure out how to determine if there is an appearance stream. Is that the /AP property? I checked that for one of the annotations and it's a dictionary with a single entry whose value is 28 0 R.

因此其中一个注释确实带有外观流。值为28 0 R单项大概有N名称表示normal外貌。 28 0 R 是对对象编号为 28 且生成为 0 的间接对象的引用。

如果您想更改文本内容但不想处理格式细节,则应删除 AP 条目。