无法更改/注释内容 [英] Can't change /Contents of annotation
问题描述
我正在尝试使用iTextSharp更改某些PDF注释中的文本。这是我的代码:
I'm trying to change the text in some PDF annotations using iTextSharp. Here is my code:
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中打开输出文件时,没有任何文字发生变化在任何注释中。我应该如何在注释中设置新值?
When I open the output file in Adobe Reader, none of the text has changed in any of the annotations. How should I be setting the new value in an annotation?
更新:我发现当我点击时出现的弹出框中正在更改该值注解。在某些情况下,当我在弹出框中修改此值时,更改将应用于注释。
UPDATE: I've found that the value is being changed in the popup box that appears when I click on the annotation. And in some cases, when I modify this value in the popup box, the change is then applied to the annotation.
推荐答案
As OP在评论中澄清:
As the OP clarified in a comment:
这个注释是
FreeText
,怎么做我找到并更改了此文本框中显示的文本?
This annotation is a
FreeText
, how do I find and change the text that's displayed in this text box?
自由文本注释允许使用多种机制来设置显示的文本:
Free text annotations allow a number of mechanisms to set the displayed text:
- 预先格式化的外观流,由 AP N 条目引用>词典
- 富文本字符串,默认样式字符串分别在 RC 和 DS 中给出
- 应用于 DA 和内容中给出的内容的默认外观字符串
- A pre-formatted appearance stream, referenced by the N entry in the AP dictionary
- A rich text string with a default style string given in RC and DS respectively
- A default appearance string applied to the contents given in DA and Contents respectively
(有关详细信息,参见PDF规范 ISO 32000-1 部分12.5.6.6 自由文本注释)
(For details cf. the PDF specification ISO 32000-1 section 12.5.6.6 Free Text Annotations)
如果要使用其中一种机制更改文本,请确保删除或调整其他机制的条目内容;否则您的更改可能在一些观看者身上不可见或甚至不可见,但在其他观看者身上不可见。
我无法想象如何确定是否有外观流。那是
/ AP
属性吗?我检查了其中一个注释,它是一个带有单个条目的字典,其值为28 0 R
。
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 is28 0 R
.
因此其中一个注释确实附带了一个外观流。 单个条目的值 28 0 R
可能有 N 名称来表示正常外观。 28 0 R
是对对象编号 28
且生成 0 <的间接对象的引用/ code>。
So that one of the annotations indeed comes with an appearance stream. The single entry whose value is 28 0 R
presumably has the N name to indicate the normal appearance. 28 0 R
is a reference to the indirect object with object number 28
and generation 0
.
如果要更改文本内容但不想处理格式详细信息,则应删除 AP 进入。
If you want to change the text content but do not want to deal with the formatting details, you should remove the AP entry.
这篇关于无法更改/注释内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!