• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#生成PDF文件流

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1。设置字体

1 static BaseFont FontBase = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
2         static iTextSharp.text.Font bodyFont = new iTextSharp.text.Font(FontBase, 12);
3         static iTextSharp.text.Font titleFont = new iTextSharp.text.Font(FontBase, 18);
4         static iTextSharp.text.Font paragraphFont = new iTextSharp.text.Font(FontBase, 15);
5         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;
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#高效编程发布时间:2022-07-10
下一篇:
C#入门面向对象编程(方法)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap