0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Java PDF内で表の作成

Last updated at Posted at 2021-09-27

PDF文書でデータを処理する時がよくあると思います。PDF内で表やテーブルを作ったり、データを編集したりすることで、スキルがアップされますよね。だからこの記事では、Spire.PDF for Javaというライブラリを活用して、PDF内で表の作成法を紹介致します!

Spire.PDF for Javaとは?

>

Spire.PDF for Java is a PDF API that enables Java applications to create, edit and convert 》PDF documents without using Adobe Acrobat.

和訳:Spire.PDF for Javaは、Eiceblue社が開発され、開発者が JavaプラットホームでPDFのドキュメントを迅速かつ高品質で作成・編集・変換印刷するために設計された専門的なPDF処理APIです。

そして、商用版と無料版のFree.Spire.Pdf for javaがあります。Spire.PDF for Javaは商用版ではありますが、基本的な機能を搭載しているので、無料試用で日常の仕事にもう結構だと思います。

下準備

1.E-iceblueの公式サイトからSpire.PDF for Javaをダウンロードしてください。

f:id:lendoris:20210927154630p:plain

2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにある相応しいSpire.PDF.jarを参照に追加してください。

Mavenの側なら?

もしMavenの利用者だったら、pom.xmlファイルに下記のようなコードを追加することで、JARを簡単にインポートできます。

```java com.e-iceblue http://repo.e-iceblue.cn/repository/maven-public/ e-iceblue spire.PDF 4.8.7 ```

サンプルコード

```java import com.spire.pdf.*; import com.spire.pdf.graphics.*; import com.spire.pdf.tables.*; import java.awt.*; import java.awt.geom.Point2D;

public class PDF {
public static void main(String[] args) {
// PdfDocument オブジェクトを作成します。
PdfDocument doc = new PdfDocument();

    //マージンを設定します。
    PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
    PdfMargins margin = new PdfMargins();
    margin.setTop(unitCvtr.convertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));
    margin.setBottom(margin.getTop());
    margin.setLeft(unitCvtr.convertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));
    margin.setRight(margin.getLeft());

    //Add one page
    PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);

    float y = 10;

    //タイトルを書きます
    PdfBrush brush1 = PdfBrushes.getBlack();
    PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Ms mincho", Font.BOLD, 16));
    PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
    page.getCanvas().drawString("世界国家", font1, brush1, page.getCanvas().getClientSize().getWidth() / 2, y, format1);
    y = y + (float) font1.measureString("世界国家", format1).getHeight();
    y = y + 5;

    //データを追加します。
    String[] data = {"Name;Capital;Continent;Area;Population", "Argentina;Buenos Aires;South America;2777815;32300003", "Bolivia;La Paz;South America;1098575;7300000", "Brazil;Brasilia;South America;8511196;150400000", "Canada;Ottawa;North America;9976147;26500000", "Chile;Santiago;South America;756943;13200000", "Colombia;Bagota;South America;1138907;33000000", "Cuba;Havana;North America;114524;10600000", "Ecuador;Quito;South America;455502;10600000", "El Salvador;San Salvador;North America;20865;5300000", "Guyana;Georgetown;South America;214969;800000", "Jamaica;Kingston;North America;11424;2500000", "Mexico;Mexico City;North America;1967180;88600000", "Nicaragua;Managua;North America;139000;3900000", "Paraguay;Asuncion;South America;406576;4660000", "Peru;Lima;South America;1285215;21600000", "United States of America;Washington;North America;9363130;249200000", "Uruguay;Montevideo;South America;176140;3002000", "Venezuela;Caracas;South America;912047;19700000"};

    String[][] dataSource = new String[data.length][];
    for (int i = 0; i < data.length; i++) {
        dataSource[i] = data[i].split("[;]", -1);
    }

    //データソースをつくります。
    PdfTable table = new PdfTable();
    table.getStyle().setCellPadding(2);
    table.getStyle().setHeaderSource(PdfHeaderSource.Rows);
    table.getStyle().setHeaderRowCount(1);
    table.getStyle().setShowHeader(true);
    table.setDataSource(dataSource);

    //テーブルをつくります
    PdfLayoutResult result = table.draw(page, new Point2D.Float(0, y));
    y = y + (float) result.getBounds().getHeight() + 5;
    PdfBrush brush2 = PdfBrushes.getGray();
    PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Ms mincho", 0, 9));
    page.getCanvas().drawString(String.format("* %1$s ヶ国が含まれます.", data.length - 1), font2, brush2, 5, y);

    //保存します。
    doc.saveToFile("output/Table.pdf");
}

}


<h1><strong>実行結果</strong><?h1>

<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210927/20210927154841.png" alt="f:id:lendoris:20210927154841p:plain" width="554" height="443" loading="lazy" title="" class="hatena-fotolife" itemprop="image" /></p>

<h1><strong>以下の関連記事もご参照</strong></h1>
><p><strong>PDF変換機能:<a href="https://www.e-iceblue.com/Tutorials/Java/Spire.PDF-for-Java/Program-Guide/Conversion/Convert-PDF-to-Excel-in-Java.html">Convert PDF to Excel in Java</a></strong></p>
<p><strong>PDFの作成法:<a href="https://www.e-iceblue.com/Tutorials/Spire.PDF-for-JAVA/Spire.PDF-Program-Guide-JAVA/Document-Operation/Create-a-PDF-Document-in-Java.html">Create a PDF Document in Java</a></strong></p>
<p><strong>PDFを印刷:<a href="https://www.e-iceblue.com/Tutorials/Java/Spire.PDF-for-Java/Program-Guide/Print/How-to-print-PDF-document-in-Java.html">Create a PDF Document in Java</a></strong></p>

<h1><strong>終わりに</strong></h1>
<p><strong><a href="https://www.e-iceblue.com/Introduce/pdf-for-java.html">Spire.PDF for Java</a>は、パワフルな機能を満載しているので、使いやすいし、開発者にとってかなり便利なツールですね。もし完全な機能を使いたいのなら、直接<a href="https://www.e-iceblue.com/">E-iceblue</a>に連絡していいんですよ。</strong></p>
<p> </p>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?