AcroFormの変更

次のコードスニペットは、Aspose.PDF.Drawingライブラリでも動作します。

フィールド制限の取得または設定

FormEditorクラスのSetFieldLimit(field, limit)メソッドを使用すると、フィールド制限、つまりフィールドに入力できる最大文字数を設定できます。

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

    // Create FormEditor instance
    using (var form = new Aspose.Pdf.Facades.FormEditor())
    {
        // Bind PDF document
        form.BindPdf(dataDir + "input.pdf");

        // Set field limit for "textbox1"
        form.SetFieldLimit("textbox1", 15);

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

同様に、Aspose.PDFにはDOMアプローチを使用してフィールド制限を取得するメソッドがあります。次のコードスニペットはその手順を示しています。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "FieldLimit.pdf"))
    {
        // Get the field and its maximum limit
        if (document.Form["textbox1"] is Aspose.Pdf.Forms.TextBoxField textBoxField)
        {
            Console.WriteLine("Limit: " + textBoxField.MaxLen);
        }
    }
}

次のコードスニペットを使用して、Aspose.Pdf.Facades名前空間を使用して同じ値を取得することもできます。

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

    // Create Form instance
    using (var form = new Aspose.Pdf.Facades.Form())
    {
        // Bind PDF document
        form.BindPdf(dataDir + "FieldLimit.pdf");

        // Get the field limit for "textbox1"
        Console.WriteLine("Limit: " + form.GetFieldLimit("textbox1"));
    }
}

フォームフィールドのカスタムフォントを設定

Adobe PDFファイルのフォームフィールドは、特定のデフォルトフォントを使用するように設定できます。Aspose.PDFの初期バージョンでは、14のデフォルトフォントのみがサポートされていました。後のリリースでは、開発者が任意のフォントを適用できるようになりました。フォームフィールドに使用されるデフォルトフォントを設定および更新するには、DefaultAppearance(Font font, double size, Color color)クラスを使用します。このクラスはAspose.Pdf.InteractiveFeatures名前空間の下にあります。このオブジェクトを使用するには、FieldクラスのDefaultAppearanceプロパティを使用します。

次のコードスニペットは、PDFフォームフィールドのデフォルトフォントを設定する方法を示しています。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "FormFieldFont14.pdf"))
    {
        // Get particular form field from document
        if (document.Form["textbox1"] is Aspose.Pdf.Forms.Field field)
        {
            // Create font object
            var font = Aspose.Pdf.Text.FontRepository.FindFont("ComicSansMS");

            // Set the font information for form field
            field.DefaultAppearance = new Aspose.Pdf.Annotations.DefaultAppearance(font, 10, System.Drawing.Color.Black);
        }

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

既存のフォームにフィールドを追加/削除

すべてのフォームフィールドは、DocumentオブジェクトのFormコレクションに含まれています。このコレクションは、Deleteメソッドを含むフォームフィールドを管理するためのさまざまなメソッドを提供します。特定のフィールドを削除したい場合は、Deleteメソッドにフィールド名をパラメータとして渡し、更新されたPDFドキュメントを保存します。次のコードスニペットは、PDFドキュメントから特定のフィールドを削除する方法を示しています。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "DeleteFormField.pdf"))
    {
        // Delete a particular field by name
        document.Form.Delete("textbox1");

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