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 ...
随机推荐
- css根据子元素多少类设置子元素的属性--九宫格
.moment-image-div:nth-child(n+1):nth-last-child(-n+9){ width: 33.33%; float: left; } .moment-image-d ...
- ftp权限设置大全!!!
1.登录和对匿名用户的设置write_enable=YES //是否对登录用户开启写权限.属全局性设置.默认NOlocal_enable=YES ...
- Object类型的转为String类型
Map<String, Object> scaleMap = new HashMap(): scaleMap.put("name","张三"); S ...
- PHP5.6 Dockerfile
FROM centos COPY ["src","/src"] RUN groupadd -g 1000 www \ && useradd -u ...
- php在cli模式下取得命令行中的参数的方法-getopt命令行可传递数组-简单自定义方法取命令行参数
在cli模式下执行PHP时,自动给脚本文件传递了一个变量$argv,其值即是一个命令中所有值组成的数组(以空格区分),在PHP程序中接收参数有3种方法1.直接使用argv变量数组. 2.使用$_SER ...
- A Summary of Multi-task Learning
A Summary of Multi-task Learning author by Yubo Feng. Intro In this paper[0], the introduction of mu ...
- oracle中number数据类型简单明了解释
NUMBER (p,s) p和s范围: p 1-38 s -84-127 number(p,s),s大于0,表示有效位最大为p,小数位最多为s,小数点右边s位置开始四舍五入,若s>p,小数点右侧 ...
- sql 根据表名查找存储过程
SELECT obj.Name, sc.TEXT FROM syscomments sc INNER JOIN sysobjects obj ON sc.Id = obj.ID WHERE sc.TE ...
- Win32汇编学习(8):菜单
这次我们将在我们的应用程序中加入一个菜单. 理论: 菜单可以说是WINDOWS最重要的元素之一.有了它,用户可以方便地选择操作命令.用户只要细读一下所有的菜单项就可以明了应用程序所提供的大概功能,而且 ...
- 【ASP.NET】 Config Error: This configuration section cannot be used at this path.
Config Error: This configuration section cannot be used at this path. This happens when the section ...