C#生成PDF文件流
本文实例为大家分享了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(System.Drawing.Image 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, 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, 0, bytes.Length); return bytes; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
栏 目:C#教程
下一篇:C# 在PDF中创建和填充域
本文标题:C#生成PDF文件流
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5826.html
您可能感兴趣的文章
- 01-10C#实现多线程下载文件的方法
- 01-10C#文件断点续传实现方法
- 01-10C#实现多线程写入同一个文件的方法
- 01-10C#编程获取资源文件中图片的方法
- 01-10C#实现读取被进程占用的文件实现方法
- 01-10C#删除只读文件或文件夹(解决File.Delete无法删除文件)
- 01-10C# readnodefile()不能读取带有文件名为汉字的osg文件解决方法
- 01-10C#路径,文件,目录及IO常见操作汇总
- 01-10C#获取动态生成的CheckBox值
- 01-10winform实现拖动文件到窗体上的方法
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 01-10C#中split用法实例总结
- 01-10delphi制作wav文件的方法
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11ajax实现页面的局部加载
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10SublimeText编译C开发环境设置
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 04-02jquery与jsp,用jquery