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);