iTextSharp中提供了Chapter和Section,可以用来生成文档结构图样式的pdf文档
iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(PageSize.A4.Width, PageSize.A4.Height);
rect.BackgroundColor = new BaseColor(ColorTranslator.FromHtml("#cccccc"));
iTextSharp.text.Document doc = new Document(rect);
PdfWriter pw=PdfWriter.GetInstance(doc, new System.IO.FileStream("c:\\test.pdf", System.IO.FileMode.OpenOrCreate));
doc.Open();
Chapter c1 = new Chapter(new Paragraph("this is test"), 1);
Section s1 = c1.AddSection(new Paragraph("this is test1"));
BaseFont baseFont = BaseFont.CreateFont(
"C:\\WINDOWS\\FONTS\\SIMHEI.TTF",
BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 11);
//在内容中加入一个超链接
Paragraph p = new Paragraph("asdasfdfasdfsdfasdfsadfsdf\r\nasdfasdfsadf");
iTextSharp.text.Anchor ah = new iTextSharp.text.Anchor("超链接",font);
ah.Reference = "http://wangfu-02.iteye.com";
p.Add(ah);
s1.Add(p);
Section s2 = c1.AddSection(new Paragraph("this is test2"));
c1.BookmarkTitle = "chapter";
c1.BookmarkOpen = true;
doc.Add(c1);
doc.Close();
说明:使用Chapter和Section创建文档结构图样式的时候,占用内存会比较大。
pdf中使用中文的时候需要指定iTextSharp.text.Font,指定使用的字体格式,要不不能正确显示中文