C#生成PDF文件流
更新時(shí)間:2017年03月24日 10:32:01 作者:恝置
這篇文章主要為大家詳細(xì)介紹了C#生成PDF文件流的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C#生成PDF文件流的具體代碼,供大家參考,具體內(nèi)容如下
1、設(shè)置字體
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, 12);
static iTextSharp.text.Font titleFont = new iTextSharp.text.Font(FontBase, 18);
static iTextSharp.text.Font paragraphFont = new iTextSharp.text.Font(FontBase, 15);
static iTextSharp.text.Font linkFont = new iTextSharp.text.Font(FontBase, 12, Font.UNDERLINE, BaseColor.BLUE);
2.生成PDF文件流返回byte數(shù)組
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();
//標(biāo)題
Paragraph title = new Paragraph(new Chunk("標(biāo)題", 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);
//文獻(xiàn)出處
Paragraph p2 = new Paragraph(new Chunk("出處", paragraphFont));
p2.IndentationLeft = indentationLeft;
document.Add(p2);
InitData(list);//初始化業(yè)務(wù)數(shù)據(jù)
CreateSteps(list, document, list.FirstOrDefault(it => it.PID == 0));//添加業(yè)務(wù)數(shù)據(jù)
////添加印章
//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, "水印內(nèi)容", docWidth, docHeight);//添加水印,見另外一篇博客
newfile.Position = 0;//重置流指針位置
byte[] bytes = new byte[newfile.Length];
newfile.Read(bytes, 0, bytes.Length);
return bytes;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WPF實(shí)現(xiàn)動(dòng)畫效果(三)之時(shí)間線(TimeLine)
這篇文章介紹了WPF實(shí)現(xiàn)動(dòng)畫效果之時(shí)間線(TimeLine),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C#實(shí)現(xiàn)Json轉(zhuǎn)DataTable并導(dǎo)出Excel的方法示例
這篇文章主要介紹了C#實(shí)現(xiàn)Json轉(zhuǎn)DataTable并導(dǎo)出Excel的方法,結(jié)合實(shí)例形式總結(jié)分析了Json轉(zhuǎn)換DataTable,以及DataTable導(dǎo)出Excel相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
Winform消除button按下出現(xiàn)的虛線簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了Winform消除button按下出現(xiàn)的虛線簡(jiǎn)單實(shí)現(xiàn)方法,通過重寫button設(shè)置Selectable參數(shù)實(shí)現(xiàn)該功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08

