Import and Export
Foxit PDF SDK for Android supports importing and exporting annotation data via FDF and XFDF for exchange between documents and systems. The SDK also provides annotation flattening to merge annotations into page content permanently.
FDF and XFDF Formats
| Format | Constant | Description |
|---|---|---|
| FDF | FDFDoc.e_FDF | Forms Data Format (PDF binary format) |
| XFDF | FDFDoc.e_XFDF | XML FDF for parsing and integration |
FDFDoc core methods:
| Method | Description |
|---|---|
FDFDoc(int type) | Create empty FDF (FDFDoc.e_FDF or FDFDoc.e_XFDF) |
FDFDoc(String path) | Load from file path |
getType() | Format type |
isEmpty() | Whether empty |
saveAs(String filePath) | Save to file |
Export Annotations
Export All Annotations
java
import com.foxit.sdk.fdf.FDFDoc;
import com.foxit.sdk.pdf.PDFDoc;
PDFDoc doc = new PDFDoc("path/to/Sample.pdf");
doc.load(null);
FDFDoc fdfDoc = new FDFDoc(FDFDoc.e_XFDF);
doc.exportToFDF(fdfDoc, PDFDoc.e_Annots, null);
fdfDoc.saveAs("path/to/annotations.xfdf");NOTE
The second parameter to exportToFDF() is export type; PDFDoc.e_Annots exports annotations. The third parameter is page range (Range); null exports all pages.
Export a Single Annotation
java
import com.foxit.sdk.fdf.FDFDoc;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.annots.Annot;
PDFDoc doc = new PDFDoc("path/to/Sample.pdf");
doc.load(null);
PDFPage page = doc.getPage(0);
Annot annot = page.getAnnot(0);
FDFDoc fdfDoc = new FDFDoc(FDFDoc.e_FDF);
doc.exportAnnotToFDF(annot, fdfDoc);
fdfDoc.saveAs("path/to/single_annot.fdf");Import Annotations
java
import com.foxit.sdk.fdf.FDFDoc;
import com.foxit.sdk.pdf.PDFDoc;
PDFDoc doc = new PDFDoc("path/to/Sample.pdf");
doc.load(null);
FDFDoc fdfDoc = new FDFDoc("path/to/annotations.xfdf");
doc.importFromFDF(fdfDoc, PDFDoc.e_Annots, null);
doc.saveAs("path/to/output.pdf", PDFDoc.e_SaveFlagNormal);Flatten Annotations
Flattening merges annotation appearance into page content and removes the annotation objects.
Flatten All Annotations on a Page
java
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
PDFDoc doc = new PDFDoc("path/to/Sample.pdf");
doc.load(null);
PDFPage page = doc.getPage(0);
page.flattenAnnots(true);
doc.saveAs("path/to/flattened.pdf", PDFDoc.e_SaveFlagNormal);NOTE
flattenAnnots(true) flattens all annotations; false flattens form fields only.
Flatten a Single Annotation
java
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.annots.Annot;
PDFPage page = doc.getPage(0);
Annot annot = page.getAnnot(0);
if (annot != null && !annot.isEmpty()) {
page.flattenAnnot(annot);
}Typical Use Cases
| Scenario | Approach |
|---|---|
| Collaborative review — share annotations | Export XFDF, distribute, members import locally |
| Server-side annotation processing | XFDF (XML) for parsing and storage |
| Final publish — annotations not editable | Export backup, then flatten all |
| Archival — consistent rendering | Flatten annotations and form fields |