最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C#生成PDF文件流
时间:2022-06-25 07:49:31 编辑:袖梨 来源:一聚教程网
1、设置字体
代码如下 | 复制代码 |
staticBaseFont FontBase = BaseFont.CreateFont("C:\WINDOWS\FONTS\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); staticiTextSharp.text.Font bodyFont =newiTextSharp.text.Font(FontBase, 12); staticiTextSharp.text.Font titleFont =newiTextSharp.text.Font(FontBase, 18); staticiTextSharp.text.Font paragraphFont =newiTextSharp.text.Font(FontBase, 15); staticiTextSharp.text.Font linkFont =newiTextSharp.text.Font(FontBase, 12, Font.UNDERLINE, BaseColor.BLUE); |
2.生成PDF文件流返回byte数组
代码如下 | 复制代码 |
publicbyte[] DocCreate(System.Drawing.Image image, List { MemoryStream file =newMemoryStream();
stringfileName =string.Empty; Rectangle page = PageSize.A4; floaty = page.Height; Document document =newDocument(page, 15, 15, 30, 30); floatdocWidth = page.Width - 15 * 2; floatdocHeight = 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 =newParagraph(newChunk("标题", titleFont)); title.Alignment = Element.ALIGN_CENTER; document.Add(title); //图片 iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(image, ImageFormat.Png); floatwidthSzie = (page.Width - 30) / img.Width; if(widthSzie < 1) { img.ScalePercent(widthSzie * 100); } document.Add(img); //文献出处 Paragraph p2 =newParagraph(newChunk("出处", 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 =newParagraph(newChunk(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 =newbyte[newfile.Length]; newfile.Read(bytes, 0, bytes.Length); returnbytes; } |
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22