Import and Export Annotations to XFDF
Aspose.PDF for .NET is a feature rich component when it comes to editing the PDF documents. As we know XFDF is an important aspect of PDF forms manipulation, Aspose.Pdf.Facades namespace in Aspose.PDF for .NET has considered this very well, and have provided methods to import and export annotations data to XFDF files.
PDFAnnotationEditor class contains two methods to work with import and export of annotations to XFDF file. ExportAnnotationsXfdf method provides the functionality to export annotations from a PDF document to XFDF file, while ImportAnnotationFromXfdf method allows you to import annotations from an existing XFDF file. In order to import or export annotations we need to specify the annotation types. We can specify these types in the form of an enumeration and then pass this enumeration as an argument to any of these methods. This way, the annotations of the specified types will only be imported or exported to an XFDF file.
The following code snippet also work with Aspose.PDF.Drawing library.
The following code snippet shows you how to export annotations to an XFDF file:
// The path to the documents directory.
private const string dataDir = "..\\..\\..\\..\\Samples\\";
/// <summary>
/// Importing annotations from XFDF file
/// XML Forms Data Format (XFDF) file created by Adobe Acrobat, a PDF authoring application;
/// stores descriptions of page form elements and their values, such as the names and values for
/// text fields; used for saving form data that can be imported into a PDF document.
/// You can import annotation data from the XFDF file to PDF using
/// the ImportAnnotationsFromXfdf method in PdfAnnotationEditor class.
/// </summary>
// Create PdfAnnotationEditor object
PdfAnnotationEditor AnnotationEditor = new PdfAnnotationEditor();
// Bind PDF document to the Annotation Editor
AnnotationEditor.BindPdf(dataDir + "AnnotationDemo1.pdf");
// Export annotations
var fileStream = File.OpenWrite(dataDir + "exportannotations.xfdf");
var annotType = new AnnotationType[] { AnnotationType.Line, AnnotationType.Square };
AnnotationEditor.ExportAnnotationsXfdf(fileStream, 1, 1, annotType);
fileStream.Flush();
fileStream.Close();
The next code snippet describes how import annotations to an XFDF file:
public static void ImportAnnotationXFDF()
{
// Create PdfAnnotationEditor object
PdfAnnotationEditor AnnotationEditor = new PdfAnnotationEditor();
// Create a new PDF document
var document = new Document();
document.Pages.Add();
AnnotationEditor.BindPdf(document);
var exportFileName = dataDir + "exportannotations.xfdf";
if (!File.Exists(exportFileName))
{
ExportAnnotationXFDF();
}
// Import annotation
AnnotationEditor.ImportAnnotationsFromXfdf(exportFileName);
// Save output PDF
document.Save(dataDir + "AnnotationDemo2.pdf");
}
Yet another way to export/import annotations at once
In the code below an ImportAnnotations method allows import annotations directly from another PDF doc.
/// <summary>
/// ImportAnnotations method allow import annotations directly from another PDF doc
/// </summary>
public static void ImportAnnotationFromPDF()
{
// Create PdfAnnotationEditor object
PdfAnnotationEditor AnnotationEditor = new PdfAnnotationEditor();
// Create a new PDF document
var document = new Document();
document.Pages.Add();
AnnotationEditor.BindPdf(document);
var exportFileName = dataDir + "exportannotations.xfdf";
if (!File.Exists(exportFileName))
{
ExportAnnotationXFDF();
}
// Annotation Editor allows import annotations from several PDF documents,
// but in this example, we use only one.
AnnotationEditor.ImportAnnotations(new[] { dataDir + "AnnotationDemo1.pdf" });
// Save output PDF
document.Save(dataDir + "AnnotationDemo3.pdf");
}