上面两篇文章都讲到了pdf,但如何用C#操作pdf的生成和保存等一系列的动作就要用到Itextsharp.dll控件了。
下面简单的介绍一下itextsharp,因为C#中使用itextsharp这类的文章百度一大堆,这里就不多说了。
itextsharp.dll,是一个开源的在C#中用来生成PDF文档的库文件,不少C#爱好者用它制作出了PDF文档生成器。使用时只需在你的C#项目中添加引入此组件即可,使用方法网上有很多,自己查阅一下。如果系统提示“没有找到itextsharp.dll”文件,或者“缺少itextsharp.dll”等错误信息,您可下载本文件后,将其注册为组件或复制到相关目录,即可解决出错提示!
用itextsharp生成pdf有两种方式:
1、pdf模板方式生成pdf文件流(这里涉及到如何制作pdf的模板问题,在ASP.NET,flexpaper,SWFTools 实现简单的PDF显示(四)中将讲到)
/// <summary> /// 根据模板生产pdf二进制文件 /// </summary> /// <param name="TempFileName">模板文件名</param> /// <param name="FieldValues">字典形式数据</param> /// <returns></returns> public static byte[] CreatePDf(string TempFileName, Dictionary<string, string> FieldValues) { MemoryStream ms = new MemoryStream(); string sourcePath = HttpContext.Current.Server.MapPath("PDF") +"\\"+ TempFileName; PdfReader pdfReader = new PdfReader(sourcePath); PdfStamper stamp = new PdfStamper(pdfReader, ms, PdfWriter.VERSION_1_5, false); //stamp.ViewerPreferences = PdfWriter.HideWindowUI; //下面这段为pdf加密设置 //stamp.SetEncryption(PdfWriter.STRENGTH40BITS, null, null,PdfWriter.ALLOW_COPY | PdfWriter.AllowPrinting); //stamp.Writer.CompressionLevel = PdfStream.BEST_COMPRESSION; //stamp.FormFlattening = true; //stamp.SetFullCompression(); //stamp.Writer.CloseStream = false; AcroFields fields = stamp.AcroFields; //字体设置 //1、直接调用系统字体 //BaseFont font = BaseFont.CreateFont("c:\\windows\\fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //2、系统字体放在项目文件夹中调用 //string fontpath = HttpContext.Current.Server.MapPath(@"~/Font"); //BaseFont font = BaseFont.CreateFont(fontpath + "\\DroidSansFallback.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //3、通过itext的中文支持库调用 BaseFont.AddToResourceSearch(HttpContext.Current.Server.MapPath("iTextAsian.dll")); BaseFont.AddToResourceSearch(HttpContext.Current.Server.MapPath("iTextAsianCmaps.dll")); BaseFont font = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); foreach (string _key in FieldValues.Keys) { fields.SetFieldProperty(_key, "textfont", font, null); fields.SetField(_key, FieldValues[_key]); } stamp.FormFlattening = true; stamp.Close(); byte[] array = ms.ToArray(); ms.Close(); return array; }
2、直接代码方式生成pdf文件流(下面这段代码写的只是itextsharp的简单应用,itextsharp的功能远不止这些,还需深入研究......)
public byte[] CreatePDF_Doc(Dictionary<string, string> _ret) { MemoryStream ms = new MemoryStream(); //加载字体文件dll BaseFont.AddToResourceSearch(HttpContext.Current.Server.MapPath("Bin")+"\\iTextAsian.dll"); BaseFont.AddToResourceSearch(HttpContext.Current.Server.MapPath("Bin") + "\\iTextAsianCmaps.dll"); BaseFont font = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); itex.Font fontSize14 = new itex.Font(font, 14); itex.Font fontSize12 = new itex.Font(font, 12); itex.Font fontULine = new itex.Font(font, 14, itex.Font.UNDERLINE, itex.BaseColor.GRAY); itex.Font fontBlack = new itex.Font(font, 16, itex.Font.BOLD, itex.BaseColor.BLACK); itex.Document document = new itex.Document(itex.PageSize.A4); PdfWriter.GetInstance(document, ms); if (!document.IsOpen()) { document.Open(); } itex.Chunk chunk1 = new itex.Chunk("中华人民共和国", fontBlack); itex.Chunk chunk2 = new itex.Chunk(_ret["HGMC"], fontBlack); chunk2.SetUnderline(0.5f, -1.5f); itex.Chunk chunk3 = new itex.Chunk("海关", fontBlack); itex.Phrase phrase1 = new itex.Phrase(); phrase1.Add(chunk1); phrase1.Add(chunk2); phrase1.Add(chunk3); itex.Chunk chunk4 = new itex.Chunk("扣留清单", fontBlack); itex.Paragraph paragraph1 = new itex.Paragraph(phrase1); paragraph1.Alignment = itex.Rectangle.ALIGN_CENTER; itex.Paragraph paragraph2 = new itex.Paragraph(chunk4); paragraph2.Alignment = itex.Element.ALIGN_CENTER; document.Add(paragraph1); document.Add(paragraph2); itex.Chunk chunk5 = new itex.Chunk(" 根据", fontSize14); itex.Chunk chunk6 = new itex.Chunk(_ret["AJBH"], fontSize14); chunk6.SetUnderline(0.5f, -1.5f); itex.Chunk chunk7 = new itex.Chunk("《扣留(封存)决定书》,扣留(封存)对象列明如下:", fontSize14); itex.Phrase phrase2 = new itex.Phrase(); phrase2.Add(chunk5); phrase2.Add(chunk6); phrase2.Add(chunk7); itex.Paragraph paragraph3 = new itex.Paragraph(phrase2); paragraph3.Alignment = itex.Rectangle.ALIGN_LEFT; //行距 paragraph3.MultipliedLeading = 3f; document.Add(paragraph3); itex.Paragraph paragraph4 = new itex.Paragraph(" "); paragraph4.MultipliedLeading = 1f; document.Add(paragraph4); PdfPTable table = new PdfPTable(6); table.HorizontalAlignment = 1; table.TotalWidth = 600f; table.DefaultCell.HorizontalAlignment = 1; table.AddCell(new itex.Phrase("序号", fontSize12)); table.AddCell(new itex.Phrase("名称", fontSize12)); table.AddCell(new itex.Phrase("规格", fontSize12)); table.AddCell(new itex.Phrase("数量", fontSize12)); table.AddCell(new itex.Phrase("单位", fontSize12)); table.AddCell(new itex.Phrase("备注", fontSize12)); for (int i = 0; i < dt.Rows.Count; i++) { int j = i + 1; table.AddCell(j.ToString()); table.AddCell(" ", fontSize12)); table.AddCell(new itex.Phrase(" ", fontSize12)); table.AddCell(new itex.Phrase(" ", fontSize12)); table.AddCell(new itex.Phrase(" ",fontSize12)); table.AddCell(new itex.Phrase(" ",fontSize12)); } document.Add(table); itex.Chunk chunk8 = new itex.Chunk("扣留(封存)地点:", fontSize12); itex.Chunk chunk9 = new itex.Chunk(_ret["KLDZ"], fontSize12); chunk9.SetUnderline(0.5f, -1.5f); itex.Phrase phrase3 = new itex.Phrase(); phrase3.Add(chunk8); phrase3.Add(chunk9); itex.Paragraph paragraph5 = new itex.Paragraph(phrase3); paragraph5.MultipliedLeading = 2f; paragraph5.Alignment = itex.Rectangle.ALIGN_LEFT; paragraph5.IndentationLeft = 55f; document.Add(paragraph5); itex.Chunk chunk10 = new itex.Chunk("当事人(代理人)签章:己收到", fontSize12); itex.Chunk chunk11 = new itex.Chunk(_ret["KLNR"], fontSize12); chunk11.SetUnderline(0.5f, -1.5f); itex.Chunk chunk12 = new itex.Chunk("《扣留(封存)决定书》", fontSize12); itex.Phrase phrase4 = new itex.Phrase(); phrase4.Add(chunk10); phrase4.Add(chunk11); phrase4.Add(chunk12); itex.Paragraph paragraph6 = new itex.Paragraph(phrase4); paragraph6.MultipliedLeading = 2f; paragraph6.Alignment = itex.Rectangle.ALIGN_LEFT; paragraph6.IndentationLeft = 55f; document.Add(paragraph6); itex.Chunk chunk13 = new itex.Chunk("及本《扣留(封存)清单》。", fontSize12); itex.Chunk chunk14 = new itex.Chunk(_ret["ZFR"], fontSize12); chunk14.SetUnderline(0.5f, -1.5f); itex.Phrase phrase5 = new itex.Phrase(); phrase5.Add(chunk13); phrase5.Add(chunk14); itex.Paragraph paragraph7 = new itex.Paragraph(phrase5); paragraph7.MultipliedLeading = 2f; paragraph7.Alignment = itex.Rectangle.ALIGN_LEFT; paragraph7.IndentationLeft = 55f; document.Add(paragraph7); itex.Chunk chunk15 = new itex.Chunk("执法人员签名:", fontSize12); itex.Chunk chunk16 = new itex.Chunk(_ret["JZR"], fontSize12); chunk16.SetUnderline(0.5f, -1.5f); itex.Chunk chunk17 = new itex.Chunk("见证人签名:", fontSize12); itex.Chunk chunk18 = new itex.Chunk(_ret["AJBH2"], fontSize12); chunk18.SetUnderline(0.5f, -1.5f); itex.Phrase phrase6 = new itex.Phrase(); phrase6.Add(chunk15); phrase6.Add(chunk16); phrase6.Add(chunk17); phrase6.Add(chunk18); itex.Paragraph paragraph8 = new itex.Paragraph(phrase6); paragraph8.MultipliedLeading = 2f; paragraph8.Alignment = itex.Rectangle.ALIGN_LEFT; paragraph8.IndentationLeft = 55f; document.Add(paragraph8); itex.Paragraph paragraph9 = new itex.Paragraph(" "); paragraph9.MultipliedLeading = 3f; document.Add(paragraph9); itex.Paragraph paragraph10 = new itex.Paragraph(DateTime.Now.Year + " 年 " + DateTime.Now.Month + " 月 " + DateTime.Now.Day + " 日 ", fontSize12); paragraph10.Alignment = itex.Rectangle.ALIGN_RIGHT; document.Add(paragraph10); document.Close(); byte[] array = ms.ToArray(); #if DEBUG string fname = HttpContext.Current.Server.MapPath("WSTemp") + "\\扣留清单" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".PDF"; FileStream sFile = new FileStream(fname, FileMode.CreateNew); sFile.Write(array, 0, array.Length); sFile.Close(); #endif return array; }
注:上面这两种示例代码中我都使用了如下的字体代码:
BaseFont.AddToResourceSearch(HttpContext.Current.Server.MapPath("Bin")+"\\iTextAsian.dll");
BaseFont.AddToResourceSearch(HttpContext.Current.Server.MapPath("Bin") + "\\iTextAsianCmaps.dll");
BaseFont font = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
关于这种字体在ASP.NET,flexpaper,SWFTools 实现简单的PDF显示(二)Itexsharp生成pdf字体问题也说过,生成的pdf在flexpaper+SWFTools结合中无法显示,具体的原因我找了很多资料也没有没有答案,不过如果要显示这种字体生成的文书也有其他的方式,这个也准备在接下来的ASP.NET,flexpaper,SWFTools 实现简单的PDF显示(五)涉及。不过使用这种字体有个好处就是在pdf支持中文的前提下生成最小的pdf文件流即文件。