Itext-formatting-borders-of-cell

提供:Dev Guides
移動先:案内検索

iText-セルの境界線のフォーマット

この章では、iTextライブラリを使用してテーブル内のセルの境界線をフォーマットする方法を説明します。

セルの境界線のフォーマット

*Document* クラスをインスタンス化することにより、空のPDFドキュメントを作成できます。 このクラスをインスタンス化する際、コンストラクターにパラメーターとして *PdfDocument* オブジェクトを渡す必要があります。

次に、ドキュメントにテーブルを追加するには、 Table クラスをインスタンス化し、* add()*メソッドを使用してこのオブジェクトをドキュメントに追加する必要があります。

*DashedBorder、SolidBorder、DottedBorder、DoubleBorder、RoundDotsBorder* などのさまざまなタイプの境界線を追加できます。 *Cell* クラスの* setBorder()*メソッドを使用してさまざまな色で。

以下は、テーブル内のセルの境界線をフォーマットする手順です。

ステップ1:PdfWriterオブジェクトの作成

*PdfWriter* クラスは、PDFのDocWriterを表します。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 このクラスのコンストラクターは、PDFが作成されるファイルのパスを表す文字列を受け入れます。

以下に示すように、文字列値(PDFを作成する必要があるパスを表す)をコンストラクターに渡すことにより、PdfWriterクラスをインスタンス化します。

//Creating a PdfWriter
String dest = "C:/itextExamples/coloredBorders.pdf";
PdfWriter writer = new PdfWriter(dest);

このタイプのオブジェクトがPdfDocument(クラス)に渡されると、このドキュメントに追加されたすべての要素が指定されたファイルに書き込まれます。

ステップ2:PdfDocumentオブジェクトの作成

*PdfDocument* クラスは、iTextでPDFDocumentを表すクラスです。 このクラスは、パッケージ *com.itextpdf.kernel.pdf* に属します。 (書き込みモードで)このクラスをインスタンス化するには、クラス *PdfWriter* のオブジェクトをコンストラクターに渡す必要があります。

以下に示すように、上記で作成した PdfWriter オブジェクトをコンストラクターに渡すことで、 PdfDocument クラスをインスタンス化します。

//Creating a PdfDocument
PdfDocument pdfDoc = new PdfDocument(writer);

PdfDocumentオブジェクトを作成したら、そのクラスが提供するそれぞれのメソッドを使用して、ページ、フォント、添付ファイル、イベントハンドラーなどのさまざまな要素を追加できます。

ステップ3:Documentオブジェクトを作成する

パッケージ com.itextpdf.layoutDocument クラスは、自給自足のPDFを作成する際のルート要素です。 このクラスのコンストラクターの1つは、クラスPdfDocumentのオブジェクトを受け入れます。

以下に示すように、前の手順で作成した PdfDocument クラスのオブジェクトを渡すことにより、 Document クラスをインスタンス化します。

//Creating a Document
Document document = new Document(pdfDoc);

ステップ4:テーブルオブジェクトの作成

*Table* クラスは、行と列に並べられたセルで満たされた2次元グリッドを表します。 パッケージ *com.itextpdf.layout.element* に属します。

以下に示すように Table クラスをインスタンス化します。

//Creating a table
float [] pointColumnWidths = {200F, 200F};
Table table = new Table(pointColumnWidths);

ステップ5:セルの作成

パッケージの Cell クラスをインスタンス化してセルオブジェクトを作成します com.itextpdf.layout.element は、以下に示すように、 Cell クラスの* add()*メソッドを使用してセルのコンテンツを追加します。

//Adding cell 1 to the table
Cell cell1 = new Cell();//Creating a cell
cell1.add("Name");      //Adding content to the cell

ステップ6:セルの境界線をフォーマットする

iTextライブラリは、 DashedBorder、SolidBorder、DottedBorder、DoubleBorder、RoundDotsBorder などの境界を表すさまざまなクラスを提供します。

これらのクラスのコンストラクターは、境界線の色を表す color オブジェクトと境界線の幅を表す integer という2つのパラメーターを受け入れます。

以下に示すように、この境界タイプの1つを選択し、 color オブジェクトと幅を表す integer を渡すことにより、それぞれの境界をインスタンス化します。

Border b1 = new DashedBorder(Color.RED, 3);

