This demo reads customer data from a database and creates standard-sized 5160 mailing labels. Put some label paper into your printer and see for yourself! |
\
public void GenerateDocument() { // Get the addresses from a datasource and return them as properly formatted addresses string[] AddressArray = GetCustomerAddressStrings(); // Create a DataTable with one column for each // label across DataTable AddressTable = new DataTable(); AddressTable.Columns.Add("Address1"); AddressTable.Columns.Add("Address2"); AddressTable.Columns.Add("Address3"); string[] Row = new string[AddressTable.Columns.Count]; int addridx = 0; // Set an infinite loop, break is called inside while (addridx < AddressArray.Length) { // Loop once for every Template column for (int i = 0; i < Row.Length; i++) { // If there's a value left in mAddressValues, use it // Otherwise, fill an empty string if (addridx < AddressArray.Length) Row[i] = AddressArray[addridx]; else Row[i] = String.Empty; addridx++; } // Add the data row after all columns are filled AddressTable.Rows.Add(Row); } // Create an instance of WordTemplate WordTemplate wt = new WordTemplate(); // Open the template document string templatePath = @"..\..\WordTemplateFiles\MailLabelTemplate.docx"; wt.Open(templatePath); // Set the LabelRow repeat block with the AddressTable table wt.SetRepeatBlock(AddressTable, "LabelRow"); // Process the template to populate the values wt.Process(); // Save the document wt.Save(@"..\..\WordOutputFiles\MailLabels_output.docx"); } |
Template: MailLabelTemplate.docx Output: MailLabels_output.docx |