Java を使って PDF にテーブルを追加

Java ライブラリの Aspose.PDF を使用して、プログラムで PDF ドキュメントにテーブルを挿入します

Java ライブラリを使用して PDF ドキュメントにテーブルを追加する方法

テーブルを追加するために、Javaプラットフォーム用の機能が豊富で強力で使いやすい変換APIである Aspose.PDF for Java APIを使用します。最新バージョンを Maven から直接ダウンロードし、pom.xmlに以下の設定を追加することで、Mavenベースのプロジェクト内にインストールできます。

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

Java でテーブルをPDFに追加


ご使用の環境でコードを試すには、Aspose.PDF for Java が必要です。

  1. PDF を Document のインスタンスとともに読み込みます。
  2. インデックスを介してページにアクセスします。
  3. Table オブジェクトを作成します。
  4. テーブル設定を設定します (例:境界線の設定)。
  5. テーブルを移入します。
  6. テーブルをページに追加します。
  7. ファイルを保存します。

PDF 形式のテーブルを追加-Java

Document pdfDocument = new Document(DATA_DIR.resolve("input.pdf").toString());
alizes a new instance of the Table
com.aspose.pdf.Table table = new com.aspose.pdf.Table();
he table border color as LightGray
table.setBorder(new BorderInfo(BorderSide.All, .5f, Color.getLightGray()));
he border for table cells
table.setDefaultCellBorder(new BorderInfo(BorderSide.All, .5f, Color.getLightGray()));
e a loop to add 10 rows
for (int row_count = 1; row_count < 10; row_count++) {
    // add row to table
    Row row = table.getRows().add();
    // add table cells
    row.getCells().add("Column (" + row_count + ", 1)");
    row.getCells().add("Column (" + row_count + ", 2)");
    row.getCells().add("Column (" + row_count + ", 3)");
}
able object to first page of input document
pdfDocument.getPages().get_Item(1).getParagraphs().add(table);
updated document containing table object
pdfDocument.save(DATA_DIR.resolve("document_with_table.pdf").toString());
pdfDocument.close();