C#生成PDF文件流

作者:恝置 时间:2023-03-19 08:52:56 

本文实例为大家分享了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#,PDF,文件流
0
投稿

猜你喜欢

  • SpringBoot使用Redisson实现分布式锁(秒杀系统)

    2022-07-17 05:15:41
  • C语言手把手带你掌握带头双向循环链表

    2023-01-14 16:52:59
  • kotlin 定义接口并实现回调的例子

    2022-12-06 14:30:19
  • Java实现树形结构的示例代码

    2023-07-30 01:05:19
  • SSH原理及两种登录方法图文详解

    2023-11-14 11:10:53
  • Android 自定义弹性ListView控件实例代码(三种方法)

    2022-04-14 15:00:33
  • Maven+Tomcat8 实现自动化部署的方法

    2023-01-03 06:44:20
  • Android ListView实现简单列表功能

    2022-11-29 12:35:18
  • C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法

    2023-11-24 11:00:37
  • LeetCode程序员面试题之无重复字符的最长子串

    2021-09-12 05:26:36
  • 基于C#实现端口扫描器(单线程和多线程)

    2021-10-16 20:32:23
  • Android7.0版本影响开发的改进分析

    2022-06-06 03:40:37
  • Android中Retrofit 2.0直接使用JSON进行数据交互

    2022-01-08 07:22:46
  • C语言运用函数指针数组实现计算器功能

    2023-10-01 18:45:25
  • 安卓(Android)动态创建多个按钮并添加监听事件

    2023-04-25 16:11:43
  • android 通过MediaRecorder实现简单的录音示例

    2023-07-29 06:03:54
  • SpringBoot微信扫码支付的实现示例

    2023-02-08 03:03:44
  • Android读写文件工具类详解

    2021-10-08 18:08:12
  • Java8 Stream流的常用方法汇总

    2023-07-17 17:56:59
  • C#实现文件上传与下载功能实例

    2022-11-18 07:59:03
  • asp之家 软件编程 m.aspxhome.com