Here is sample Java code allowing to export a given set of annotations contained in a PDF document to an FDF file in Java.

This sample is using Qoppa’s PDF component jPDFNotes.

The method used is MutableDocument.exportAnnotsAsFDF:

void exportAnnotsAsFDF(String fileName,
                     Set annotSet)
                       throws PDFException

Exports annotations in FDF format.

Parameters:
    fileName - The name of the file to export the data to.
    annotSet - The set of annotations to export. If annotSet is not null, only the annotations contained in the set will be exported. Otherwise, all annotations will be exported.

In this sample, we are exporting all the “FreeText” annotations. These annotations are also sometimes known as text box annotations.

// get the loaded PDF document
IPDFDocument iDoc = m_NotesBean.getDocument();
// create a new annotations set 
Set<Annotation> exportSet = new HashSet();
// loop through the PDF document pages            
for (int i = 0; i < iDoc.getPageCount(); i++)
{
 // get the next page
  IPDFPage iPage = iDoc.getIPage(i);
  for (int j = 0; j < iPage.getAnnotations().size(); j++)
  {
    if (iPage.getAnnotations().get(j) instanceof FreeText)
    {
      // add any FreeText annotation to the annotation set
      exportSet.add(iPage.getAnnotations().get(j));
    }
  }
}
// export annotations to an FDF file
m_NotesBean.getMutableDocument().exportAnnotsAsFDF("fileName", exportSet);

To export all annotations contained in the PDF document, simply pass null for the set parameter.

// export all annotations to an FDF file
m_NotesBean.getMutableDocument().exportAnnotsAsFDF("fileName", null);