PDFのフォームフィールドを装飾する

既存のPDFファイル内の特定のフォームフィールドを装飾する

FormEditorクラスに存在するDecorateFieldメソッドを使用すると、PDFファイル内の特定のフォームフィールドを装飾できます。特定のフィールドを装飾したい場合は、このメソッドにフィールド名を渡す必要があります。ただし、このメソッドを呼び出す前に、FormEditorおよびFormFieldFacadeクラスのオブジェクトを作成する必要があります。また、FormEditorオブジェクトのFacadeプロパティにFormFieldFacadeオブジェクトを割り当てる必要があります。その後、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ファイル内の特定のタイプのすべてのフォームフィールドを一度に装飾できます。特定のタイプのすべてのフィールドを装飾したい場合は、このメソッドにフィールドタイプを渡す必要があります。ただし、このメソッドを呼び出す前に、FormEditorおよびFormFieldFacadeクラスのオブジェクトを作成する必要があります。また、FormEditorオブジェクトのFacadeプロパティにFormFieldFacadeオブジェクトを割り当てる必要があります。その後、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");
    }
}