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 5 years have passed since last update.

JavaはPDFに表を追加します

Posted at

本文では、Javaを通じてPDFドキュメントに表を追加する方法を紹介します。表を追加すると、表の枠、セルの配置、セルの背景色、セルの結合、画像の挿入、行の高さ、列の幅、フォント、サイズなどを設定できます。

使用ツール:Free Spire.PDF for Java (無料版)

Jarファイルの取得と導入:

方法1:ホームページを通じてjarファイルのカバンをダウンロードします。ダウンロード後、ファイルを解凍して、libフォルダの下Spire.Pdf.jarファイルをJavaプログラムに導入します。

方法2:Maven倉庫設置による導入。

import com.spire.pdf.graphics.*;
import com.spire.pdf.grid.PdfGrid;
import java.awt.*;

public class CreateGrid {

    public static void main(String[] args) {
        //文書を作成し、PDFページを追加します
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add();

        // PdfGridオブジェクトを作成します
        PdfGrid grid = new PdfGrid();

        //セル内余白、標準フォント、フォント色、標準背景色を設定します
        grid.getStyle().setCellPadding(new PdfPaddings(3,3,3,3));
        grid.getStyle().setFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN,10), true));
        grid.getStyle().setTextBrush(PdfBrushes.getBlack());
        grid.getStyle().setBackgroundBrush(PdfBrushes.getLightGray());

        // PdfBordersオブジェクトを作成し、色と太さを設定します
        PdfBorders borders= new PdfBorders();
        borders.setAll(new PdfPen(PdfBrushes.getWhite(),1f));

        //データの定義
        String[] data = {"Continent;Country;Population;Ratio to World Pop;Flag",
                "Asia;China;1,391,190,000;18.2%; ",
                "Asia;Japan;126,490,000;1.66%; ",
                "Europe;United Kingdom;65,648,054;0.86%; ",
                "Europe;Germany;82,665,600;1.08%; ",
                "North America; Canada; 37,119,000; 0.49%; ",
                "North America; United States; 327,216,000; 4.29%; "
                };
        String[][] dataSource = new String[data.length][];
        for (int i = 0; i < data.length; i++) {
            dataSource[i] = data[i].split("[;]", -1);
        }

        //テーブルにデータを埋める
        grid.setDataSource(dataSource);

        //セルの背景色を設定grid.getRows().get(1).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-China.png"));
        grid.getRows().get(2).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-Japan.png"));
        grid.getRows().get(3).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-United-Kingdom.png"));
        grid.getRows().get(4).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-Germany.png"));
        grid.getRows().get(5).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-Canada.png"));
        grid.getRows().get(6).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-United-States-of-America.png"));

        //行ごとの高さを設定します
        grid.getColumns().get(grid.getColumns().getCount()-1).setWidth(60f);

        //セルを縦に結合
        grid.getRows().get(1).getCells().get(0).setRowSpan(2);
        grid.getRows().get(3).getCells().get(0).setRowSpan(2);
        grid.getRows().get(5).getCells().get(0).setRowSpan(2);

        for (int i = 0; i < data.length ; i++) {

            //列ごとの高さを設定します
            grid.getRows().get(i).setHeight(30f);

            //最初の列の背景色を設定しますgrid.getRows().get(i).getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());
            //最初の列のフォントを設定します
grid.getRows().get(i).getCells().get(0).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12),true));

            for (int j = 0; j < grid.getColumns().getCount(); j++) {

                //すべてのセルの外枠スタイルを設定します
grid.getRows().get(i).getCells().get(j).getStyle().setBorders(borders);

                //すべてのセル内のテキストの配置を設定します
grid.getRows().get(i).getCells().get(j).setStringFormat(new PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Middle));

                //最初の行のフォントを設定します
grid.getRows().get(0).getCells().get(j).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12),true));

                //最初の行の背景色を設定します
grid.getRows().get(0).getCells().get(j).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());

            }
        }

        //表をPDFに描画します
        grid.draw(page,0,30);

        //文書を保存
        doc.saveToFile("Grid.pdf");
        doc.close();
    }
}

PDF table.png

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?