C# 利用PdfSharp生成Pdf文件的示例
PdfSharp一款開(kāi)源的用于創(chuàng)建,操作PDF文檔的.Net類庫(kù),本文以一個(gè)簡(jiǎn)單的小例子,簡(jiǎn)述如何通過(guò)PdfSharp進(jìn)行創(chuàng)建PDF文檔,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正。
PdfSharp下載
在本例中,主要通過(guò)NuGet包管理器進(jìn)行下載安裝,目前PdfSharp版本為v1.5.0.5147,如下所示:
涉及知識(shí)點(diǎn)
在生成PDF文檔過(guò)程中,主要知識(shí)點(diǎn)如下:
- PdfDocument : 表示一個(gè)PDF文檔對(duì)象,調(diào)用save方法保存文檔到指定路徑。
- PdfPage : 表示PDF文檔中的一頁(yè)。
- XGraphics:表示頁(yè)面上的繪制對(duì)象,所有的頁(yè)面內(nèi)容,都可通過(guò)此對(duì)象繪制。如:DrawString繪制文本內(nèi)容,DrawLine繪制直線等。
- XFont:繪制文本的字體,字體名稱只能取C:\Windows\Fonts目錄下的ttf字體文件,不能是ttc格式的字體。
- XTextFormatter:表示一個(gè)簡(jiǎn)單的文本字體格式,如識(shí)別文本的換行符而實(shí)現(xiàn)自動(dòng)換行等內(nèi)容。
文檔示例圖
在本例中,主要是將頁(yè)面內(nèi)容寫(xiě)入PDF文件中,頁(yè)面如下所示:
生成的PDF文件,如下所示:
核心代碼
在本例中,核心代碼主要包括如下幾個(gè)部分:
- 創(chuàng)建文檔
- 繪制文本
- 繪制直線
- 設(shè)置紙張大小,
- 設(shè)置邊界
- 文本的自動(dòng)換行
具體代碼,如下所示:
/// <summary> /// 生成Pdf /// </summary> /// <param name="filePath"></param> /// <param name="bo"></param> /// <returns></returns> public bool GeneratePdf(string filePath, PdfBo bo) { int margin_left_right = 30;//左右邊距 int margin_top_bottom = 30;//上下邊距 //1. 定義文檔對(duì)象 PdfDocument document = new PdfDocument(); //2. 新增一頁(yè) PdfPage page = document.AddPage(); // 設(shè)置紙張大小 page.Size = PageSize.A4; //3. 創(chuàng)建一個(gè)繪圖對(duì)象 XGraphics gfx = XGraphics.FromPdfPage(page); XFont font = new XFont("華文宋體", 40, XFontStyle.Bold); //定制化內(nèi)容開(kāi)始 int cur_x = 0 + margin_left_right; int cur_y = 0 + margin_top_bottom; //標(biāo)題1 gfx.DrawString(bo.Head1, font, XBrushes.Red, new XRect(cur_x, cur_y, page.Width-2*cur_x, 80), XStringFormats.Center); //序號(hào) font = new XFont("華文宋體", 12, XFontStyle.Regular); cur_y = cur_y + 80; gfx.DrawString(bo.No, font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft); //密級(jí) cur_x = cur_x + 200; gfx.DrawString(string.Format("密級(jí)[{0}]",bo.Private), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft); //緩級(jí) cur_x = cur_x + 100; gfx.DrawString(string.Format("緩級(jí)[{0}]", bo.Speed), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft); //簽發(fā)人 cur_x = cur_x + 100; gfx.DrawString(string.Format("簽發(fā)人:{0}", bo.Person), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft); //一條橫線 cur_x = 0 + margin_left_right; cur_y = cur_y + 20; XPen pen = new XPen(XColor.FromKnownColor(XKnownColor.Black), 1); gfx.DrawLine(pen, cur_x, cur_y, page.Width-cur_x, cur_y+2); //標(biāo)題2 font = new XFont("華文宋體", 20, XFontStyle.Regular); cur_y = cur_y + 10; gfx.DrawString(bo.Head2, font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width-2*cur_x, 40), XStringFormats.Center); //抬頭 font = new XFont("華文宋體", 15, XFontStyle.Bold); cur_y = cur_y + 40; gfx.DrawString(bo.Title, font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width, 40), XStringFormats.CenterLeft); //正文 ,自動(dòng)換行 cur_y = cur_y + 40; XTextFormatter tf = new XTextFormatter(gfx); font = new XFont("華文宋體", 12, XFontStyle.Regular); //測(cè)量當(dāng)前內(nèi)容下,一行可以多少個(gè)漢字 int cnt = 0; int height = 0; for (int i = 0; i < bo.Content.Length; i++) { XSize xsize=gfx.MeasureString(bo.Content.Substring(0,i+1), font, XStringFormats.TopLeft); double width = xsize.Width; if (width >= page.Width - 2 * cur_x) { cnt = i; //表示一行可以放多少個(gè)漢字。 height =(int) xsize.Height; break; } } cnt = cnt > 0 ? cnt : bo.Content.Length;//每一行多少漢字 string[] arrContent = bo.Content.Split('\n'); string new_content = ""; int total_lines = 0; foreach (string content in arrContent) { if (content.Length <= cnt) { new_content+=string.Format("{0}\n",content); total_lines++; } else { string tmpContent = content; int lines = content.Length / cnt + 1; for (int j = 0; j < lines; j++) { tmpContent = tmpContent.Insert(j * cnt, "\n"); total_lines++; } new_content += string.Format("{0}\n", tmpContent); } } int num = new_content.Length - new_content.Replace("\r", "").Length; //計(jì)算矩形 XRect rect = new XRect(cur_x, cur_y, page.Width - 2 * cur_x, (total_lines+num)*(height+2)); tf.DrawString(new_content, font, XBrushes.Black, rect, XStringFormats.TopLeft); //主題詞 cur_y = cur_y + (total_lines + num) * (height + 2) + 20; font = new XFont("華文宋體", 12, XFontStyle.Bold); gfx.DrawString(string.Format("主題詞:{0}",bo.Keyword), font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width, 40), XStringFormats.CenterLeft); //再加一條橫線 cur_y = cur_y + 40; gfx.DrawLine(pen, cur_x, cur_y, page.Width - cur_x, cur_y + 2); cur_y = cur_y + 2; font = new XFont("華文宋體", 10, XFontStyle.Regular); gfx.DrawString(string.Format("{0}{1}",bo.Company, bo.Dept), font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width-2*cur_x, 40), XStringFormats.CenterLeft); gfx.DrawString(DateTime.Now.ToString("yyyy 年 MM 月 dd 日 印發(fā)"), font, XBrushes.Black, new XRect(cur_x, cur_y, page.Width-2*cur_x, 40), XStringFormats.CenterRight); //水印開(kāi)始 font = new XFont("華文宋體", 20, XFontStyle.BoldItalic); // 計(jì)算長(zhǎng)度 var size = gfx.MeasureString(bo.Watermark, font); // 定義旋轉(zhuǎn)中心 gfx.TranslateTransform(page.Width / 2, page.Height / 2); gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI); gfx.TranslateTransform(-page.Width / 2, -page.Height / 2); // 字符樣式 var format = new XStringFormat(); format.Alignment = XStringAlignment.Near; format.LineAlignment = XLineAlignment.Near; //畫(huà)刷 XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0)); for (int i = 0; i < 3; i++) { gfx.DrawString(bo.Watermark, font, brush, new XPoint((page.Width - size.Width) / (1.5+i*0.5), (page.Height - size.Height) / (1.5 + i * 0.5)), format); } //水印結(jié)束 //6. 保存文檔 document.Save(filePath); return true; }
以上就是C# 利用PdfSharp生成Pdf文件的示例的詳細(xì)內(nèi)容,更多關(guān)于c# 生成Pdf文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#項(xiàng)目彩票選號(hào)實(shí)現(xiàn)思路
今天做了一個(gè)彩票選號(hào)的小軟件,將學(xué)到的知識(shí)點(diǎn)總結(jié)一下,下面通過(guò)本文給大家分享C#項(xiàng)目彩票選號(hào)實(shí)現(xiàn)思路,感興趣的朋友跟隨小編一起看看吧2024-08-08C#實(shí)現(xiàn)修改系統(tǒng)時(shí)間的方法
這篇文章主要介紹了C#實(shí)現(xiàn)修改系統(tǒng)時(shí)間的方法,是一個(gè)非常實(shí)用的功能,需要的朋友可以參考下2014-07-07C#Url操作類封裝、仿Node.Js中的Url模塊實(shí)例
這篇文章主要介紹了C#Url操作類封裝、仿Node.Js中的Url模塊,實(shí)例分析了C#Url操作類封裝的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-10-10C#基礎(chǔ)教程之IComparable用法,實(shí)現(xiàn)List<T>.sort()排序
這篇文章主要介紹了C#的一些基礎(chǔ)知識(shí),主要是IComparable用法,實(shí)現(xiàn)List<T>.sort()排序,非常的實(shí)用,這里推薦給大家。2015-02-02WinForm實(shí)現(xiàn)最小化到系統(tǒng)托盤方法實(shí)例詳解
這篇文章主要介紹了WinForm實(shí)現(xiàn)最小化到系統(tǒng)托盤方法,實(shí)例分析了C#中實(shí)現(xiàn)WinForm最小化到系統(tǒng)托盤所需的相關(guān)控件與使用技巧,需要的朋友可以參考下2015-05-05CefSharp過(guò)濾圖片RequestHandler問(wèn)題
這篇文章主要介紹了CefSharp過(guò)濾圖片RequestHandler問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01關(guān)于C#中使用Oracle存儲(chǔ)過(guò)程返回結(jié)果集的問(wèn)題
Oracle中可以使用游標(biāo)(Cursor)對(duì)數(shù)據(jù)集進(jìn)行操作,但在存儲(chǔ)過(guò)程輸出參數(shù)中直接使用Cursor錯(cuò)誤,下面小編給大家?guī)?lái)了C#中使用Oracle存儲(chǔ)過(guò)程返回結(jié)果集的問(wèn)題,感興趣的朋友一起看看吧2021-10-10C#用記事本編寫(xiě)簡(jiǎn)單WinForm窗體程序
這篇文章主要為大家詳細(xì)介紹了C#用記事本編寫(xiě)簡(jiǎn)單WinForm窗體程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03C#之關(guān)于Base64簡(jiǎn)單加密與解密方式
這篇文章主要介紹了C#之關(guān)于Base64簡(jiǎn)單加密與解密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06