AcroFormの抽出 - C#でPDFからフォームデータを抽出

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

フォームからデータを抽出

PDFドキュメントのすべてのフィールドから値を取得

PDFドキュメントのすべてのフィールドから値を取得するには、すべてのフォームフィールドをナビゲートし、次にValueプロパティを使用して値を取得する必要があります。Formコレクションから各フィールドを取得し、基本フィールドタイプであるFieldにアクセスして、そのValueプロパティにアクセスします。

次のC#コードスニペットは、PDFドキュメントからすべてのフィールドの値を取得する方法を示しています。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetValuesFromAllFields.pdf"))
    {
        // Get values from all fields
        foreach (Aspose.Pdf.Forms.Field formField in document.Form)
        {
            Console.WriteLine("Field Name : {0} ", formField.PartialName);
            Console.WriteLine("Value : {0} ", formField.Value);
        }
    }
}

PDFドキュメントの個々のフィールドから値を取得

フォームフィールドのValueプロパティを使用すると、特定のフィールドの値を取得できます。値を取得するには、DocumentオブジェクトのFormコレクションからフォームフィールドを取得します。このC#の例では、TextBoxFieldを選択し、そのValueプロパティを使用して値を取得します。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetValueFromField.pdf"))
    {
        // Get a field
        if (document.Form["textbox1"] is Aspose.Pdf.Forms.TextBoxField textBoxField)
        {
            // Get field value
            Console.WriteLine("PartialName : {0} ", textBoxField.PartialName);
            Console.WriteLine("Value : {0} ", textBoxField.Value);
        }
    }
}

送信ボタンのURLを取得するには、次のコード行を使用します。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetValueFromField.pdf"))
    {
        // Get the SubmitFormAction from the form field
        if (document.Form[1].OnActivated is Aspose.Pdf.Annotations.SubmitFormAction act)
        {
            // Output the URL of the SubmitFormAction
            Console.WriteLine(act.Url.Name);
        }
    }
}

PDFファイルの特定の領域からフォームフィールドを取得

場合によっては、ドキュメント内のフォームフィールドの位置を知っているが、その名前を知らないことがあります。たとえば、印刷されたフォームのスキーマしかない場合です。Aspose.PDF for .NETを使用すれば、これは問題ではありません。PDFファイルの特定の領域にどのフィールドがあるかを確認できます。特定の領域からフォームフィールドを取得するには:

  1. Documentオブジェクトを使用してPDFファイルを開きます。
  2. ドキュメントのFormsコレクションからフォームを取得します。
  3. 矩形領域を指定し、それをFormオブジェクトのGetFieldsInRectメソッドに渡します。Fieldsコレクションが返されます。
  4. これを使用してフィールドを操作します。

次のC#コードスニペットは、PDFファイルの特定の矩形領域にあるフォームフィールドを取得する方法を示しています。

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

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "GetFieldsFromRegion.pdf"))
    {
        // Create rectangle object to get fields in that area
        var rectangle = new Aspose.Pdf.Rectangle(35, 30, 500, 500);

        // Get the PDF form
        var form = document.Form;

        // Get fields in the rectangular area
        var fields = form.GetFieldsInRect(rectangle);

        // Display Field names and values
        foreach (var field in fields)
        {
            // Display image placement properties for all placements
            Console.Out.WriteLine("Field Name: " + field.FullName + "-" + "Field Value: " + field.Value);
        }
    }
}