导入和导出注释到 XFDF

XFDF 代表 XML 表单数据格式。它是一种基于 XML 的文件格式。该文件格式用于表示 PDF 表单中包含的表单数据或注释。XFDF 可以用于许多不同的目的,但在我们的案例中,它可以用于将表单数据或注释发送或接收至其他计算机或服务器等,或者可以用于归档表单数据或注释。在本文中,我们将看到 Aspose.Pdf.Facades 如何考虑这一概念,以及我们如何将注释数据导入和导出到 XFDF 文件。

导入和导出注释到 XFDF

Aspose.PDF for .NET 是一个功能丰富的组件,涉及 PDF 文档的编辑。正如我们所知,XFDF 是 PDF 表单操作的重要方面,Aspose.Pdf.Facades 命名空间Aspose.PDF for .NET 中对此进行了很好的考虑,并提供了将注释数据导入和导出到 XFDF 文件的方法。

PDFAnnotationEditor 类包含两个方法,用于处理注释的导入和导出到 XFDF 文件。ExportAnnotationsXfdf 方法提供了将注释从 PDF 文档导出到 XFDF 文件的功能,而 ImportAnnotationFromXfdf 方法允许您从现有的 XFDF 文件导入注释。为了导入或导出注释,我们需要指定注释类型。我们可以以枚举的形式指定这些类型,然后将该枚举作为参数传递给这些方法中的任何一个。这样,只有指定类型的注释才会被导入或导出到 XFDF 文件。

以下代码片段展示了如何将注释导入到 XFDF 文件:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportAnnotation()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdfFacades_Annotations();

    // Sources of PDF with annotations           
    var sources = new string[] { dataDir + "ImportAnnotations.pdf" };
            
    using (var annotationEditor = new Aspose.Pdf.Facades.PdfAnnotationEditor())
    {
        // Bind PDF document
        annotationEditor.BindPdf(dataDir + "input.pdf");
        annotationEditor.ImportAnnotations(sources);
        // Save PDF document
        annotationEditor.Save(dataDir + "ImportAnnotations_out.pdf");
    }
}

下一个代码片段描述了如何将注释导入/导出到 XFDF 文件:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportExportXFDF01()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdfFacades_Annotations();

    using (var annotationEditor = new Aspose.Pdf.Facades.PdfAnnotationEditor())
    {
        // Bind PDF document
        annotationEditor.BindPdf(dataDir + "ExportAnnotations.pdf");
        using (FileStream xmlOutputStream = File.OpenWrite(dataDir + "exportannotations_out.xfdf"))
        {
            annotationEditor.ExportAnnotationsToXfdf(xmlOutputStream);
        }

        // Create PDF document
        using (var document = new Aspose.Pdf.Document())
        {
            // Add page
            document.Pages.Add();
            // Bind PDF document
            annotationEditor.BindPdf(document);
            annotationEditor.ImportAnnotationsFromXfdf(File.OpenRead(dataDir + "exportannotations_out.xfdf"));
            // Save PDF document
            annotationEditor.Save(dataDir + "ImportedAnnotation_out.pdf");
        }
    }
}

这样,只有指定类型的注释才会被导入或导出到 XFDF 文件。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ImportExportXFDF02()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdfFacades_Annotations();
    
    using (var annotationEditor = new Aspose.Pdf.Facades.PdfAnnotationEditor())
    {
        // Bind PDF document
        annotationEditor.BindPdf(dataDir + "ExportAnnotations.pdf");

        // Export annotations
        using (FileStream xmlOutputStream = File.OpenWrite(dataDir + "exportannotations_out.xfdf"))
        {
            var annotationTypes = new[] {AnnotationType.FreeText, AnnotationType.Text};
            annotationEditor.ExportAnnotationsXfdf(xmlOutputStream, 1, 5, annotationTypes);
        }

        // Import annotations
        using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
        {
            // Add page
            document.Pages.Add();
            // Bind PDF document
            annotationEditor.BindPdf(document);
            annotationEditor.ImportAnnotationsFromXfdf(File.OpenRead(dataDir + "annotations.xfdf"));
            // Save PDF document
            annotationEditor.Save(dataDir + "ImportedAnnotation_XFDF02_out.pdf");
        }
    }
}