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, );
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文件流的更多相关文章
- Itext生成pdf文件
来源:https://my.oschina.net/lujianing/blog/894365 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等. ...
- pdf文件流生成pdf文件
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Code.Login Starr_mode ...
- asp.net MVC设计模式中使用iTextSharp实现html字符串生成PDF文件
因个人需求,需要将html格式转换成PDF并加上水印图片.于是乎第一次接触这种需求的小菜鸟博主我,在某度搜索引擎上不断的查阅关键字资料.踩坑,终于有了一个相应的解决方案.以下是解决步骤,记录下来方便以 ...
- 小容量单片机生成pdf文件
工作上要求使用小容量单片机生成直接生成pdf文件.经过一段时间的摸索,其中参考了libharu,库太大,不适合在单片机上使用页参考了与非网上一位前辈的库,占用的RAM太大,不适合小容量单片机,主要资料 ...
- [itext]Java生成PDF文件
一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...
- 在C#.NET中,如何生成PDF文件?主要有以下几个途径
1.使用.NET文件流技术:若通过.NET的文件流技术生成PDF文件,必须对PDF文件的语法很清楚,例如BT表示实体内容开始:ET表示实体内容结束:TD表示换行等等.我们可以从Adobe的官方网站上下 ...
- 记-ItextPDF+freemaker 生成PDF文件---导致服务宕机
摘要:已经上线的项目,出现服务挂掉的情况. 介绍:该服务是专门做打印的,业务需求是生成PDF文件进行页面预览,主要是使用ItextPDF+freemaker技术生成一系列PDF文件,其中生成流程有:解 ...
- springMVC生成pdf文件
pom.xml文件配置=== <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <dependenc ...
- linux下编译bib、tex生成pdf文件
实验: 在linux环境下,编译(英文)*.bib和*.tex文件,生成pdf文件. 环境: fedora 20(uname -a : Linux localhost.localdomain 3.19 ...
随机推荐
- Shell read交互
read语句:设定客户端交互的任意输出值. 参数: -a 后跟一个变量,该变量会被认为是个数组,然后给其赋值,默认是以空格为分割符. -d 后面跟一个标志符,其实只有其后的第一个字符有用,作为结束的标 ...
- 关于MVC RouteExistingFiles疑问
如图,使用ajpg模拟静态资源.使用 routes.RouteExistingFiles = true; 使静态资源也Map Route,匹配不到则显示Not Foud. ①/a.jpg:访问不到 ② ...
- 从实验室走向世界:HSP90抑制剂,一种新的癌症药物
热休克蛋白90(HSP90)是细胞内一种普遍存在的.十分保守的以及有高度活性的蛋白质,它在肿瘤细胞内的含量要比正常细胞多.作为一种分子伴侣,HSP90协助不同种类的癌蛋白(即HSP90的服务蛋白)进行 ...
- hdu 5564 Clarke and digits 矩阵快速幂优化数位dp
Clarke and digits Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- redhat7.2安全基线BI
(一) Redhat linux7.2安全基线基本型(BI) 1. 密码复杂度策略 /etc/pam.d/system-auth文件中,增加内容 password requisite pam_ ...
- Lab 7-1
Analyze the malware found in the file Lab07-01.exe. Questions and Short Answers How does this progra ...
- linux的基本操作概览
目录 文件路径相关的命令 最重要的一个通配符:* zip相关命令 重定向 三种权限 一些网络操作 安装软件的三种方式 最近开始学习linux的一些基础操作,安装了ubantu 16版本的Linux操作 ...
- 查看局域网指定IP的电脑名
nbtstat -a 192.168.0.139 节点 IP 址址: [192.168.0.140] 范围 ID: [] NetBIOS 远程计算机名称表 名称 类型 状态 ------------- ...
- Spring cloud系列之Zuul配置项中sensitiveHeaders和ignoredHeaders
sensitiveHeaders:会过滤客户端请求中的和该配置项匹配的headers比如: zuul: sensitiveHeaders: X-ABC 如果客户端在发请求是带了X-ABC,那么X-AB ...
- zzw原创_非root用户启动apache的问题解决(非root用户启动apache的1024以下端口)
场景:普通用户编译的apache,要在该用户下启动1024端口以下的apache端口 1.假设普通用户为sims20,用该用户编译 安装了一个apache,安装路径为/opt/aspire/produ ...