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, );
static iTextSharp.text.Font titleFont = new iTextSharp.text.Font(FontBase, );
static iTextSharp.text.Font paragraphFont = new iTextSharp.text.Font(FontBase, );
static iTextSharp.text.Font linkFont = new iTextSharp.text.Font(FontBase, , 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, , , , );
float docWidth = page.Width - * ;
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 - ) / img.Width;
if (widthSzie < )
{
img.ScalePercent(widthSzie * );
}
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 == ));//添加业务数据
////添加印章
//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 = ; document.Add(createtime); document.Close();
file.Position = ;
MemoryStream newfile = SetWaterMark(file, "水印内容", docWidth, docHeight);//添加水印,见另外一篇博客
newfile.Position = ;//重置流指针位置
byte[] bytes = new byte[newfile.Length];
newfile.Read(bytes, , bytes.Length);
return bytes;
}

C#生成PDF文件流的更多相关文章

  1. Itext生成pdf文件

    来源:https://my.oschina.net/lujianing/blog/894365 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等. ...

  2. pdf文件流生成pdf文件

    protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Code.Login Starr_mode ...

  3. asp.net MVC设计模式中使用iTextSharp实现html字符串生成PDF文件

    因个人需求,需要将html格式转换成PDF并加上水印图片.于是乎第一次接触这种需求的小菜鸟博主我,在某度搜索引擎上不断的查阅关键字资料.踩坑,终于有了一个相应的解决方案.以下是解决步骤,记录下来方便以 ...

  4. 小容量单片机生成pdf文件

    工作上要求使用小容量单片机生成直接生成pdf文件.经过一段时间的摸索,其中参考了libharu,库太大,不适合在单片机上使用页参考了与非网上一位前辈的库,占用的RAM太大,不适合小容量单片机,主要资料 ...

  5. [itext]Java生成PDF文件

    一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...

  6. 在C#.NET中,如何生成PDF文件?主要有以下几个途径

    1.使用.NET文件流技术:若通过.NET的文件流技术生成PDF文件,必须对PDF文件的语法很清楚,例如BT表示实体内容开始:ET表示实体内容结束:TD表示换行等等.我们可以从Adobe的官方网站上下 ...

  7. 记-ItextPDF+freemaker 生成PDF文件---导致服务宕机

    摘要:已经上线的项目,出现服务挂掉的情况. 介绍:该服务是专门做打印的,业务需求是生成PDF文件进行页面预览,主要是使用ItextPDF+freemaker技术生成一系列PDF文件,其中生成流程有:解 ...

  8. springMVC生成pdf文件

    pom.xml文件配置=== <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <dependenc ...

  9. linux下编译bib、tex生成pdf文件

    实验: 在linux环境下,编译(英文)*.bib和*.tex文件,生成pdf文件. 环境: fedora 20(uname -a : Linux localhost.localdomain 3.19 ...

随机推荐

  1. python核心技术

    基本语法 Python的设计目标之一是让代码具备高度的可阅读性.它设计时尽量使用其它语言经常使用的标点符号和英文单字,让代码看起来整洁美观.它不像其他的静态语言如C.Pascal那样需要重复书写声明语 ...

  2. .net core webapi+vue 跨域访问

    最近在做一个前后端分离的示例,以下代码完美解决跨域的问题 一.后端服务 1.首先我们建一个.net core webapi的项目 2.项目引用Microsoft.AspNetCore.Cors 包 3 ...

  3. EPOCH batchsize

    只有在数据很庞大的时候(在机器学习中,几乎任何时候都是),我们才需要使用 epochs,batch size,迭代这些术语,在这种情况下,一次性将数据输入计算机是不可能的.因此,为了解决这个问题,我们 ...

  4. myeclipse项目在Tomcat服务器部署问题

    错误信息:Deployment of project mybook will replace this resource. Please specify the action you wish to ...

  5. Html Link 标签

    Html Link 标签 Link 是 HTML Head 内部标签 <html> <head> <!-- link标签:rel="shortcut icon& ...

  6. dubbo多网卡时,服务提供者的错误IP注册到注册中心导致消费端连接不上

    使用了虚拟机之后,启动了dubbo服务提供者应用,又连了正式环境的注册中心: 一旦dubbo获取的ip错误后, 这种情况即使提供者服务停掉,目前dubbo没有能力清除这类错误的提供者: (需要修改源码 ...

  7. Git冲突和解决冲突-测试方法

    原文链接:https://www.cnblogs.com/blogslee/p/6828659.html

  8. zabbix/自动发现规则

      对于zabbix 我们并不陌生 他是开源的监控系统,现在的一部分企业都在用zabbix,今天给大家分享的是企业级监控zabbix的自动发现规则,有了它我们自定义健康的时候根据宏值可以让他自动发现对 ...

  9. 微信小程序实现图片裁剪上传(wepy)

    参考https://github.com/we-plugin/we-cropper,在wepy中实现,参考的具体例子是we-cropper/example/cutInside/ 项目上传图片时2:3的 ...

  10. 【Entity Framework】Revert the database to specified migration.

    本文涉及的相关问题,如果你的问题或需求有与下面所述相似之处,请阅读本文 [Entity Framework] Revert the database to specified migration. [ ...