次に、 cell クラスの* setBorder()メソッドを使用してセルの境界線を設定します。 このメソッドは、 *Border 型のオブジェクトをパラメーターとして受け入れます。

以下に示すように、上記で作成した Border オブジェクトをパラメーターとして* setBorder()*メソッドに渡すことにより、セルの境界線を設定します。

c1.setBorder(b1)

最後に、このセルをテーブルに追加するには、以下に示すように、 Table クラスの* addCell()メソッドを呼び出し、 *cell オブジェクトをパラメーターとしてこのメ​​ソッドに渡します。

table.addCell(c1);

ステップ7:文書に表を追加する

以下に示すように、 Document クラスの* add()メソッドを使用して、前の手順で作成した *table オブジェクトを追加します。

//Adding list to the document
document.add(table);

ステップ8:ドキュメントを閉じる

以下に示すように、 Document クラスの* close()*メソッドを使用してドキュメントを閉じます。

//Closing the document
document.close();

次のJavaプログラムは、iTextライブラリを使用してテーブル内のセルの境界線をフォーマットする方法を示しています。 coloredBorders.pdf という名前のPDFドキュメントを作成し、テーブルを追加し、そのセルのコンテンツをフォーマットし、パス C:/itextExamples/ に保存します

このコードを FormatedBorders.java という名前のファイルに保存します。

import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;

import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.border.DashedBorder;
import com.itextpdf.layout.border.DottedBorder;
import com.itextpdf.layout.border.DoubleBorder;
import com.itextpdf.layout.border.RoundDotsBorder;
import com.itextpdf.layout.border.SolidBorder;

import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;

public class FormatedBorders {
   public static void main(String args[]) throws Exception {
     //Creating a PdfWriter object
      String dest = "C:/itextExamples/coloredBorders.pdf";

      PdfWriter writer = new
      PdfWriter(dest);

     //Creating a PdfDocument object
      PdfDocument pdfDoc = new PdfDocument(writer);

     //Creating a Document object
      Document doc = new Document(pdfDoc);

     //Creating a table
      float [] pointColumnWidths = {200F, 200F};
      Table table = new Table(pointColumnWidths);

     //Adding row 1 to the table
      Cell c1 = new Cell();

     //Adding the contents of the cell
      c1.add("Name");

     //Setting the back ground color of the cell
      c1.setBackgroundColor(Color.DARK_GRAY);

     //Instantiating the Border class
      Border b1 = new DashedBorder(Color.RED, 3);

     //Setting the border of the cell
      c1.setBorder(b1);

     //Setting the text alignment
      c1.setTextAlignment(TextAlignment.CENTER);

     //Adding the cell to the table
      table.addCell(c1);
      Cell c2 = new Cell();
      c2.add("Raju");
      c1.setBorder(new SolidBorder(Color.RED, 3));
      c2.setTextAlignment(TextAlignment.CENTER);
      table.addCell(c2);

     //Adding row 2 to the table
      Cell c3 = new Cell();
      c3.add("Id");
      c3.setBorder(new DottedBorder(Color.DARK_GRAY, 3));
      c3.setTextAlignment(TextAlignment.CENTER);
      table.addCell(c3);

      Cell c4 = new Cell();
      c4.add("001");
      c4.setBorder(new DoubleBorder(Color.DARK_GRAY, 3));
      c4.setTextAlignment(TextAlignment.CENTER);
      table.addCell(c4);

     //Adding row 3 to the table
      Cell c5 = new Cell();
      c5.add("Designation");
      c5.setBorder(new RoundDotsBorder(Color.RED, 3));
      c5.setTextAlignment(TextAlignment.CENTER);
      table.addCell(c5);

      Cell c6 = new Cell();
      c6.add("Programmer");
      c6.setBorder(new RoundDotsBorder(Color.RED, 3));
      c6.setTextAlignment(TextAlignment.CENTER);
      table.addCell(c6);

     //Adding Table to document
      doc.add(table);

     //Closing the document
      doc.close();

      System.out.println("Borders added successfully..");
   }
}

次のコマンドを使用して、コマンドプロンプトから保存したJavaファイルをコンパイルして実行します-

javac FormatedBorders.java
java FormatedBorders

実行時に、上記のプログラムはPDFドキュメントを作成し、次のメッセージを表示します。

Borders added successfully

指定したパスを確認すると、以下に示すように、作成されたPDFドキュメントを見つけることができます。

色付きの境界線