How to replace an existing attachment with a new one?

Q:

I have a file attachment annotation that points to word doc, and I want to replace it with a new one, how do I do that?

A:

The following code will replace attachment of the first annotation on page 1 (assuming that this is the FileAttachment annotation)

Annot annot = doc.GetPage(1).GetAnnot(0);
pdftron.PDF.Annots.FileAttachment fileAttachment = new pdftron.PDF.Annots.FileAttachment(annot); // assumes annot is a FileAttachment type
FileSpec fileSpec = FileSpec.Create(doc, input_path + "new_attachment.docx");
`fileAttachment`.SetFileSpec(`fileSpec`);
doc.Save(output_path + "out.pdf", SDFDoc.SaveOptions.e_remove_unused);

See the AnnotationTest sample, or search this forum post, for code on how to find annotations by other means, such as type.