Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
    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");
        }

 Downloads

Panel

 Template: MailLabelTemplate.docx

Output: MailLabels_output.docx