使用 XFA 表单

表单类提供了处理静态 AcroForm 的能力,您可以使用 Form 类的 GetFieldFacade(..) 方法获取特定字段实例。然而,XFA 字段无法通过 Form.GetFieldFacade(..) 方法访问。相反,请使用 Document.Form.XFA 来获取/设置字段值并操作 XFA 字段模板(设置字段属性)。

以下代码片段也可以与 Aspose.PDF.Drawing 库一起使用。

填写 XFA 字段

以下代码片段演示了如何填写 XFA 表单中的字段。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "FillXFAFields.pdf"))
    {
        // Get names of XFA form fields
        var names = document.Form.XFA.FieldNames;

        // Set field values
        if (names.Length > 0)
        {
            document.Form.XFA[names[0]] = "Field 0";
        }
        if (names.Length > 1)
        {
            document.Form.XFA[names[1]] = "Field 1";
        }

        // Save PDF document
        document.Save(dataDir + "FilledXfa_out.pdf");
    }
}

将 XFA 转换为 Acroform

动态表单基于一种称为 XFA 的 XML 规范,即“XML 表单架构”。关于表单的信息(就 PDF 而言)非常模糊——它指定了字段的存在、属性和 JavaScript 事件,但没有指定任何渲染。

目前,PDF 支持两种不同的方法来集成数据和 PDF 表单:

  • AcroForms(也称为 Acrobat 表单),在 PDF 1.2 格式规范中引入并包含。
  • Adobe XML 表单架构(XFA)表单,在 PDF 1.5 格式规范中作为可选功能引入(XFA 规范未包含在 PDF 规范中,仅作引用)。

我们无法提取或操作 XFA 表单的页面,因为表单内容是在运行时(在查看 XFA 表单时)在尝试显示或渲染 XFA 表单的应用程序中生成的。Aspose.PDF 具有一个功能,允许开发人员将 XFA 表单转换为标准 AcroForms。

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

    // Load dynamic XFA form
    using (var document = new Aspose.Pdf.Document(dataDir + "DynamicXFAToAcroForm.pdf"))
    {
        // Set the form fields type as standard AcroForm
        document.Form.Type = Aspose.Pdf.Forms.FormType.Standard;

        // Save PDF document
        document.Save(dataDir + "StandardAcroForm_out.pdf");
    }
}

获取 XFA 字段属性

要访问字段属性,首先使用 Document.Form.XFA.Template 访问字段模板。以下代码片段展示了获取 XFA 表单字段的 X 和 Y 坐标的步骤。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetXFAProperties.pdf"))
    {
        // Get names of XFA form fields
        var names = document.Form.XFA.FieldNames;

        // Set field values
        if (names.Length > 0)
        {
            document.Form.XFA[names[0]] = "Field 0";
        }
        if (names.Length > 1)
        {
            document.Form.XFA[names[1]] = "Field 1";
        }

        // Get field position
        if (names.Length > 0)
        {
            Console.WriteLine(document.Form.XFA.GetFieldTemplate(names[0]).Attributes["x"].Value);
            Console.WriteLine(document.Form.XFA.GetFieldTemplate(names[0]).Attributes["y"].Value);
        }

        // Save PDF document
        document.Save(dataDir + "FilledXfa_out.pdf");
    }
}