How do export a single annotation?

Q:
I would like to export a single annotation, but the only option is to export everything.

A:
Currently, the best way to export a subset of annotations in a document is the following.

fdfdoc = doc.FDFExtract(PDFDoc.e_both) # other options are e_forms_only, e_annots_only, e_annots_only_no_links
fdf = fdfdoc.GetFDF()
annots = fdf.FindObj('Annots')
new_annots = fdfdoc.GetSDFDoc().CreateIndirectArray()
if annots != None and annots.IsArray():
for i in range(annots.Size()):
annot_obj = annots_obj.GetAt(i)
annot = Annot(annot_obj)
# here you have an Annotation object, and you can now decide what to keep/remove
if(this_is_annotation_i_want_to_export)
new_annots.PushBack(annot_obj)

fdf.Put('Annots', new_annots) # replace old annots
# now you can save as FDF binary format, or XFDF XML format
string xfdf_xml = fdf.SaveAsXFDF()

Same code above works for Forms, just use “Fields” key instead of “Annots”. However, note that if there are fields, then there will be corresponding annotations for those fields, so you would want to keep them all together.

An alternative solution, if you just want to export a single annotation to FDF or XFDF is the following pseudo code (this one includes document locking, for the case where you are viewing the document at the time)

myviewer.DocLock()
Obj p_obj = annot_to_export.GetSDFObj().FindObj("P")
annot_to_export.GetSDFObj().Erase("P")
// create temp doc and page
PDFDoc temp
Obj imported_annot_obj = temp.GetSDFDoc().ImportObj(annot_to_export.GetSDFObj(), true)
// put page back and release lock
annot_to_export.GetSDFObj().Put("P", p_obj)
myviewer.DocUnlock()
// create temp page, add, and add imported annot
Page temp_page = temp.PageCreate()
temp.PagePushBack(temp_page)
temp_page.AnnotPushBack(Annot(imported_annot_obj))
fdf = temp.FDFExtract(PDFDoc.e_both)
fdf.GetFDF().FindObj("Annots").GetAt(0).PutNumber("Page", original_page_number - 1) // PDF pages start at 1, but FDF pages start at zero.
xfdf_xml = fdf.SaveAsXFDF()

It might be tempting to re-use the temp PDFDoc object, but without calling temp.Save with e_remove_unused flag, the allocated memory will not get released. It is probably better to allow it to get released.

There is now a new version of PDFDoc.FDFExtract that accepts a list of annotations to export.

For example, in .Net.

FDFDoc PDFDoc.FDFExtract(ArrayList annotations)

https://www.pdftron.com/pdfnet/docs/PDFNet/?topic=html/M_pdftron_PDF_PDFDoc_FDFExtract_2.htm