フッターコンテンツにスキップ
IRONOCRの使い方

C# PDF フォームフィールドを読む:プログラムでフォームデータを抽出する

PDFフォームを扱うことは、開発者にとって非常に困難なことがあります。 求人の応募処理、アンケートの回答、保険請求の処理に関わらず、手動でフォームデータをコピーするのは非常に時間がかかり、間違いが生じやすいものです。 IronPDFを使用すると、そのような面倒な作業を省略し、コードを数行書くだけでPDFドキュメント内のインタラクティブフォームフィールドから値を取得できます。 以前は時間がかかっていた作業を数秒でできます。

この記事では、C#のフォームオブジェクトを使用して簡単なフォームのすべてのフィールドを取得する方法を紹介します。 サンプルコードでは、各フィールドをループしてその値を抽出する方法を示しています。 それは簡単で、複雑なPDFビューアと戦ったり隠れた書式の問題に対処する必要はありません。

IronPDFを始めよう

PDFフォームフィールド抽出のためのIronPDFのセットアップには最小限の構成が必要です。 NuGetパッケージマネージャーを使用してライブラリをインストールしてください:

Install-Package IronPDF

またはVisual StudioのパッケージマネージャーUIを通して。 IronPdfはWindows、Linux、macOS、Dockerコンテナをサポートし、様々なデプロイシナリオに対応します。 詳細な設定方法については、IronPDFドキュメントを参照してください。

IronPDFでPDFフォームデータを読み取る

次のコードは、IronPDFを使用して既存のPDFファイルからすべてのフィールドを読み取る方法を示しています。

using IronPdf;
using System;

class Program
{
    static void Main(string[] args)
    {
        // Load the PDF document containing interactive form fields
        PdfDocument pdf = PdfDocument.FromFile("application_form.pdf");
        // Access the form object and iterate through all fields
        var form = pdf.Form;
        foreach (var field in form)
        {
            Console.WriteLine($"Field Name: {field.Name}");
            Console.WriteLine($"Field Value: {field.Value}");
            Console.WriteLine($"Field Type: {field.GetType().Name}");
            Console.WriteLine("---");
        }
    }
}
using IronPdf;
using System;

