2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

iTextShapを使った記録

Last updated at Posted at 2022-02-10

この前C#でiTextSharpを使ってPDF作成をしたので使ったので記録用に
実際のソースを簡略化したものが以下。出力関連は省いてます。

using iTextSharp.text;
using iTextSharp.text.pdf;

// 省略

public void createPdf() {

// A4縦でドキュメントを作成
var document = new Document(PageSize.A4);
document.Open();

// Fontの設定
FontFactory.RegisterDirectories();
var fontDefault = new Font("MS Gothic", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
var fontTitle = new Font("MS Gothic", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, 20f, (int)FontStyle.Bold);
var fontHeader = new Font("MS Gothic", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, 14f, (int)FontStyle.Bold);

/////////////////////////

// 右上寄せの枠線なしの2カラムのテーブルを作成
var paragraph = new Paragraph();
var table = new PdfPTable(2);
table.HorizontalAligment = Element.ALIGN_RIGHT;
table.WidthParcentage = 35;
table.SpacingAfter = 10f;
table.SetWidths(new float[] { 5f, 4f });
table.AddCell(new PdfPCell(new Phrase("Date:", fontDefault)) { BorderWidth = 0 });
table.AddCell(new PdfPCell(new Phrase(DateTime.Now.ToString("yyyy/MM/dd"), fontDefault)) { BorderWidth = 0 });
paragraph.Add(table);
document.Add(paragrap);

// ファイルタイトルを挿入
document.Add(new Paragraph("File Title", fontTitle) { Aligment = Element.ALIGN_CENTER });

//下線付きのHeaderを追加
document.Add(new Chunk("Category", fontHeader).SetUnderline(0.1f, -2f));

// 枠線ありの4カラムのテーブルを作成
paragtaph = new Paragrapg();
table = new PdfPTable(4);
table.WidthParcentage = 90;
table.SpacingAfter = 14f;
table.SetWidths(new float[] { 1f, 1f, 1f, 3f });
// TableHeader
table.Add(new PdfPHeaderCell { Phrase = new Phrase("Header1"), BackGroundColor = BaseColor.LIGHT_GRAY });
table.Add(new PdfPHeaderCell { Phrase = new Phrase("Header2"), BackGroundColor = BaseColor.LIGHT_GRAY });
table.Add(new PdfPHeaderCell { Phrase = new Phrase("Header3"), BackGroundColor = BaseColor.LIGHT_GRAY });
table.Add(new PdfPHeaderCell { Phrase = new Phrase("Header4"), BackGroundColor = BaseColor.LIGHT_GRAY });
// TableBody
table.AddCell(new PdfPCell(new Phrase("Body1", fontDefault)));
table.AddCell(new PdfPCell(new Phrase("Body2", fontDefault)));
table.AddCell(new PdfPCell(new Phrase("Body3", fontDefault)));
table.AddCell(new PdfPCell(new Phrase("Body4", fontDefault)));
paragraph.Add(table);
document.Add(paragraph);

/////////////////////////

document.Close();
}

ドキュメント作成

ドキュメント作成関連は古いけどこの記事を参考に

Fontの設定

一行目は必要かわからない。
第一引数:"MS Gothic" -> フォントファミリーの指定。今回はMSゴシック
第二引数:BaseFont.IDENTITY_H -> 横書き、縦書きの指定。(BaseFont.IDENTITY_H:横、BaseFont.IDENTITY_V:縦)
第三引数:BaseFont.NOT_EMBEDDED -> 何の指定かわかってません
第四引数:20f -> フォントサイズ。デフォルトは-1fだったはず
第五引数:(int)FontStyle.Bold -> 太字の指定

FontFactory.RegisterDirectories();
var fontDefault = new Font("MS Gothic", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
var fontTitle = new Font("MS Gothic", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, 20f, (int)FontStyle.Bold);
var fontHeader = new Font("MS Gothic", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, 14f, (int)FontStyle.Bold);

右上寄せの枠線なしの2カラムのテーブルを作成

document.Add(table)をすると上詰めでテーブルが入るっぽいので、PdfPTableも入れられるParagraphを用意。

var paragraph = new Paragraph();

new PdfPTable(2) -> 2カラムのテーブルを作成。
HorizontalAligment -> テーブルを左寄せ、中央、右寄せかを指定。
WidthParcentage -> テーブルの横幅をパーセンテージで指定。
SpacingAfter -> テーブルの下に入れるスペース。
SetWidths -> カラムそれぞれの幅を指定。
new float[] { 5f, 4f } -> 今回は5fが1つ目のカラム、4fが2つ目のカラム。カラム1:カラム2=5:4で幅が決まるらしい。

var table = new PdfPTable(2);
table.HorizontalAligment = Element.ALIGN_RIGHT;
table.WidthParcentage = 35;
table.SpacingAfter = 10f;
table.SetWidths(new float[] { 5f, 4f });

table.AddCell() -> テーブルにPdfPCellを追加
new PdfPCell() -> テーブルのPdfPCellを作成
new Phrase("Date:", fontDefault) -> フォント設定で作成したFontの指定で「Date:」が入っているPhraseを作成。
BorderWidth = 0 -> 作成したPdfPCellの枠線の太さを0に = 枠線を削除。

table.AddCell(new PdfPCell(new Phrase("Date:", fontDefault)) { BorderWidth = 0 });
table.AddCell(new PdfPCell(new Phrase(DateTime.Now.ToString("yyyy/MM/dd"), fontDefault)) { BorderWidth = 0 });

ParagraphにPdfPTableを追加してdocumentに追加

paragraph.Add(table);
document.Add(paragrap);

下線付きのHeaderを追加

下線付けられるのはChunkだけっぽいのでChunkを作成。
SetUnderline(0.1f, -2f) -> 下線をつける。引数は参考にした記事をそのまま。

document.Add(new Chunk("Category", fontHeader).SetUnderline(0.1f, -2f));

枠線ありの4カラムのテーブルを作成

前述で説明したので割愛

paragtaph = new Paragrapg();
table = new PdfPTable(4);
table.WidthParcentage = 90;
table.SpacingAfter = 14f;
table.SetWidths(new float[] { 1f, 1f, 1f, 3f });

TableHeaderを作成。
PdfPCellでも作れるけど、なんとなくPdfPHeaderCellを使ってみた。
表の項目が多すぎてページが増えたときに新しいページの先頭に追加してくれないかと期待したけど、そんな気遣いはなかった
new PdfPHeaderCell -> PdfPHeaderCellを作成。
Phrase = new Phrase("Header1", fontDefault) -> PdfPHeaderCellのコンテンツを作成
BackGroundColor = BaseColor.LIGHT_GRAY -> 背景色を指定。

// TableHeader
table.Add(new PdfPHeaderCell { Phrase = new Phrase("Header1", fontDefault), BackGroundColor = BaseColor.LIGHT_GRAY });
table.Add(new PdfPHeaderCell { Phrase = new Phrase("Header2", fontDefault), BackGroundColor = BaseColor.LIGHT_GRAY });
table.Add(new PdfPHeaderCell { Phrase = new Phrase("Header3", fontDefault), BackGroundColor = BaseColor.LIGHT_GRAY });
table.Add(new PdfPHeaderCell { Phrase = new Phrase("Header4", fontDefault), BackGroundColor = BaseColor.LIGHT_GRAY });

PdfPCellで作る場合は以下のように書き換え
GrayFill = 0.8f -> 背景色の濃度を指定

table.Add(new PdfPCell(new Phrase("Header1", fontDefault)) { GrayFill = 0.8f });

tableのBodyを作成。
実際はForeachで回して作成。

// TableBody
table.AddCell(new PdfPCell(new Phrase("Body1", fontDefault)));
table.AddCell(new PdfPCell(new Phrase("Body2", fontDefault)));
table.AddCell(new PdfPCell(new Phrase("Body3", fontDefault)));
table.AddCell(new PdfPCell(new Phrase("Body4", fontDefault)));
paragraph.Add(table);
document.Add(paragraph);

感想

調べてみたら文献まとまってないし、古くてそのまま使えなくて困ったので記録。
こうやって記事にしてみるとまだわかってないことたくさんあって反省です。
参考はもっとあったけどありすぎて見つからないので、見つけられたら追々追加します。

参考

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?