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:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExportAnnotationsToXfdf()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Create PdfAnnotationEditor object
using (var annotationEditor = new Aspose.Pdf.Facades.PdfAnnotationEditor())
{
// Bind PDF document
annotationEditor.BindPdf(dataDir + "AnnotationDemo1.pdf");
// Define the annotation types to export
var annotType = new Aspose.Pdf.Annotations.AnnotationType[] { Aspose.Pdf.Annotations.AnnotationType.Line, Aspose.Pdf.Annotations.AnnotationType.Square };
// Export annotations to XFDF file
using (var fileStream = File.OpenWrite(dataDir + "exportannotations_out.xfdf"))
{
annotationEditor.ExportAnnotationsXfdf(fileStream, 1, 1, annotType);
fileStream.Flush();
}
}
}
The next code snippet describes how import annotations from an XFDF file:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportAnnotationFromXfdf()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Create PdfAnnotationEditor object
using (var annotationEditor = new Aspose.Pdf.Facades.PdfAnnotationEditor())
{
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Bind PDF document
annotationEditor.BindPdf(document);
// Define the export file name
var exportFileName = dataDir + "exportannotations.xfdf";
// Import annotations from the XFDF file
annotationEditor.ImportAnnotationsFromXfdf(exportFileName);
// Save PDF document
document.Save(dataDir + "ImportAnnotationFromXfdf_out.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.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportAnnotationFromPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Annotations();
// Open PDF document
using (var documentFrom = new Aspose.Pdf.Document(dataDir + "some_doc.pdf"))
{
// Create PDF document
using (var documentTo = new Aspose.Pdf.Document())
{
// Add page
var page = documentTo.Pages.Add();
// Export/import
using (var ms = new MemoryStream())
{
documentFrom.ExportAnnotationsToXfdf(ms);
documentTo.ImportAnnotationsFromXfdf(ms);
}
// Save PDF document
documentTo.Save(dataDir + "AnnotationDemo3_out.pdf");
}
}
}