TX Text Control Text processing tutorial (7) Mail Merge

Source: Internet
Author: User

The Mail Merge function is different from the previously mentioned function of saving all TX documents to the database. The Mail Merge function is to insert specific fields in the database to a specific location in the template document. At the same time, this section also demonstrates how to add the print function based on the Mail Merge function and create the template documents required for mail merge.
The source code in this section can be found in the installation directory of TX Text Control. NET:
Samples \ WinForms \ VB. NET \ MailMerge
Samples \ WinForms \ CSharp \ MailMerge
Step 1: Merge database data and text
Start the program, select the Load command under the Template menu, and Load the Template file through the Load command. The Template file contains some text fields, and the data of these text fields will be replaced by the corresponding data in the database.
650) this. width = 650; "alt =" "src =" http://gcdn.grapecity.com/attachment.aspx? Attachmentid = 1757 "/>
Select the Browse Database command under the Database menu to open the Address Database form, through which you can select data from the Database and merge it into the document.
650) this. width = 650; "alt =" "src =" http://gcdn.grapecity.com/attachment.aspx? Attachmentid = 1758 "/>
When you click the Merge button, the data in the database will be copied to the corresponding text domain in the document. You can also click the Next and Merge buttons to Merge different data into the document.
650) this. width = 650; "alt =" "src =" http://gcdn.grapecity.com/attachment.aspx? Attachmentid = 1759 "/>
The data source used in this example comes from an XML file, which contains some address information. Open the Address Database form and click the Previous and Next buttons each time. The data is obtained using the GetRecord () method:

 

 
 
  1. [C#]  
  2. private void GetRecord()  
  3. {  
  4.     DataRow Row = dsAddress.Tables[0].Rows[CurrentRow];  
  5.     lblCompany.Text = Row["company"].ToString();  
  6.     lblRecipient.Text = Row["recipient"].ToString();  
  7.     lblStreet.Text = Row["street"].ToString();  
  8.     lblCity.Text = Row["city"].ToString();  
  9.     lblCountry.Text = Row["country"].ToString();  
  10.     lblSalutation.Text = Row["salutation"].ToString();  
  11.     SetButtonState();  

When you click Merge, the data in the data source is copied to the corresponding text domain of the document. The text domain has the same name as the field in the data source, therefore, you can use the For Each operation to complete the copy operation:

 

 
 
  1. [C#]  
  2. private void cmdMerge_Click(object sender, System.EventArgs e)  
  3. {  
  4.     foreach (TXTextControl.TextField Field in tx.TextFields)  
  5.     {  
  6.         Field.Text = dsAddress.Tables[0].Rows[CurrentRow][Field.Name].ToString();  
  7.     }  

Step 2: Print
In the Address Database form, add a Print button. When you click Print, the program merges the records in the data source into the document and prints them:

 

 
 
  1. [C#]  
  2. private void cmdPrint_Click(object sender, System.EventArgs e)  
  3. {  
  4.     PrintDocument PrintDoc = new PrintDocument();  
  5.     foreach (DataRow CurrentRow in dsAddress.Tables[0].Rows)  
  6.     {  
  7.         // Merge data from current record  
  8.         foreach (TXTextControl.TextField Field in tx.TextFields)  
  9.             Field.Text = CurrentRow[Field.Name].ToString();  
  10.         // Print  
  11.         PrintDoc.PrinterSettings.FromPage = 0;  
  12.         PrintDoc.PrinterSettings.ToPage = tx.Pages;  
  13.         tx.Print(PrintDoc);  
  14.     }  

Because the print operation will automatically Merge the data, you do not need the Merge button in step 1, and use Grid to display the data in the data source, so that you can better browse the data in the data source.
650) this. width = 650; "alt =" "src =" http://gcdn.grapecity.com/attachment.aspx? Attachmentid = 1760 "/>
Step 3: Create a template document
Load, save, and import files
In step 1 and step 2, the program will automatically load the template. tx template file, so you cannot load other template documents or save documents. Therefore, you need to modify the file-related code, select the template document to be loaded through a file dialog box, and add the Saveing and Importing menu items at the same time, note that the Text field in the document can be retained only when the document is saved as the TX Text Control format. You can use the SaveFile dialog box to save files in other formats:

 

 
 
  1. [C#]  
  2. private void mnuFile_SaveTemplateAs_Click(object sender, System.EventArgs e)  
  3. {  
  4.     dlgSaveFile.Filter = "Text Control Files (*.tx)|*.tx";  
  5.     dlgSaveFile.ShowDialog();  
  6.     if (dlgSaveFile.FileName != "")  
  7.         textControl1.Save(dlgSaveFile.FileName,  
  8.         TXTextControl.StreamType.InternalFormat);  

Add database fields
Steps 1 and 2 contain necessary text fields. In order to create more flexible files, the application should provide the function for users to select database fields, you can decide which fields to add to the template document. Add an Insert menu to the program. The menu includes all columns in the Data source:
650) this. width = 650; "alt =" "src =" http://gcdn.grapecity.com/attachment.aspx? Attachmentid = 1761 "/>
However, each data source may contain different column information. The Insert Menu also needs to be dynamically created. Each column in the data source corresponds to a menu item in the Insert menu. When you click these menu items, the corresponding text field will be entered in the document:

 

 
 
  1. [C#]  
  2. private void CreateTextFieldMenu()  
  3. {  
  4.     mnuInsert.MenuItems.Clear();  
  5.     foreach (DataColumn DataField in dsAddress.Tables[0].Columns)  
  6.         mnuInsert.MenuItems.Add(DataField.ColumnName,  
  7.             new EventHandler(InsertMenuItems_Click));  
  8.  
  9. private void InsertMenuItems_Click(object sender, System.EventArgs e)  
  10. {  
  11.     TXTextControl.TextField textField = new TXTextControl.TextField();  
  12.     textField.Text = "(" + ((MenuItem)sender).Text + ")";  
  13.     textField.Name = ((MenuItem)sender).Text;  
  14.     textField.ShowActivated = true;  
  15.     textField.DoubledInputPosition = true;  
  16.     textControl1.TextFields.Add(textField);  

 

Download the trial version of TX Text Control

This article from the "grape city control blog" blog, please be sure to keep this source http://powertoolsteam.blog.51cto.com/2369428/773933

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.