代码如下:
Code
1 /// <summary> 2 /// 生成PDF 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 protected void btnSave_Click(object sender, EventArgs e) 7 { 8 DataTable dt = bll.GetSupply(); 9 string fontPath = @"c:\windows\fonts\SIMKAI.ttf";//楷体字 10 //创建PDF文档对象 11 Document doc = new Document(); 12 //实例化生成的文档 13 PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(this.txtPath.Text.Trim(), FileMode.Create)); 14 BaseFont baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 15 Font font = new Font(baseFont, 10); 16 doc.AddAuthor(this.txtAuthor.Text.Trim());//作者 17 doc.AddKeywords(this.txtKeyWords.Text.Trim());//关键字 18 doc.AddSubject(this.txtZhuti.Text.Trim());//主题 19 doc.AddTitle(this.txtTitle.Text.Trim());//标题 20 //页眉 页眉和页脚的HeaderFooter 类 只存在于 Itextsharp 5.0 version以下,高版本现在已经Remove HeaderFooter类 21 HeaderFooter header = new HeaderFooter(new Phrase(this.txtHeader.Text.Trim()), true); 22 header.Border = Rectangle.NO_BORDER; 23 doc.Header = header; 24 25 //页脚 26 HeaderFooter footer = new HeaderFooter(new Phrase(this.txtFooter.Text.Trim()), true); 27 doc.Footer = footer; 28 29 doc.Open(); 30 //添加图片 31 iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(@"C:\Documents and Settings\Administrator\My Documents\My Pictures\129810262468791250.png"); 32 doc.Add(jpg); 33 doc.Add(new Phrase(this.txtContent.Text.Trim(), font)); 34 //创建Table 35 PdfPTable table = new PdfPTable(dt.Columns.Count); 36 //设置默认表格样式 37 table.DefaultCell.BorderWidth = 0.5f; 38 table.DefaultCell.BorderColor = Color.BLUE; 39 table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER; 40 table.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE; 41 for (int i = 0; i < dt.Columns.Count; i++) 42 { 43 table.AddCell(new Phrase(dt.Columns[i].ColumnName, font)); 44 } 45 table.DefaultCell.BackgroundColor = Color.GRAY; 46 table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; 47 table.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE; 48 for (int i = 0; i < dt.Rows.Count; i++) 49 { 50 for (int j = 0; j < dt.Columns.Count; j++) 51 { 52 table.AddCell(new Phrase(dt.Rows[i][j].ToString(), font)); 53 } 54 } 55 doc.Add(table); 56 doc.Close(); 57 writer.Close(); 58 } 59 60 /// <summary> 61 /// 在页面中显示PDF 62 /// </summary> 63 /// <param name="sender"></param> 64 /// <param name="e"></param> 65 protected void btnShow_Click(object sender, EventArgs e) 66 { 67 Response.Clear(); 68 Response.ContentType = "Application/pdf"; 69 Response.WriteFile(this.txtPath.Text.Trim()); 70 Response.End(); 71 }
字体文件一般保存在Windows/fonts目录中,扩展名为TTF、TTC,TTC扩展名的字体是一个集合。在Windows系统中集中标准字体的文件名称:
SIMSUN.TTC 宋体和新宋体
SIMKAI.TTF 楷体
SIMHEI.TTF 黑体
SIMFANG.TTF 仿宋体
STZHONGS.TTF 中宋体
字体的应用例子
字体应用
1 Document document = new Document(); 2 PdfWriter.GetInstance(document, new FileStream(@"c:\hello.pdf", FileMode.Create)); 3 BaseFont baseFont = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 4 Font font = new Font(baseFont, 12); 5 document.Open(); 6 document.Add(new Paragraph("测试一下数据", font)); 7 document.Close();
字体集合的应用
字体集合
1 Document document = new Document(); 2 PdfWriter.GetInstance(document, new FileStream(@"c:\hello.pdf", FileMode.Create)); 3 BaseFont baseFont1 = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 4 Font font1 = new Font(baseFont1, 12); 5 6 BaseFont baseFont2 = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMSUN.TTC,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 7 Font font2 = new Font(baseFont2, 12); 8 document.Open(); 9 document.Add(new Paragraph("测试一下数据", font1)); 10 document.Add(new Paragraph("测试一下数据", font2)); 11 document.Close();
如果以下代码,输入中文字体不会显示或者乱码
code
1 Document document = new Document(); 2 PdfWriter.GetInstance(document, new FileStream(@"c:\hello.pdf", FileMode.Create)); 3 BaseFont baseFont1 = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\ROCKB.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 4 Font font1 = new Font(baseFont1, 12); 5 document.Open(); 6 document.Add(new Paragraph("测试一下", font1)); 7 document.Close();
原因是指定的字体是支持英文不支持中文的,此时我们只需要修改我们的指定的字体就可以。
创建table
table
1 Document document = new Document(); 2 PdfWriter.GetInstance(document, new FileStream(@"c:\hello.pdf", FileMode.Create)); 3 document.Open(); 4 iTextSharp.text.Table table = new iTextSharp.text.Table(3); 5 table.Cellpadding = 0; 6 table.Cellspacing = 0; 7 table.BorderWidth = 0.5f; 8 table.BorderColor = Color.BLUE; 9 Cell cell = new Cell(" table header"); 10 11 cell.Colspan = 3; 12 cell.HorizontalAlignment = Element.ALIGN_CENTER; 13 cell.VerticalAlignment = Element.ALIGN_MIDDLE; 14 cell.Header = true; 15 table.AddCell(cell); 16 17 18 cell = new Cell("cell 1"); 19 cell.HorizontalAlignment = Element.ALIGN_CENTER; 20 cell.VerticalAlignment = Element.ALIGN_MIDDLE; 21 cell.Rowspan = 2; 22 table.AddCell(cell); 23 24 cell = new Cell("cell 2"); 25 cell.HorizontalAlignment = Element.ALIGN_CENTER; 26 cell.VerticalAlignment = Element.ALIGN_MIDDLE; 27 table.AddCell(cell); 28 29 cell = new Cell("cell 3"); 30 cell.HorizontalAlignment = Element.ALIGN_CENTER; 31 cell.VerticalAlignment = Element.ALIGN_MIDDLE; 32 table.AddCell(cell); 33 34 cell = new Cell("cell 4"); 35 cell.HorizontalAlignment = Element.ALIGN_CENTER; 36 cell.VerticalAlignment = Element.ALIGN_MIDDLE; 37 table.AddCell(cell); 38 39 cell = new Cell("cell 5"); 40 cell.HorizontalAlignment = Element.ALIGN_CENTER; 41 cell.VerticalAlignment = Element.ALIGN_MIDDLE; 42 table.AddCell(cell); 43 44 cell = new Cell("cell 6"); 45 cell.HorizontalAlignment = Element.ALIGN_CENTER; 46 cell.VerticalAlignment = Element.ALIGN_MIDDLE; 47 table.AddCell(cell); 48 49 cell = new Cell("cell 7"); 50 cell.HorizontalAlignment = Element.ALIGN_CENTER; 51 cell.VerticalAlignment = Element.ALIGN_MIDDLE; 52 cell.Rowspan = 2; 53 cell.Colspan = 2; 54 table.AddCell(cell); 55 56 cell = new Cell("cell 8"); 57 cell.HorizontalAlignment = Element.ALIGN_CENTER; 58 cell.VerticalAlignment = Element.ALIGN_MIDDLE; 59 table.AddCell(cell); 60 document.Add(table); 61 document.Close();
PDF加密
用到PdfWriter实例的setEncryption(bool strength,string userPassword ,string ownerPassWord,int permission)函数
Code
1 Document doc = new Document(); 2 PdfWriter we = PdfWriter.GetInstance(doc, new FileStream(@"c:\d.pdf", FileMode.Create)); 3 we.SetEncryption(PdfWriter.STRENGTH128BITS, "test", "admin", PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING|PdfWriter.ALLOW_MODIFY_CONTENTS); 4 doc.Open(); 5 doc.Add(new Paragraph("Hello World!")); 6 doc.Close();
Permission参数为下列常量
PdfWriter.AllowPrinting
PdfWriter.AllowCopy
......
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
相信技术和创新的力量
喜欢快速反应的工作节奏