在PDF中装饰表单字段

在现有PDF文件中装饰特定表单字段

DecorateField方法存在于FormEditor类中,允许您在PDF文件中装饰特定的表单字段。如果您想装饰特定字段,则需要将字段名称传递给此方法。然而,在调用此方法之前,您需要创建FormEditorFormFieldFacade类的对象。您还需要将FormFieldFacade对象分配给FormEditor对象的Facade属性。之后,您可以设置FormFieldFacade对象提供的任何属性。一旦设置了属性,您可以调用DecorateField方法,最后使用FormEditor类的Save方法保存更新后的PDF。以下代码片段向您展示了如何装饰特定的表单字段。

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

    // Create an instance of FormEditor to manipulate form fields
    using (var editor = new Aspose.Pdf.Facades.FormEditor())
    {
        // Bind PDF document
        editor.BindPdf(dataDir + "Sample-Form-01.pdf");

        // Create a FormFieldFacade object to define decoration properties for the field
        var cityDecoration = new Aspose.Pdf.Facades.FormFieldFacade
        {
            // Set the font style to Courier
            Font = Aspose.Pdf.Facades.FontStyle.Courier,
            // Set the font size to 12
            FontSize = 12,
            // Set the border color to black
            BorderColor = System.Drawing.Color.Black,
            // Set the border width to 2
            BorderWidth = 2
        };

        // Assign the decoration facade to the FormEditor
        editor.Facade = cityDecoration;

        // Apply the decoration to the field named "City"
        editor.DecorateField("City");

        // Save PDF document
        editor.Save(dataDir + "Sample-Form-02.pdf");
    }
}

在现有PDF文件中装饰特定类型的所有字段

DecorateField方法允许您一次性装饰PDF文件中特定类型的所有表单字段。如果您想装饰特定类型的所有字段,则需要将字段类型传递给此方法。然而,在调用此方法之前,您需要创建FormEditorFormFieldFacade类的对象。您还需要将FormFieldFacade对象分配给FormEditor对象的Facade属性。之后,您可以设置FormFieldFacade对象提供的任何属性。一旦设置了属性,您可以调用DecorateField方法,最后使用FormEditor类的Save方法保存更新后的PDF。以下代码片段向您展示了如何装饰特定类型的所有字段。

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

    // Create an instance of FormEditor to manipulate form fields
    using (var editor = new Aspose.Pdf.Facades.FormEditor())
    {
        // Bind PDF document
        editor.BindPdf(dataDir + "Sample-Form-01.pdf");

        // Create a FormFieldFacade object to define alignment properties for text fields
        var textFieldDecoration = new Aspose.Pdf.Facades.FormFieldFacade
        {
            // Set text alignment to center
            Alignment = Aspose.Pdf.Facades.FormFieldFacade.AlignCenter
        };

        // Assign the decoration facade to the FormEditor
        editor.Facade = textFieldDecoration;

        // Apply the alignment decoration to all text fields in the PDF
        editor.DecorateField(Aspose.Pdf.Facades.FieldType.Text);

        // Save PDF document
        editor.Save(dataDir + "Sample-Form-01-align-text.pdf");
    }
}