Java を使用して PDF フォームからデータを抽出する

このチュートリアルでは、PDF フォームから Java を使用してデータを抽出する方法について説明します。IDE の設定方法、プログラムを作成する手順、および Java を使用して PDF フォームからデータをエクスポートする方法 を示すサンプルコードを紹介します。フォーム内のすべてのフィールド、または特定のフィールドにアクセスし、必要に応じて処理する方法を説明します。

Java を使用して PDF フォームフィールドからデータを抽出する手順

  1. Aspose.PDF for Java を使用してフォームデータを抽出するために IDE を設定する
  2. テキストボックスフィールドとサンプルデータを含む PDF ファイルを作成する
  3. フォームと入力フィールドを含む PDF ファイルを Document オブジェクトに読み込む
  4. 読み込んだドキュメントから Form オブジェクトのフィールドコレクションにアクセスする
  5. すべてのフィールドを反復処理し、完全な名前と値を取得してコンソールに表示する

これらの手順により、Java を使用して PDF フォームフィールドを抽出する方法 を理解できます。フォームフィールドとサンプルデータを含む PDF ファイルを作成するか、既存のフォームデータを含む PDF ファイルを読み込みます。Document オブジェクトの Form プロパティを使用してフィールドコレクションにアクセスし、すべてのフィールドを反復処理して必要なプロパティを取得します。

Java を使用して記入可能な PDF からデータを抽出するコード

import com.aspose.pdf.*;
public class Main {
public static void main(String[] args) throws Exception {
// Load Aspose PDF license
License license = new License();
license.setLicense("license.lic");
// Generate PDF with input fields
createPdfWithFields();
// Open and process the generated PDF file
Document pdfDocument = new Document("UserForm.pdf");
// Retrieve and display form fields
Field[] formFields = pdfDocument.getForm().getFields();
for (Field formField : formFields) {
System.out.println("Field Name: " + formField.getFullName());
System.out.println("Field Content: " + formField.getValue());
}
// Release resources
pdfDocument.close();
}
private static void createPdfWithFields() {
// Instantiate new PDF document
Document pdfFile = new Document();
for (int pageIndex = 1; pageIndex <= 3; pageIndex++) {
Page newPage = pdfFile.getPages().add();
for (int fieldIndex = 1; fieldIndex <= 4; fieldIndex++) {
// Define a text input field
TextBoxField inputField = new TextBoxField(newPage,
new Rectangle(120, fieldIndex * 90, 320, (fieldIndex + 1) * 90));
inputField.setPartialName("inputField_" + pageIndex + "_" + fieldIndex);
inputField.setValue("Data Entry " + pageIndex + "-" + fieldIndex);
// Attach field to the document form
pdfFile.getForm().add(inputField, pageIndex);
}
}
// Save document to disk
pdfFile.save("UserForm.pdf");
// Free resources
pdfFile.close();
}
}

このコードは、Java を使用して PDF フォームからデータを抽出する方法 を示しています。フォームから、フィールドの別名、マッピング名、コンテンツ、部分名、アクティブ状態、選択状態の名前、ページインデックスなど、さまざまなプロパティにアクセスできます。特定のフィールドのみを取得するには、フィールドのインデックスを使用し、例えば formFields[1].getValue() を使用すると、最初のフィールドの値を取得できます。

この記事では、PDF ファイル内のフォームを処理しました。PDF ファイルからフォントを抽出する方法については、Java を使用して PDF からフォントを抽出する 記事を参照してください。

 日本語