本文实例为大家分享了C#生成pdf文件流的具体代码,供大家参考,具体内容如下
1、设置字体
static BaseFont FontBase = BaseFont.CreateFont("C:\windows\FontS\STSONG.TTF",BaseFont.IDENTITY_H,BaseFont.EMbedDED); static iTextSharp.text.Font bodyFont = new iTextSharp.text.Font(FontBase,12); static iTextSharp.text.Font TitleFont = new iTextSharp.text.Font(FontBase,18); static iTextSharp.text.Font paragraphFont = new iTextSharp.text.Font(FontBase,15); static iTextSharp.text.Font linkFont = new iTextSharp.text.Font(FontBase,12,Font.UNDERliNE,Basecolor.BLUE);
2.生成pdf文件流返回byte数组
public byte[] DocCreate(Sy@[email protected] image,List<TreeNodes> List) { MemoryStream file = new MemoryStream(); string filename = string.Empty; Rectangle page = PageSize.A4; float y = page.Height; document document = new document(page,15,30,30); float docWIDth = page.WIDth - 15 * 2; float docHeight = page.Height - document.Bottommargin - document.topmargin; pdfWriter writer = pdfWriter.GetInstance(document,file); writer.CloseStream = false; writer.open(); PdfcontentByte cb = writer.DirectContent; document.open(); //标题 Paragraph Title = new Paragraph(new Chunk("标题",TitleFont)); Title.Alignment = Element.AliGN_CENTER; document.Add(Title); //图片 iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(image,ImageFormat.Png); float wIDthSzIE = (page.WIDth - 30) / img.WIDth; if (wIDthSzIE < 1) { img.ScalePercent(wIDthSzIE * 100); } document.Add(img); //文献出处 Paragraph p2 = new Paragraph(new Chunk("出处",paragraphFont)); p2.Indentationleft = indentationleft; document.Add(p2); InitData(List);//初始化业务数据 CreateSteps(List,document,List.FirstOrDefault(it => it.PID == 0));//添加业务数据 ////添加印章 //iTextSharp.text.Image whyz = iTextSharp.text.Image.GetInstance(whyzPath); //whyz.ScalePercent(50); //whyz.paddingtop = 100; //whyz.Alignment = Element.AliGN_RIGHT; //document.Add(whyz); //添加日期 Paragraph createtime = new Paragraph(new Chunk(DateTime.Now.TolongDateString().ToString(),bodyFont)); createtime.Alignment = Element.AliGN_RIGHT; //createtime.SpacingBefore = -80; createtime.paddingtop = 200; document.Add(createtime); document.Close(); file.position = 0; MemoryStream newfile = SetWaterMark(file,"水印内容",docWIDth,docHeight);//添加水印,见另外一篇博客 newfile.position = 0;//重置流指针位置 byte[] bytes = new byte[newfile.Length]; newfile.Read(bytes,bytes.Length); return bytes; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的C#生成PDF文件流全部内容,希望文章能够帮你解决C#生成PDF文件流所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)