Zenn
Open18

iTextSharpのメモ

motonamotona

久々に使うので、ついでにまとめたい

motonamotona

2つのテーブルを横並びにしたい


https://stackoverflow.com/questions/42877599/itextsharp-how-can-i-iterate-tables-side-by-side-in-c-sharp-winforms

TableのなかにTableを置けば、2カラム表示ができるよう

// 外枠のテーブルを用意
var outerTable = new PdfPTable(2);
outerTable.WidthPercentage = 100;
outerTable.SpacingAfter = 14f;
// カラムは3:1の比率で
outerTable.SetWidths(new float[] { 3f, 1f });
// 外枠のテーブルの枠線を消す
outerTable.DefaultCell.Border = 0;

// 左のセルに入れる要素を準備(今回はテーブル)
var innerLeftTable = new PdfPTable(3);
innerLeftTable.WidthPercentage = 100;
innerLeftTable.AddCell(new PdfPHeaderCell { Phrase = new Phrase("Header 1", FONT_Default), BackgroundColor = BaseColor.LIGHT_GRAY });
innerLeftTable.AddCell(new PdfPHeaderCell { Phrase = new Phrase("Header 2", FONT_Default), BackgroundColor = BaseColor.LIGHT_GRAY });
innerLeftTable.AddCell(new PdfPHeaderCell { Phrase = new Phrase("Header 3", FONT_Default), BackgroundColor = BaseColor.LIGHT_GRAY });
innerLeftTable.AddCell(new PdfPCell(new Phrase("Content 1", FONT_Default)));
innerLeftTable.AddCell(new PdfPCell(new Phrase("Content 2", FONT_Default)));
innerLeftTable.AddCell(new PdfPCell(new Phrase("Content 2", FONT_Default)));
outerTable.AddCell(innerLeftTable);

// 右のセルに入れる要素を準備(今回はこちらもテーブル)
var innerRightTable = new PdfPTable(1);
innerRightTable.WidthPercentage = 100;
innerRightTable.AddCell(new PdfPHeaderCell { Phrase = new Phrase("Header 4", FONT_Default), BackgroundColor = BaseColor.LIGHT_GRAY });
innerRightTable.AddCell(new PdfPCell(new Phrase("Content 2", FONT_Default)));
outerTable.AddCell(innerRightTable);

document.Add(outerTable);
motonamotona
iTextSharp.text.Font FONT_Default = FontFactory.GetFont("MS Gothic", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

var table = new PdfPTable(1);
table.AddCell(
    new PdfPCell(
        new Phrase("", FONT_Default)
    ) { MinimumHeight = 1f }
);

PdfPCellMinimumHeightを指定すれば、セルの中身が無くても最低限の高さを持たせられそう?


MinimumHeight = 1fは小さすぎる。1mmとか、そんなぐらいしかあかない感じ。
14fぐらいがちょうど1行ぐらい?
FONT_Defaultのフォントサイズにもよるのかも

motonamotona

セル結合したい

var cell = new PdfPCell(new Phrase("Content 1", FONT_Default));
cell.Colspan = 2;

これでいけるかも?結局使ってないので確認はしていない。

motonamotona

Chunkの背景塗りつぶして、白文字とかできないかな


ちょっと違うけど、Table使って背景黒、白文字の表示を作った。

private static readonly iTextSharp.text.Font FONT_Title_White = FontFactory.GetFont("MS Gothic", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, 20f, (int)FontStyle.Bold, BaseColor.WHITE);

var table = new PdfPTable(1);
table.WidthPercentage = 100;
table.SpacingAfter = 14f;
table.AddCell(new PdfPCell(new Phrase("PDF Title", FONT_Title_White))
{
    BackgroundColor = BaseColor.BLACK,
    Padding = 10f,
    // 多分ベースラインからPaddingBottomを取っているので、はみ出る分大きめにとる
    PaddingBottom = 14f,
});
motonamotona

テーブルセルの中に改行してデータを列挙するが、区切りが分かりやすいように改行単位で横線を入れたい
セルに分けるのは面倒なので、<hr />みたいなのを追加する感じでしたい。

https://stackoverflow.com/questions/17051368/hr-tag-is-not-implemented-in-itextsharp

https://stackoverflow.com/questions/37387918/how-to-draw-a-horizontal-line-in-itextsharp


Separator というのがあるらしい

https://kb.itextpdf.com/it5kb/separator-examples


粗削りだけど、こんな感じで作りたい表示は作れた。

// using iTextSharp.text;
// using iTextSharp.text.pdf;
// using iTextSharp.text.pdf.draw;

var data = new List<string>() { "A", "B", "C", "D" };

var table = new PdfPTable(1);
var cell = new PdfPCell();
foreach (var d in data)
{
    cell.AddElement(new Paragraph(d, FONT_Default));
    var line = new LineSeparator();
    line.Offset = -2;
    cell.AddElement(line);
}
table.AddCell(cell);
document.Add(table);

motonamotona

ドキュメントを読んでいたら、Gridを見つけたので、2カラム表示はこちらを使った方が良さそう?

https://kb.itextpdf.com/it5kb/drawing-a-grid


ちょっと書き直して動かしてみたら、ドットで升目が作ってあるノートみたいなことができるだけっぽい?
Gridは表示のことじゃなかったのかも

var canvas = writer.DirectContent;
for (float x = 0; x < pagesize.Width;)
{
    for (float y = 0; y < pagesize.Height;)
    {
        //canvas.circle(x, y, 1f);
        canvas.Circle(x, y, 1f);
        y += 72f;
    }
    x += 72f;
}
canvas.Fill();

motonamotona

RowSpanの挙動確認

var table = new PdfPTable(3);
table.AddCell(GeneratePdfPCell("1"));
var cell = GeneratePdfPCell("2");
cell.Rowspan = 3;
table.AddCell(cell);
table.AddCell(GeneratePdfPCell("3"));
table.AddCell(GeneratePdfPCell("4"));
table.AddCell(GeneratePdfPCell("5"));
table.AddCell(GeneratePdfPCell("6"));
table.AddCell(GeneratePdfPCell("7"));
table.AddCell(GeneratePdfPCell("8"));
table.AddCell(GeneratePdfPCell("9"));
table.AddCell(GeneratePdfPCell("10"));

// ------------------------------
public static PdfPCell GeneratePdfPCell(string content)
{
    return new PdfPCell(new Phrase(content, FONT_Default))
    {
        MinimumHeight = 14f,
    };
}

ログインするとコメントできます