How do I adjust the pages of annotations in a FDF file?

Question:

Before merging annotations in a FDF object into a PDF object, I want to adjust the pages that some annotations are on. How do I check the annotations type and possibly change the page?

Answer:

The following code will get you started with this. Note that in a FDF document, the pages start at index zero, while for a PDFDoc the index is one.

//given FDFDoc fdfdoc instance
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);
Annot.Type annotType = annot.GetType();
int page_index = annotobj.FindObj("Page").GetNumber();
// change the page number to page 10 for example
annotobj.PutNumber("Page", 9)' // zero indexed, so index 9 is the tenth page in the PDF
}