Java REST API を使用して入力可能な PDF を作成する

この簡単なチュートリアルでは、Java REST API を使用して入力可能な PDF を作成する方法を説明します。 Java ベースの Cloud SDK を使用して、Java RESTful サービスで入力可能な PDF フォームを自動的に作成する方法を学習します。 PDF に追加する前に、さまざまなプロパティを設定し、フォーム フィールドをカスタマイズする方法を学習します。

前提条件

Java ローコード API を使用して入力可能な PDF を構築する手順

  1. クライアント ID とシークレットを設定して PdfApi オブジェクトを構成し、入力可能な PDF を作成します
  2. フィールドを作成してパラメータを設定する
  3. PDF ページ上にフィールドを配置する四角形を作成し、フィールドに設定します
  4. 選択した項目のリストを作成し、フィールドのタイプを設定します
  5. フィールドを追加するための名前を割り当てて、ソース PDF ファイルをクラウド ストレージにアップロードします
  6. PostCreateField() メソッドを呼び出して、指定されたページにフィールドを挿入します
  7. API 応答を確認し、結果の PDF ファイルをディスクに保存します

これらの手順は、Java ベースの API を使用して入力可能な PDF を作成する方法 をまとめたものです。 PdfAp オブジェクトを作成し、フィールドを作成し、デフォルト値、名前、サイズと位置の四角形、フィールド タイプなどのパラメーターを設定します。最後に、ソース PDF ファイルをロードし、定義されたページに PostCreateField を追加して、出力 PDF ファイルを保存します。

Java REST API を使用して編集可能な PDF を生成するコード

import com.aspose.pdf.cloud.sdk.ApiException;
import com.aspose.pdf.cloud.sdk.Configuration;
import com.aspose.pdf.cloud.sdk.api.PdfApi;
import com.aspose.pdf.cloud.sdk.model.AsposeResponse;
import com.aspose.pdf.cloud.sdk.model.Field;
import com.aspose.pdf.cloud.sdk.model.Rectangle;
import com.aspose.pdf.cloud.sdk.model.FieldType;
import com.aspose.pdf.cloud.sdk.model.FileUploadResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class PdfTasks {
public static void createFormField() throws ApiException, IOException {
// Initialize API with credentials
String clientId = "ClientID";
String clientSecret = "ClientSecret";
// Setup the Aspose API configuration
Configuration configuration = new Configuration(clientId, clientSecret);
PdfApi pdfApi = new PdfApi(configuration);
String fileName = "sample.pdf"; // Input PDF file
Integer pageNo = 2; // The page number where the form field should be added
// Create the field
List<String> values = new ArrayList<>();
values.add("TestDataForPDF");
Field field = new Field();
field.setName("StudentName");
field.setValues(new ArrayList<String>() {{
add("NewFieldValue");
}});
// Set the field position
Rectangle rectangle = new Rectangle();
rectangle.setLLX(0.0f); // Lower-left X
rectangle.setLLY(0.0f); // Lower-left Y
rectangle.setURX(0.0f); // Upper-right X
rectangle.setURY(0.0f); // Upper-right Y
field.setRect(rectangle);
field.setSelectedItems(new ArrayList<Integer>() {{
add(1);
}});
field.setType(FieldType.Text);
try {
// Upload the source file to Aspose Cloud storage
File pdfFile = new File(fileName);
try (FileInputStream fileInputStream = new FileInputStream(pdfFile)) {
FileUploadResponse uploadResult = pdfApi.uploadFile(fileName, fileInputStream);
System.out.println("File uploaded: " + uploadResult.getStatus());
}
// Create form field in the PDF
AsposeResponse apiResponse = pdfApi.postCreateField(fileName, pageNo, field);
if (apiResponse != null && "OK".equals(apiResponse.getStatus())) {
// Download the created PDF file
FileOutputStream fileOutputStream = new FileOutputStream("output.pdf");
try {
fileOutputStream.write(pdfApi.downloadFile(fileName).readAllBytes());
System.out.println("Output file created: output.pdf");
} finally {
fileOutputStream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
createFormField();
} catch (ApiException | IOException e) {
e.printStackTrace();
}
}
}

このコードは、Java Low Code API を使用して入力可能な PDF を作成する方法を示しています。別のコントロールをフォームに追加する場合は、FieldType プロパティを Text または他のタイプに設定することで編集ボックスを追加できます。 Link クラス オブジェクトを使用し、それをフィールドのプロパティとして設定することで、フィールドにリンクを追加できます。

この記事では、Java REST インターフェイスを使用して入力可能な PDF を生成する方法を説明しました。 PDF を Word 文書に変換する場合は、Java REST API を使用して PDF を DOCX に変換する方法 の記事を参照してください。

 日本語