欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C#生成PDF文件流

时间:2020-12-09 20:04:24|栏目:.NET代码|点击:

本文实例为大家分享了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;
    }

上一篇:python实现AutoResetEvent类的阻塞模式方法解析

栏    目:.NET代码

下一篇:ASP.NET Page函数调用顺序解析

本文标题:C#生成PDF文件流

本文地址:http://www.codeinn.net/misctech/30838.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有