C#生成PDF文件流
更新時間:2017年03月24日 10:32:01 作者:恝置
這篇文章主要為大家詳細(xì)介紹了C#生成PDF文件流的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(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;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
c#中WinForm使用OpencvSharp4實(shí)現(xiàn)簡易抓邊
本文主要介紹了c#中WinForm使用OpencvSharp4實(shí)現(xiàn)簡易抓邊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
C#實(shí)現(xiàn)凍結(jié)Excel窗口以鎖定行列或解除凍結(jié)
在處理大型Excel工作簿時,有時候我們需要在工作表中凍結(jié)窗格,這樣可以在滾動查看數(shù)據(jù)的同時保持某些行或列固定不動,下面我們就來看看如何使用C#實(shí)現(xiàn)凍結(jié)Excel窗口吧2024-04-04
C#使用第三方組件實(shí)現(xiàn)動態(tài)解析和求值字符串表達(dá)式
這篇文章主要介紹了C#如何使用第三方組件(LambdaParser、DynamicExpresso、Z.Expressions)實(shí)現(xiàn)動態(tài)解析和求值字符串表達(dá)式,感興趣的小伙伴可以跟隨小編一起了解一下2022-06-06
C#使用代碼實(shí)現(xiàn)春晚撲克牌魔術(shù)
這篇文章主要為大家詳細(xì)介紹了C#如何使用代碼實(shí)現(xiàn)龍年春晚撲克牌魔術(shù)(守歲共此時),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-02-02