class Program
{
    static void Main(string[] args)
    {
        // Load the PDF document containing interactive form fields
        PdfDocument pdf = PdfDocument.FromFile("application_form.pdf");
        // Access the form object and iterate through all fields
        var form = pdf.Form;
        foreach (var field in form)
        {
            Console.WriteLine($"Field Name: {field.Name}");
            Console.WriteLine($"Field Value: {field.Value}");
            Console.WriteLine($"Field Type: {field.GetType().Name}");
            Console.WriteLine("---");
        }
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

このコードは、簡単なフォームを含むPDFファイルをロードし、各フォームフィールドを反復処理し、フィールド名、フィールド値、およびフィールドタイプを出力します。 PdfDocument.FromFile()メソッドはPDFドキュメントを解析し、Formプロパティはすべてのインタラクティブなフォームフィールドへのアクセスを提供します。 各フィールドは、そのフィールドタイプに特有の他のプロパティを公開しており、正確なデータ抽出を可能にしています。 より複雑なシナリオについては、IronPDF APIリファレンスで高度なフォーム操作方法をご覧ください。

出力

C# Read PDF Form Fields:プログラムでフォームデータを抽出する:画像1 - PDF文書内のすべてのフォームフィールド値を読み取るための出力

異なるフォームフィールドタイプの読み取り

PDFフォームにはさまざまなフィールドタイプがあり、それぞれに特定の取り扱いが必要です。 IronPDFはフィールドタイプを自動的に識別し、特定のアクセスを提供します。

using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("complex_form.pdf");
// Text fields - standard input boxes
var nameField = pdf.Form.FindFormField("fullName");
string userName = nameField.Value;
// Checkboxes - binary selections
var agreeCheckbox = pdf.Form.FindFormField("termsAccepted");
bool isChecked = agreeCheckbox.Value == "Yes";
// Radio buttons - single choice from group
var genderRadio = pdf.Form.FindFormField("gender");
string selectedGender = genderRadio.Value;
// Dropdown lists (ComboBox) - predefined options
var countryDropdown = pdf.Form.FindFormField("country");
string selectedCountry = countryDropdown.Value;
// Access all available options
var availableCountries = countryDropdown.Choices;
// Multi-line text areas
var commentsField = pdf.Form.FindFormField("comments_part1_513");
string userComments = commentsField.Value;
// Grab all fields that start with "interests_"
var interestFields = pdf.Form
    .Where(f => f.Name.StartsWith("interests_"));
// Collect checked interests
List<string> selectedInterests = new List<string>();
foreach (var field in interestFields)
{
    if (field.Value == "Yes")  // checkboxes are "Yes" if checked
    {
        // Extract the interest name from the field name
        string interestName = field.Name.Replace("interests_", "");
        selectedInterests.Add(interestName);
    }
}
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("complex_form.pdf");
// Text fields - standard input boxes
var nameField = pdf.Form.FindFormField("fullName");
string userName = nameField.Value;
// Checkboxes - binary selections
var agreeCheckbox = pdf.Form.FindFormField("termsAccepted");
bool isChecked = agreeCheckbox.Value == "Yes";
// Radio buttons - single choice from group
var genderRadio = pdf.Form.FindFormField("gender");
string selectedGender = genderRadio.Value;
// Dropdown lists (ComboBox) - predefined options
var countryDropdown = pdf.Form.FindFormField("country");
string selectedCountry = countryDropdown.Value;
// Access all available options
var availableCountries = countryDropdown.Choices;
// Multi-line text areas
var commentsField = pdf.Form.FindFormField("comments_part1_513");
string userComments = commentsField.Value;
// Grab all fields that start with "interests_"
var interestFields = pdf.Form
    .Where(f => f.Name.StartsWith("interests_"));
// Collect checked interests
List<string> selectedInterests = new List<string>();
foreach (var field in interestFields)
{
    if (field.Value == "Yes")  // checkboxes are "Yes" if checked
    {
        // Extract the interest name from the field name
        string interestName = field.Name.Replace("interests_", "");
        selectedInterests.Add(interestName);
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

FindFormField()メソッドは、名前によって特定のフィールドに直接アクセスすることを可能にし、すべてのフォームフィールドを繰り返し処理する必要性をなくします。 チェックボックスはチェックされた場合"Yes"を返し、ラジオボタンは選択された値を返します。 ドロップダウンやリストボックスなどの選択フィールドは、Choicesプロパティを通して、フィールドの値と利用可能なすべてのオプションの両方を提供します。 この包括的なメソッドのセットにより、開発者は複雑なインタラクティブフォームからデータへのアクセスと抽出が可能になります。 複雑なフォームを扱う場合は、IronPDFのフォーム編集機能を使用して、プログラムで抽出する前にフィールドの値を入力または修正することを検討してください。

ここで、IronPDFがより複雑なフォームを取り扱ってフォームフィールドの値からデータを抽出することができる様子をご覧ください:

C# Read PDF Form Fields:プログラムでフォームデータを抽出する:画像2 - 複雑なフォーム読み取り出力

実世界の例:アンケートフォームの処理

顧客調査から数百のPDFフォームを処理する必要があるシナリオを考えてみましょう。 以下のコードは、IronPDFを使用したバッチ処理を示しています:


using IronPdf;
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;

public class SurveyProcessor
{
    static void Main(string[] args)
    {
        ProcessSurveyBatch(@"C:\Surveys");
    }

    public static void ProcessSurveyBatch(string folderPath)
    {
        StringBuilder csvData = new StringBuilder();
        csvData.AppendLine("Date,Name,Email,Rating,Feedback");
        foreach (string pdfFile in Directory.GetFiles(folderPath, "*.pdf"))
        {
            try
            {
                PdfDocument survey = PdfDocument.FromFile(pdfFile);
                string date = survey.Form.FindFormField("surveyDate")?.Value ?? "";
                string name = survey.Form.FindFormField("customerName")?.Value ?? "";
                string email = survey.Form.FindFormField("email")?.Value ?? "";
                string rating = survey.Form.FindFormField("satisfaction")?.Value ?? "";
                string feedback = survey.Form.FindFormField("comments")?.Value ?? "";
                feedback = feedback.Replace("\n", " ").Replace("\"", "\"\"");
                csvData.AppendLine($"{date},{name},{email},{rating},\"{feedback}\"");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error processing {pdfFile}: {ex.Message}");
            }

using IronPdf;
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;

public class SurveyProcessor
{
    static void Main(string[] args)
    {
        ProcessSurveyBatch(@"C:\Surveys");
    }

    public static void ProcessSurveyBatch(string folderPath)
    {
        StringBuilder csvData = new StringBuilder();
        csvData.AppendLine("Date,Name,Email,Rating,Feedback");
        foreach (string pdfFile in Directory.GetFiles(folderPath, "*.pdf"))
        {
            try
            {
                PdfDocument survey = PdfDocument.FromFile(pdfFile);
                string date = survey.Form.FindFormField("surveyDate")?.Value ?? "";
                string name = survey.Form.FindFormField("customerName")?.Value ?? "";
                string email = survey.Form.FindFormField("email")?.Value ?? "";
                string rating = survey.Form.FindFormField("satisfaction")?.Value ?? "";
                string feedback = survey.Form.FindFormField("comments")?.Value ?? "";
                feedback = feedback.Replace("\n", " ").Replace("\"", "\"\"");
                csvData.AppendLine($"{date},{name},{email},{rating},\"{feedback}\"");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error processing {pdfFile}: {ex.Message}");
            }
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

よくある質問

IronPDFはC#でPDFフォームフィールドを読むのにどのように役立ちますか?

IronPDFはC#で記入可能なPDFからフォームフィールドのデータを抽出する合理化されたプロセスを提供し、手作業によるデータ抽出に比べて必要な時間と労力を大幅に削減します。

IronPDFを使ってどのようなPDFフォームフィールドを抽出できますか?

IronPdfを使用すると、テキスト入力、チェックボックス、ドロップダウン選択などを含む様々なフォームフィールドを記入可能なPDFから抽出することができます。

なぜPDFフォームのデータ抽出を自動化することが有益なのでしょうか?

IronPDFでPDFフォームのデータ抽出を自動化することで、時間を節約し、エラーを減らし、手入力の必要性をなくすことで生産性を向上させます。

IronPDFは大量のPDFフォーム処理に対応していますか?

IronPdfは大量のPDFフォームを効率的に処理するように設計されており、求人応募、アンケート、その他大量のドキュメントを処理するのに理想的です。

IronPdfを使う利点は何ですか?

IronPdfはヒューマンエラーを減らし、データ抽出プロセスをスピードアップし、開発者が平凡なデータ入力ではなく、より複雑なタスクに集中できるようにします。

IronPDFは異なるPDFフォーマットを扱うことができますか?

IronPdfは様々なPDFフォーマットを扱うことができ、幅広い文書やフォームデザインに対応します。

IronPdfはどのようにデータ抽出の精度を向上させるのですか?

IronPdfは抽出プロセスを自動化することで、手作業によるデータ入力で起こりがちなヒューマンエラーのリスクを最小限に抑え、精度を向上させます。

IronPDFを使用するプログラミング言語は何ですか?

IronPDFはC#で使用するように設計されており、.NETアプリケーションでPDFドキュメントを操作したりデータを抽出したりするための強力なツールを開発者に提供します。

Kannaopat Udonpant
ソフトウェアエンジニア
ソフトウェアエンジニアになる前に、Kannapatは北海道大学で環境資源の博士号を修了しました。博士号を追求する間に、彼はバイオプロダクションエンジニアリング学科の一部である車両ロボティクスラボラトリーのメンバーになりました。2022年には、C#のスキルを活用してIron Softwareのエンジニアリングチームに参加し、IronPDFに注力しています。Kannapatは、IronPDFの多くのコードを執筆している開発者から直接学んでいるため、この仕事を大切にしています。同僚から学びながら、Iron Softwareでの働く社会的側面も楽しんでいます。コードやドキュメントを書いていない時は、KannapatはPS5でゲームをしたり、『The Last of Us』を再視聴したりしていることが多いです。