public void MyTestCreatePDF()
{
Document doc = null;
try
{
//ドキュメントを作成
doc = new Document();
//ファイルの出力先を設定
PdfWriter.GetInstance(doc, new FileStream("C:\\temp\\test.pdf", FileMode.Create));
//ドキュメントを開く
doc.Open();
//Document.Add(new
Paragraph("フォントを適用させたい文字列", Fontオブジェクト));
string fontPath = Path.Combine(Environment.GetEnvironmentVariable("windir"), "fonts", "msgothic.ttc");
//[1] MSゴシック
Font fnt1 = new Font(BaseFont.CreateFont(string.Format(@"{0},0", fontPath), BaseFont.IDENTITY_H, true), 10);
// Image file path
string noImage = Path.Combine(Application.StartupPath, "C:\\temp\\No_Image.jpg");
//2列になるテーブルを作成
iTextSharp.text.pdf.PdfPTable tbl = new PdfPTable(2);
//各列の幅
float[] colWidths = { 45, 95 };
tbl.SetWidths(colWidths);
//全体の幅(パーセンテージ)
tbl.WidthPercentage = (100.0f);
tbl.DefaultCell.Padding = 5;
tbl.DefaultCell.Border = (PdfPCell.BOTTOM_BORDER);
PdfPCell cell1 = new PdfPCell(new Phrase("Text1", fnt1));
cell1.Colspan = 2;
cell1.PaddingBottom = 5;
tbl.AddCell(cell1);
//[画像]
Image pic = Image.GetInstance(noImage);
//Image property
setting
pic.ScaleAbsolute(120, 140);
pic.Border = Rectangle.BOX;
pic.BorderColor =
iTextSharp.text.BaseColor.BLACK;
pic.BorderWidth = 5f;
//the second
parameter of the constructor is explicity passed for
//clarity, and
ensures the ScalePercent() call is honored
//if you try and
add the image directly, ScalePercent() is ignored
//Cellの二つ目のプロパティ=falseにすると画像サイズ自動でセルと同じサイズに無視することができる
//その後自分の指定サイズ設定できる
PdfPCell cell2 = new PdfPCell(pic, false);
cell2.Rowspan = 3;
cell2.PaddingTop = 5;
cell2.PaddingBottom = 5;
cell2.HorizontalAlignment = Element.ALIGN_CENTER;
//cell2.VerticalAlignment
= Element.ALIGN_MIDDLE;
tbl.AddCell(cell2);
PdfPCell cell3 = new PdfPCell(new Phrase("Text2", fnt1));
cell3.PaddingBottom = 5;
tbl.AddCell(cell3);
PdfPCell cell4 = new PdfPCell(new Phrase("Text3", fnt1));
cell4.PaddingBottom = 5;
tbl.AddCell(cell4);
PdfPCell cell5 = new PdfPCell(new Phrase("Text4", fnt1));
cell5.PaddingBottom = 5;
tbl.AddCell(cell5);
PdfPCell cell6 = new PdfPCell(new Phrase("Text5", fnt1));
cell6.PaddingBottom = 5;
tbl.AddCell(cell6);
PdfPCell cell7 = new PdfPCell(new Phrase("Text6", fnt1));
cell7.PaddingBottom = 5;
tbl.AddCell(cell7);
doc.Add(tbl);
}
catch (Exception ex)
{
throw ex;
}
finally
{
//ドキュメントを閉じる
doc.Close();
doc = null;
}