Winform利用分頁(yè)控件實(shí)現(xiàn)導(dǎo)出PDF文檔功能
當(dāng)前的Winform分頁(yè)控件中,當(dāng)前導(dǎo)出的數(shù)據(jù)一般使用Excel來(lái)處理,Excel的文檔可以用于后期的數(shù)據(jù)展示或者批量導(dǎo)入做準(zhǔn)備,因此是比較好的輸入輸出格式。但是有框架的使用客戶希望分頁(yè)控件能夠直接導(dǎo)出PDF,雖然Excel也可以直接轉(zhuǎn)換為PDF,不過(guò)直接導(dǎo)出PDF的處理肯定更加方便直觀。因此整理了一下分頁(yè)控件導(dǎo)出PDF的處理過(guò)程,分享一下。
1、PDF的導(dǎo)出插件
使用PDF導(dǎo)出的插件有很多,如Aspose.PDF、Spire.PDF、PdfSharp、iTextSharp等等很多,有些是收費(fèi)的,有些是開(kāi)源免費(fèi)的,我們這里使用iTextSharp開(kāi)源組件進(jìn)行PDF的導(dǎo)出處理操作。
在測(cè)試的時(shí)候,我們可以對(duì)一個(gè)表格內(nèi)容進(jìn)行數(shù)據(jù)的導(dǎo)出,以便驗(yàn)證效果,然后在考慮整合到分頁(yè)控件的內(nèi)部中使用,如下測(cè)試界面所示。
大概的效果如下所示。
如果需要,我們可以進(jìn)一步定義頁(yè)眉和頁(yè)腳,增加一些特殊的信息等等。
我們可以測(cè)試導(dǎo)出列表中的DataTable數(shù)據(jù)源,如下所示。
private void btnExportPdf_Click(object sender, EventArgs e) { //帶參數(shù)處理 bool isLandscape = true;//是否為橫向打印,默認(rèn)為true bool includeHeader = true;//是否每頁(yè)包含表頭信息 int headerAlignment = Element.ALIGN_CENTER;//頭部的對(duì)其方式,默認(rèn)為居中對(duì)齊 float headerFontSize = 9f;//頭部字體大小 float rowFontSize = 9f;//行記錄字體大小 float? headerFixHeight = null;//頭部的固定高度,否則為自適應(yīng) <strong>TextSharpHelper</strong><strong>.ExportPdf</strong>(isLandscape, includeHeader, headerAlignment, headerFontSize, rowFontSize, headerFixHeight); }
2、導(dǎo)出PDF的邏輯處理
上面的操作,對(duì)輔助類TextSharpHelper.ExportPdf 的操作進(jìn)行封裝了,我們可以看到,方法留出了幾個(gè)特殊的配置信息,可供我們進(jìn)行調(diào)整格式。
一般使用列表的輸出為橫向較為美觀,字體比正常窗體顯示的字體小一點(diǎn),并可以設(shè)置是否每頁(yè)P(yáng)DF包含表頭信息等等。
輔助類的全部代碼如下所示:
/// <summary> /// 基于iTextSharp.text.pdf對(duì)PDF的導(dǎo)出處理 /// </summary> public static class TextSharpHelper { /// <summary> /// datatable轉(zhuǎn)PDF方法 /// </summary> /// <param name="data">dataTable數(shù)據(jù)</param> /// <param name="pdfFile">PDF文件保存的路徑</param> /// <param name="isLandscape">是否為橫向打印,默認(rèn)為true</param> /// <param name="includeHeader">是否每頁(yè)包含表頭信息</param> /// <param name="headerAlignment">頭部的對(duì)其方式,默認(rèn)為居中對(duì)齊</param> /// <param name="headerFontSize">頭部字體大小</param> /// <param name="rowFontSize">行記錄字體大小</param> /// <param name="headerFixHeight">頭部的固定高度,否則為自適應(yīng)</param> /// <returns></returns> public static bool ExportTableToPdf(DataTable data, string pdfFile, bool isLandscape = true, bool includeHeader = true, int headerAlignment = Element.ALIGN_CENTER, float headerFontSize = 9f, float rowFontSize = 9f, float? headerFixHeight = null) { //默認(rèn)頁(yè)面大小 var document = new Document(); var retangle = new iTextSharp.text.Rectangle(0, 0, isLandscape ? PageSize.A4.Height : PageSize.A4.Width, isLandscape ? PageSize.A4.Width : PageSize.A4.Height); document.SetPageSize(retangle); var writer = PdfWriter.GetInstance(document, new FileStream(pdfFile, FileMode.Create)); document.Open(); //設(shè)置字體 var bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); var fontHeader = new iTextSharp.text.Font(bfChinese, headerFontSize, iTextSharp.text.Font.BOLD, new BaseColor(0, 0, 0)); var fontRow = new iTextSharp.text.Font(bfChinese, rowFontSize, iTextSharp.text.Font.NORMAL, new BaseColor(0, 0, 0)); var table = new PdfPTable(data.Columns.Count); table.WidthPercentage = 100f; // percentage table.DefaultCell.Padding = 1; table.DefaultCell.BorderWidth = 1; table.DefaultCell.BorderWidth = 0.1f; table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; table.HeaderRows = includeHeader ? 1 : 0; //將datatable表頭轉(zhuǎn)換成PDFTable的表頭 var EventRowBackColor = Color.LightCyan; foreach (DataColumn dc in data.Columns) { var caption = !string.IsNullOrEmpty(dc.Caption) ? dc.Caption : dc.ColumnName; var cell = new PdfPCell(); if (headerFixHeight.HasValue) { cell.FixedHeight = headerFixHeight.Value; } cell.HorizontalAlignment = headerAlignment; cell.VerticalAlignment = Element.ALIGN_MIDDLE; cell.Phrase = new Phrase(caption, fontHeader); table.AddCell(cell); } //插入數(shù)據(jù) for (int i = 0; i < data.Rows.Count; i++) { var backgroudColor = new BaseColor((i % 2 == 0) ? Color.White : EventRowBackColor); for (int j = 0; j < data.Columns.Count; j++) { var cell = new PdfPCell(); var text = data.Rows[i][j].ToString(); cell.BackgroundColor = backgroudColor; cell.Phrase = new Phrase(text, fontRow); table.AddCell(cell); } } document.Add(table); document.Close(); writer.Close(); return true; } }
上面根據(jù)方法的參數(shù)對(duì)字體,頁(yè)面寬度高度、表格間隔顏色,表頭等信息進(jìn)行設(shè)置處理,然后使用插件進(jìn)行輸出PDF的內(nèi)容即可,PDF內(nèi)容的輸出,有點(diǎn)類似DataTable的表格控制,單元格的信息可以獨(dú)立控制。
為了不用重復(fù)的引用代碼或者輔助類,我們可以整合到分頁(yè)控件的列表中,在其中封裝處理邏輯即可,這樣所有列表都具有通用的導(dǎo)出PDF操作了,如下界面所示。
我們一般導(dǎo)出是通過(guò)事件進(jìn)行處理的,因此,我們?cè)诘撞康姆猪?yè)中定義一個(gè)觸發(fā)的事件,如下所示。
public delegate void <strong>ExportPdfEventHandler</strong>(object sender, EventArgs e); /// <summary> /// 分頁(yè)工具條用戶控件,僅提供分頁(yè)信息顯示及改變頁(yè)碼操作 /// </summary> public class Pager : DevExpress.XtraEditors.XtraUserControl { /// <summary> /// 導(dǎo)出PDF的事件 /// </summary> public event <strong>ExportPdfEventHandler</strong> ExportPdf;
按鈕單擊的時(shí)候,觸發(fā)事件的處理,如下代碼所示。
private void btnExportPdf_Click(object sender, EventArgs e) { if (ExportPdf != null) { ExportPdf(sender, e); } }
這樣事件會(huì)傳遞到外面的容器組件中,對(duì)容器組件中的數(shù)據(jù)源進(jìn)行導(dǎo)出處理即可。
private void WinGridViewPager_Load(object sender, EventArgs e) { if (!this.DesignMode) { this.pager.PageChanged += new PageChangedEventHandler(pager_PageChanged); this.pager.ExportCurrent += new ExportCurrentEventHandler(pager_ExportCurrent); this.pager.ExportAll += new ExportAllEventHandler(pager_ExportAll); <strong>this.pager.ExportPdf += Pager_ExportPdf;</strong>
對(duì)于列表的數(shù)據(jù)源,我們可以統(tǒng)一轉(zhuǎn)換為DataTable格式,如下 分析數(shù)據(jù)源的格式進(jìn)行轉(zhuǎn)換。
DataTable sourcetable = null; if (this.DataSource is DataView) { DataView dv = (DataView)AllToExport;//默認(rèn)導(dǎo)出顯示內(nèi)容 sourcetable = dv.ToTable(); } else if (this.DataSource is DataTable) { sourcetable = this.DataSource as DataTable; } else { sourcetable = ReflectionUtil.CreateTable(this.DataSource); }
而對(duì)于表格內(nèi)容的中文注釋,我們可以讀取表格里面的頭部信息作為PDF表頭的中文信息,如下所示。
var table = new DataTable(); for (int i = 0; i < this.gridView1.Columns.Count; i++) { if (this.gridView1.Columns[i].Visible) { var column = new DataColumn(this.gridView1.Columns[i].FieldName, typeof(string)); column.Caption = this.gridView1.Columns[i].Caption; table.Columns.Add(column); } }
而每行的內(nèi)容,可以逐一讀取并寫(xiě)入其中即可。
for (int i = 0; i < sourcetable.Rows.Count; i++) { var row = table.NewRow(); for (int j = 0; j < table.Columns.Count; j++) { var columnName = table.Columns[j].ColumnName; var displayText = this.gridView1.GetRowCellDisplayText(i, columnName); row[columnName] = displayText ?? ""; } table.Rows.Add(row); }
最后調(diào)用輔助類進(jìn)行導(dǎo)出PDF文檔即可,導(dǎo)出后進(jìn)行打開(kāi)處理。
var success =<strong> TextSharpHelper.ExportTableToPdf</strong>(table, pdfFile, isLandscape, includeHeader, headerAlignment, headerFontSize, rowFontSize, headerFixHeight); if (success) { Process.Start(pdfFile); }
導(dǎo)出PDF和前面的效果一樣了。
以上就是Winform利用分頁(yè)控件實(shí)現(xiàn)導(dǎo)出PDF文檔功能的詳細(xì)內(nèi)容,更多關(guān)于Winform分頁(yè)控件實(shí)現(xiàn)導(dǎo)出PDF的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
無(wú)法從 int? 轉(zhuǎn)換為 int 運(yùn)行時(shí)出現(xiàn)錯(cuò)誤
無(wú)法從"int?"轉(zhuǎn)換為"int" ,在運(yùn)行時(shí)會(huì)出現(xiàn)錯(cuò)誤,通過(guò)強(qiáng)制類型轉(zhuǎn)換(int)便可解決2014-05-05C# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)
下面小編就為大家?guī)?lái)一篇C# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05Unity實(shí)現(xiàn)游戲卡牌滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)游戲卡牌滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02C#控制Excel Sheet使其自適應(yīng)頁(yè)寬與列寬的方法
這篇文章主要介紹了C#控制Excel Sheet使其自適應(yīng)頁(yè)寬與列寬的方法,涉及C#操作Excel的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06C#中利用斷點(diǎn)操作調(diào)試程序的步驟詳解
所謂斷點(diǎn)調(diào)試就是檢測(cè)執(zhí)行路徑和數(shù)據(jù)是否正確,中斷游戲運(yùn)行在線調(diào)試,下面這篇文章主要給大家介紹了關(guān)于C#中利用斷點(diǎn)操作調(diào)試程序的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12winform中寫(xiě)app.config文件時(shí)調(diào)試情況下沒(méi)有改變的原因
讀取很簡(jiǎn)單基本都用過(guò) ConfigurationManager.AppSettings[""].ToString() 寫(xiě)config不是很常用2013-02-02