翻譯|使用教程|編輯:況魚杰|2019-08-05 15:50:38.567|閱讀 409 次
概述:本教程將會介紹如何使用ASP.NET Web應用程序中的MailMerge組件將模板與數據合并以創建Adobe PDF文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
TX Text Control Server for ASP.NET (incl. WPF)是一個企業級的服務器端文字處理控件。它為用于ASP.NET服務器環境提供一個完全可編程的文字處理引擎,并且包含一個WPF客戶端版本。
點擊下載TX Text Control Server for ASP.NET (incl. WPF)最新試用版
創建第一個報表應用程序
打開Visual Studio并創建一個新的ASP.NET空Web應用程序。

在Solution Explorer中,選擇項目,然后從項目主菜單中選擇Add New Item .... ,在打開的對話框的添加新項目中,選擇Web窗體并使用添加確認。 

在Solution Explorer中,選擇新創建的Web窗體,然后從View主菜單中選擇Component Designer。在工具箱中找到MailMerge組件,然后將實例拖放到組件設計器表單上。然后使用ServerTextControl組件重復此步驟。

選擇MailMerge組件并將TextComponent屬性設置為serverTextControl1 - 插入的ServerTextControl實例的名稱。

在解決方案資源管理器中選擇項目時,從項目主菜單中選擇Add New Item ....,在打開的對話框的添加現有項中,瀏覽到以下示例文件夾:
%USERPROFILE%\Documents\TX Text Control 27.0.NET Server for ASP.NET\Samples\ASP.NET\CSharp(或者VB.NET)\Documentation Tutorials\MailMerge\Tutorial
選擇以下2個文件:template.docx和sample_db.xml,然后單擊添加按鈕。

在Solution Explorer中選擇Web Form,然后從View主菜單中選擇Designer,在工具箱中找到Button控件,然后將實例拖放到Web窗體上。

雙擊該按鈕并將以下代碼插入事件處理程序:
CS:
protected void Button1_Click(object sender, EventArgs e)    
{    
// create a DataSet from the sample XML data source    
System.Data.DataSet ds = new System.Data.DataSet();    
ds.ReadXml(Server.MapPath("sample_db.xml"), System.Data.XmlReadMode.Auto);    
// load the template    
mailMerge1.LoadTemplate(Server.MapPath("template.docx"),    
TXTextControl.DocumentServer.FileFormat.WordprocessingML);    
// merge the template with data    
mailMerge1.Merge(ds.Tables[0]);    
// save the document as PDF into a byte array    
byte[] data;    
mailMerge1.SaveDocumentToMemory(out data,    
TXTextControl.BinaryStreamType.AdobePDF, null);    
// return the document to the browser for download    
Response.Clear();    
Response.AddHeader("content-disposition",    
String.Format("attachment;filename={0}", "created_by_txtextcontrol.pdf"));    
Response.ContentType = "application/pdf";    
Response.BinaryWrite(data);    
Response.End();    
}    
Sign up for freeVB:
Protected Sub Button1_Click(sender As Object, e As EventArgs)    
' create a DataSet from the sample XML data source    
Dim ds As New System.Data.DataSet()    
ds.ReadXml(Server.MapPath("sample_db.xml"), System.Data.XmlReadMode.Auto)    
' load the template    
mailMerge1.LoadTemplate(Server.MapPath("template.docx"),    
TXTextControl.DocumentServer.FileFormat.WordprocessingML)    
' merge the template with data    
mailMerge1.Merge(ds.Tables(0))    
' save the document as PDF into a byte array    
Dim data As Byte()    
mailMerge1.SaveDocumentToMemory(data,    
TXTextControl.BinaryStreamType.AdobePDF, Nothing)    
' return the document to the browser for download    
Response.Clear()    
Response.AddHeader("content-disposition",    
[String].Format("attachment;filename={0}", "created_by_txtextcontrol.pdf"))    
Response.ContentType = "application/pdf"    
Response.BinaryWrite(data)    
Response.[End]()    
End Sub在同一代碼視圖中,將以下代碼添加到Page_Load事件:
CS:
protected void Page_Load(object sender, EventArgs e)    
{    
InitializeComponent();    
}VB:
Protected Sub Page_Load(sender As Object, e As EventArgs) InitializeComponent() End Sub
編譯并啟動應用程序。按按鈕創建以PDF文件形式返回的報告。
 ASP.NET部分使用教程的第一步就完成了,接下來將會介紹AJAX應用程序的創建。
 
   
關注慧聚IT微信公眾號 ???,了解產品的最新動態及最新資訊。

本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自: