需要添加引用iTextSharp.dll,具体的添加方式可以通过nuget添加,不做赘述。
public static bool ExportRange2CSV(IEnumerable dataSource, DataGrid grid, string fileName) { try { PdfPTable table1 = new PdfPTable(16); table1.TotalWidth = 820; table1.LockedWidth = true;//锁定宽度 //int[] a = { 0, 1, 1, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; //设置列宽比例 float[] a = { 0, 33, 36, 130, 100, 100, 27, 50, 36, 50, 50, 50, 25, 50, 25, 50 }; //设置列宽比例 table1.SetWidths(a); string str = Export2Text(dataSource, ",", grid.Columns.ToArray()); //File.WriteAllText(fileName, str, encoding); string[] strArray = str.Split(','); foreach (string s in strArray) { // table1.AddCell(s); //添加单元格 } //字体定义 var bfchinese = BaseFont.CreateFont(@"c:\windows\fonts\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//simkai.ttf var ChFont_12 = new Font(bfchinese, 12); var ChFont_10 = new Font(bfchinese, 10); var ChFont_8 = new Font(bfchinese, 8); var ChFont_12_red = new Font(bfchinese, 12, iTextSharp.text.Font.ITALIC, BaseColor.RED); int coordinate = 1; foreach (string str2 in strArray) { PdfPCell cell = new PdfPCell(new Phrase(str2, ChFont_10)); cell.HorizontalAlignment = 1; //居中输入 默认 0:居左 1:居中 if(coordinate>17) cell.HorizontalAlignment = 0; table1.AddCell(cell); //添加单元格 coordinate++; } //定义一个Document,并设置页面大小为A4,竖向 Document doc = new Document(PageSize.A4.Rotate()); try { //写实例 PdfWriter.GetInstance(doc, new FileStream("C:\\Windows\\cache.pdf", FileMode.Create)); #region 设置PDF的头信息,一些属性设置,在Document.Open 之前完成 doc.AddAuthor("Mr J"); doc.AddCreationDate(); doc.AddCreator("HYBIOME"); doc.AddSubject("本文件解释权在医院"); doc.AddTitle("结果导出文件"); doc.AddKeywords("HYBIOME"); //自定义头 doc.AddHeader("Expires", "0"); #endregion //打开document doc.Open(); //载入字体 //BaseFont.AddToResourceSearch("iTextAsian.dll"); //BaseFont.AddToResourceSearch("iTextAsianCmaps.dll"); //"UniGB-UCS2-H" "UniGB-UCS2-V"是简体中文,分别表示横向字 和 // 纵向字 //" STSong-Light"是字体名称 BaseFont baseFT = BaseFont.CreateFont(@"c:\windows\fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font font = new Font(baseFT); //写入一个段落, Paragraph doc.Add(table1); //font.Size = 15; //doc.Add(new Paragraph(str, font)); //关闭document doc.Close(); SetWatermark("C:\\Windows\\cache.pdf", fileName, "HYBIOME", "", "", 123456); //打开PDF,看效果 Process.Start(fileName); } catch (DocumentException de) { Console.WriteLine(de.Message); Console.ReadKey(); } catch (IOException io) { Console.WriteLine(io.Message); Console.ReadKey(); } } catch (Exception) { return false; } return true; } /// <summary> /// 添加倾斜水印 /// </summary> /// <param name="inputfilepath"></param> /// <param name="outputfilepath"></param> /// <param name="waterMarkName"></param> /// <param name="userPassWord"></param> /// <param name="ownerPassWord"></param> /// <param name="permission"></param> public static void SetWatermark(string inputfilepath, string outputfilepath, string waterMarkName, string userPassWord, string ownerPassWord, int permission) { PdfReader pdfReader = null; PdfStamper pdfStamper = null; try { pdfReader = new PdfReader(inputfilepath); pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create)); // 设置密码 //pdfStamper.SetEncryption(false,userPassWord, ownerPassWord, permission); int total = pdfReader.NumberOfPages + 1; PdfContentByte content; BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); PdfGState gs = new PdfGState(); gs.FillOpacity = 0.2f;//透明度 //waterMarkName = waterMarkName + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); int j = waterMarkName.Length; char c; int rise = 0; for (int i = 1; i < total; i++) { rise = 500; //content = pdfStamper.GetOverContent(i);//在内容上方加水印 content = pdfStamper.GetUnderContent(i);//在内容下方加水印 content.SetGState(gs); content.BeginText(); content.SetColorFill(BaseColor.DARK_GRAY); content.SetFontAndSize(font, 100); // 设置水印文字字体倾斜 开始 if (j >= 15) { content.SetTextMatrix(200, 120); for (int k = 0; k < j; k++) { content.SetTextRise(rise); c = waterMarkName[k]; content.ShowText(c + ""); rise -= 20; } } else { content.SetTextMatrix(220, -200); for (int k = 0; k < j; k++) { content.SetTextRise(rise); c = waterMarkName[k]; content.ShowText(c + ""); //rise -= 18; } content.SetFontAndSize(font, 20); content.SetTextMatrix(250, -250); string dateTimeStr= DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); for (int k=0;k<dateTimeStr.Length;k++) { content.SetTextRise(rise); c = dateTimeStr[k]; content.ShowText(c + ""); } } // 字体设置结束 content.EndText(); ////画一个圆 ////content.Ellipse(250, 450, 350, 550); //content.Ellipse(260, 260, 480, 360); //content.SetLineWidth(3f); ////content.SetGState(gs);//试图使其透明 但无效 姜彦20181021 1702 //content.Stroke(); } } catch (Exception ex) { throw ex; } finally { if (pdfStamper != null) pdfStamper.Close(); if (pdfReader != null) pdfReader.Close(); } }