C# 利用PdfSharp生成Pdf文件的示例
PdfSharp一款開源的用于創(chuàng)建,操作PDF文檔的.Net類庫,本文以一個簡單的小例子,簡述如何通過PdfSharp進行創(chuàng)建PDF文檔,僅供學習分享使用,如有不足之處,還請指正。
PdfSharp下載
在本例中,主要通過NuGet包管理器進行下載安裝,目前PdfSharp版本為v1.5.0.5147,如下所示:
涉及知識點
在生成PDF文檔過程中,主要知識點如下:
- PdfDocument : 表示一個PDF文檔對象,調用save方法保存文檔到指定路徑。
- PdfPage : 表示PDF文檔中的一頁。
- XGraphics:表示頁面上的繪制對象,所有的頁面內容,都可通過此對象繪制。如:DrawString繪制文本內容,DrawLine繪制直線等。
- XFont:繪制文本的字體,字體名稱只能取C:\Windows\Fonts目錄下的ttf字體文件,不能是ttc格式的字體。
- XTextFormatter:表示一個簡單的文本字體格式,如識別文本的換行符而實現(xiàn)自動換行等內容。
文檔示例圖
在本例中,主要是將頁面內容寫入PDF文件中,頁面如下所示:
生成的PDF文件,如下所示:
核心代碼
在本例中,核心代碼主要包括如下幾個部分:
- 創(chuà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. 定義文檔對象 PdfDocument document = new PdfDocument(); //2. 新增一頁 PdfPage page = document.AddPage(); // 設置紙張大小 page.Size = PageSize.A4; //3. 創(chuàng)建一個繪圖對象 XGraphics gfx = XGraphics.FromPdfPage(page); XFont font = new XFont("華文宋體", 40, XFontStyle.Bold); //定制化內容開始 int cur_x = 0 + margin_left_right; int cur_y = 0 + margin_top_bottom; //標題1 gfx.DrawString(bo.Head1, font, XBrushes.Red, new XRect(cur_x, cur_y, page.Width-2*cur_x, 80), XStringFormats.Center); //序號 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); //密級 cur_x = cur_x + 200; gfx.DrawString(string.Format("密級[{0}]",bo.Private), font, XBrushes.Black, new XRect(cur_x, cur_y, 100, 20), XStringFormats.CenterLeft); //緩級 cur_x = cur_x + 100; gfx.DrawString(string.Format("緩級[{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); //標題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); //正文 ,自動換行 cur_y = cur_y + 40; XTextFormatter tf = new XTextFormatter(gfx); font = new XFont("華文宋體", 12, XFontStyle.Regular); //測量當前內容下,一行可以多少個漢字 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; //表示一行可以放多少個漢字。 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; //計算矩形 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); //水印開始 font = new XFont("華文宋體", 20, XFontStyle.BoldItalic); // 計算長度 var size = gfx.MeasureString(bo.Watermark, font); // 定義旋轉中心 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; //畫刷 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); } //水印結束 //6. 保存文檔 document.Save(filePath); return true; }
以上就是C# 利用PdfSharp生成Pdf文件的示例的詳細內容,更多關于c# 生成Pdf文件的資料請關注腳本之家其它相關文章!
相關文章
C#基礎教程之IComparable用法,實現(xiàn)List<T>.sort()排序
這篇文章主要介紹了C#的一些基礎知識,主要是IComparable用法,實現(xiàn)List<T>.sort()排序,非常的實用,這里推薦給大家。2015-02-02WinForm實現(xiàn)最小化到系統(tǒng)托盤方法實例詳解
這篇文章主要介紹了WinForm實現(xiàn)最小化到系統(tǒng)托盤方法,實例分析了C#中實現(xiàn)WinForm最小化到系統(tǒng)托盤所需的相關控件與使用技巧,需要的朋友可以參考下2015-05-05