PDFNet makes sharing annotations easy by offering an simple way to import and export annotations. The annotations are saved in a standardized XML format, XFDF. This makes the annotations easy to share across PDFTron’s product line, as well as third-party applications such as Acrobat. The XML annotation files are typically much smaller than a PDF file, making it an ideal format to use when transmitting over a network. The XML format is also well suited for conflict resolution algorithms. This tutorial will demonstrate how to use PDFNet’s APIs to export and import annotations saved in the XFDF format.
The following code snippet shows how to extract annotation data from a PDF, and save it as an XFDF XML file. It is written in Objective-C, but the same API applies for all supported languages.
// Create a new PDF Doc PDFDoc *pdfDoc = [[PDFDoc alloc] initWithFilepath: @"file_with_annotations.pdf"]; [pdfDoc InitSecurityHandler]; // Extract the annotations FDFDoc *doc_annots = [pdfDoc FDFExtract: e_annots_only]; // This is an optional field that specifies the PDF docuemnt that the FDF file // was exported from or is intended to be imported into [doc_annots SetPDFFileName: @"file_with_annotations.pdf"]; // Export the annotations in XFDF format [doc_annots SaveAsXFDF: @"annotations.xfdf"];
// Create a new PDF Doc PDFDoc in_doc = new PDFDoc("file_with_annotations.pdf"); in_doc.InitSecurityHandler(); // Extract the annotations FDFDoc doc_annots = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_annots_only); // This is an optional field that specifies the PDF docuemnt that the FDF file // was exported from or is intended to be imported into doc_annots.SetPdfFileName("file_with_annotations.pdf"); // Export the annotations in XFDF format doc_annots.SaveAsXFDF(output_path + "annotations.xfdf");
The following code snippet shows how to import annotations data from an XFDF XML file, and merge them into a PDF file.
// load XFDF annotations FDFDoc *fdfDoc = [FDFDoc CreateFromXFDF: @"annotations.xfdf"]; // load PDF with which to merge the annotations PDFDoc *pdfDoc = [[PDFDoc alloc] initWithFilepath: @"file_without_annotations.pdf"]; [pdfDoc InitSecurityHandler]; // merge in the annotations [pdfDoc FDFMerge: fdfDoc]; // save the PDF with the annotations [pdfDoc SaveToFile: @"file_with_annotations.pdf" flags: e_remove_unused]; [pdfDoc Close];
// load XFDF annotations FDFDoc fdfDoc = new FDFDoc(FDFDoc.CreateFromXFDF("annotations.xfdf")); // load PDF with which to merge the annotations PDFDoc pdfDoc = new PDFDoc("file_without_annotations.pdf"); in_doc.InitSecurityHandler(); // merge in the annotations pdfDoc.FDFMerge(fdfDoc); // save the PDF with the annotations pdfDoc.Save("pdfDoc_with_annotations.pdf"); pdfDoc.Close();
XFDF & WebViewer
A nice thing about XFDF is that once annotations are exported to XML they can be easily loaded across all platforms supported by PDFNet (Android, iOS, WinRT, Linux, Mac, etc). Besides PDFNet SDK, the same annotations can be loaded & saved in the WebViewer via Core.AnnotationManager.ImportAnnotations.
The net effect is that a single XML data representation is consistently used across all mobile & desktop platforms as well as the Web.
For more information about XFDF in conjunction with the WebViewer, as well as sample code showing how to implement XFDF processing on the server side, please see section “Processing Annotations and Forms” in WebViewer Developer Guide.