FormEditorクラスの機能を探る

Contents
[ ]

実装の詳細

開発者は、PDFドキュメントに新しいフォームやフォームフィールドを追加するだけでなく、既存のフィールドを編集するためにもAspose.Pdf.Facades名前空間を使用できます。この記事の範囲は、フォーム編集に関するAspose.PDF for .NETの機能に限定されています。

FormEditorは、開発者がフォームフィールドを編集するためのメソッドとプロパティのほとんどを含むクラスです。新しいフィールドを追加するだけでなく、既存のフィールドを削除したり、フィールドを別の位置に移動したり、フィールド名や属性を変更したりすることもできます。このクラスが提供する機能のリストは非常に包括的であり、このクラスを使用してフォームフィールドを扱うのは非常に簡単です。

これらのメソッドは、フィールドを操作するために使用されるものと、これらのフィールドの新しい属性を設定するために使用されるものの2つの主要なカテゴリに分けることができます。最初のカテゴリにグループ化できるメソッドには、AddField、AddListItem、RemoveListItem、CopyInnerField、CopyOuterField、DelListItem、MoveField、RemoveField、RenameFieldなどがあります。2番目のカテゴリには、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");                    
        }
    }
}