Importer et exporter des annotations vers XFDF

Aspose.PDF for .NET est un composant riche en fonctionnalités lorsqu’il s’agit d’éditer des documents PDF. Comme nous le savons, XFDF est un aspect important de la manipulation des formulaires PDF, l’espace de noms Aspose.Pdf.Facades dans Aspose.PDF for .NET a bien pris cela en compte et a fourni des méthodes pour importer et exporter des données d’annotations vers des fichiers XFDF.

La classe PDFAnnotationEditor contient deux méthodes pour travailler avec l’importation et l’exportation d’annotations vers un fichier XFDF. La méthode ExportAnnotationsXfdf fournit la fonctionnalité pour exporter des annotations d’un document PDF vers un fichier XFDF, tandis que la méthode ImportAnnotationFromXfdf vous permet d’importer des annotations à partir d’un fichier XFDF existant. Pour importer ou exporter des annotations, nous devons spécifier les types d’annotations. Nous pouvons spécifier ces types sous forme d’énumération et ensuite passer cette énumération comme argument à l’une de ces méthodes. De cette manière, seules les annotations des types spécifiés seront importées ou exportées vers un fichier XFDF.

Le code suivant fonctionne également avec la bibliothèque Aspose.PDF.Drawing.

Le code suivant vous montre comment exporter des annotations vers un fichier XFDF :

// 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();
        }
    }
}

Le code suivant décrit comment importer des annotations à partir d’un fichier XFDF :

// 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");
        }
    }
}

Une autre façon d’exporter/importer des annotations en une seule fois

Dans le code ci-dessous, une méthode ImportAnnotations permet d’importer des annotations directement à partir d’un autre document PDF.

// 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");
        }
    }
}