How do I generate appearances for annotations in a XFDF file?

Question:
I have annotations in a XFDF file, and I want to make sure that these have new appearances generated after merging into my PDF file.

However, I only want those newly merged annotations to have the refresh. Any existing should be left alone.

Answer:
Since annotations in a XFDF are required to have a unique ID, we can rely on that find the annotations and refresh, using the following code.

Note, starting with PDFNet 7.2 there will be a single API call to take care of all this for you. In the meantime, use this code.

var fdfdoc = new FDFDoc(FDFDoc.CreateFromXFDF(pathToXfdf));
HashSet<string> ids = new HashSet<string>();
Obj fdfroot = fdfdoc.GetRoot();
Obj fdfdict = fdfroot.FindObj("FDF");
Obj annotsarray = fdfdict.FindObj("Annots");
if (annotsarray == null || !annotsarray.IsArray()) return;
int annots_sz = annotsarray.Size();
for (int i = 0; i < annots_sz; ++i)
{
Obj annotobj = annotsarray.GetAt(i);
Annot annot = new Annot(annotobj);
ids.Add(annot.GetUniqueID().GetAsPDFText());
}
pdfDoc.FDFMerge(fdfdoc);
for (PageIterator itr = pdfDoc.GetPageIterator(); itr.HasNext(); itr.Next())
{
Page page = itr.Current();
for (int i = 0; i < page.GetNumAnnots(); ++i)
{
Annot annot = page.GetAnnot(i);
if (!annot.IsValid()) continue;
Obj o = annot.GetUniqueID();
if(o != null && o.IsString())
{
string id = o.GetAsPDFText();
if(ids.Contains(id))
{
annot.RefreshAppearance();
}
}
}
}