Using iTextSharp, can’t change /Contents of annotation (x-post StackOverflow)

Link to StackOverflow question here.

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(); } 

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? In particular, how can I change a FreeText annotation so that it displays different text contents?

SO user mkl suggested that I might need to change the appearance stream in addition to the /Contents property, but I haven't been able to find the appearance stream using iTextSharp.

by sigilToNoise via /r/csharp

Leave a Reply