//Document:(文檔)產生pdf必備的一個對象,產生一個Document樣本
Document document = new Document(PageSize.A4, 30, 30, 5, 5);
//為該Document建立一個Writer執行個體:
PdfWriter.GetInstance(document, new FileStream(@"E:\Chap0103.pdf", FileMode.Create));
//開啟當前Document
document.Open();
string fontPath = Environment.GetEnvironmentVariable("WINDIR") + "\\FONTS\\SIMHEI.TTF";//強制中文字型,否則無法顯示中文
BaseFont baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//包含頁碼沒有任何邊框的頁尾。
HeaderFooter footer = new HeaderFooter(new Paragraph("This is page: "), true);
footer.Border = iTextSharp.text.Rectangle.NO_BORDER;
document.Footer = footer;
//Chapter對象和Section對象自動構建一個樹:
iTextSharp.text.Font fh = new iTextSharp.text.Font(baseFont, 12);
iTextSharp.text.Font f1 = new iTextSharp.text.Font(baseFont,10);
f1.SetStyle(iTextSharp.text.Font.BOLD);
HeaderFooter hf = new HeaderFooter(new Paragraph("\n\n3月份第二批提貨明細\n\n" + "SKZS-03-2012\n\n" + "作業NO" + " " + "作業小番\n\n", fh),true);
document.Header = hf;
Paragraph cTitle = new Paragraph("3月份第二批提貨明細\n\n" + "SKZS-03-2012\n\n" + "作業NO" + " " + "作業小番\n\n", fh);
Chapter chapter = new Chapter(cTitle, 1);
document.Add(chapter);
table.AddCell(cell);
//f1是字型設定
cell = new Cell(new Paragraph("鋼種", f1));
table.AddCell(cell);
cell = new Cell(new Paragraph("爐號", f1));
table.AddCell(cell);
cell = new Cell(new Paragraph("厚度", f1));
table.AddCell(cell);
cell = new Cell(new Paragraph("定尺", f1));
table.AddCell(cell);
cell = new Cell(new Paragraph("捆號", f1));
table.AddCell(cell);
cell = new Cell(new Paragraph("捆數", f1));
table.AddCell(cell);
cell = new Cell(new Paragraph("支數", f1));
table.AddCell(cell);
cell = new Cell(new Paragraph("出庫量", f1));
table.AddCell(cell);
//下面這句話未來以後每頁的table都有頭
table.EndHeaders();
//構建了一個不簡單的表:
Table table = new Table(3);
table.BorderWidth = 1;
table.BorderColor = new iTextSharp.text.Color(0, 0, 255);
table.Cellpadding = 5;
table.Cellspacing = 5;
Cell cell = new Cell("header");
cell.Header = true;
cell.Colspan = 3;
table.AddCell(cell);
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.Rowspan = 2;
cell.BorderColor = new iTextSharp.text.Color(255, 0, 0);
table.AddCell(cell);
table.AddCell("1.1");
table.AddCell("2.1");
table.AddCell("1.2");
table.AddCell("2.2");
table.AddCell("cell test1");
cell = new Cell("big cell");
cell.Rowspan = 2;
cell.Colspan = 2;
cell.BackgroundColor = new iTextSharp.text.Color(0xC0, 0xC0, 0xC0);
table.AddCell(cell);
table.AddCell("cell test2");
// 改變了儲存格“big cell”的對齊:
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
document.Add(table);