探索 FormEditor 类的功能

Contents
[ ]

实现细节

开发人员可以使用 Aspose.Pdf.Facades 命名空间 不仅可以在 PDF 文档中添加新表单和表单字段,还可以编辑现有字段。本文的范围仅限于处理表单编辑的 Aspose.PDF for .NET 的功能。

FormEditor 是包含大多数方法和属性的类,允许开发人员编辑表单字段。您不仅可以添加新字段,还可以删除现有字段,将一个字段移动到另一个位置,更改字段名称或属性等。此类提供的功能列表相当全面,使用此类处理表单字段非常简单。

这些方法可以分为两个主要类别,其中一个用于操作字段,另一个用于设置这些字段的新属性。我们可以将第一类方法分组为 AddField、AddListItem、RemoveListItem、CopyInnerField、CopyOuterField、DelListItem、MoveField、RemoveField 和 RenameField 等。在第二类方法中可以包括 SetFieldAlignment、SetFieldAppearance、SetFieldAttribute、SetFieldLimit、SetFieldScript。以下代码片段向您展示了 FormEditor 类的一些方法的工作

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "inFile.pdf"))
    {
        // Create instance of FormEditor
        using (var editor = new Aspose.Pdf.Facades.FormEditor(document))
        {
            // Add field in the PDF file
            editor.AddField(Aspose.Pdf.Facades.FieldType.Text, "field1", 1, 300, 500, 350, 525);

            // Add List field in PDF file
            editor.AddField(Aspose.Pdf.Facades.FieldType.ListBox, "field2", 1, 300, 200, 350, 225);

            // Add list items
            editor.AddListItem("field2", "item 1");
            editor.AddListItem("field2", "item 2");

            // Add submit button
            editor.AddSubmitBtn("submitbutton", 1, "Submit Form", "http:// Testwebsite.com/testpage", 200, 200, 250, 225);

            // Delete list item
                editor.DelListItem("field2", "item 1");

            // Move field to new position
            editor.MoveField("field1", 10, 10, 50, 50);

            // Remove existing field from the PDF
            editor.RemoveField("field1");

            // Rename an existing field
            editor.RenameField("field1", "newfieldname");

            // Reset all visual attributes to empty value
            editor.ResetFacade();

            // Set the alignment style of a text field
            editor.SetFieldAlignment("field1", Aspose.Pdf.Facades.FormFieldFacade.AlignLeft);

            // Set appearance of the field
            editor.SetFieldAppearance("field1", Aspose.Pdf.Annotations.AnnotationFlags.NoRotate);

            // Set field attributes i.e. ReadOnly, Required
            editor.SetFieldAttribute("field1", Aspose.Pdf.Facades.PropertyFlag.ReadOnly);

            // Set field limit
            editor.SetFieldLimit("field1", 25);

            // Save modifications in the output file
            editor.Save(dataDir + "FormEditorFeatures2_out.pdf");                    
        }
    }
}