Message-ID: <157141068.9977.1711708317897.JavaMail.web05$@web05> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_9976_1279913181.1711708317897" ------=_Part_9976_1279913181.1711708317897 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
The template document has a table defined in it that exactly matc=
hes the dimensions of Avery 5160-sized mailing labels. You can take t=
his template document and resize the table to match any size labels. <=
/p>
public void GenerateDocument() { // Get t= he addresses from a datasource and return them as properly formatted addres= ses string[]= AddressArray =3D GetCustomerAddressStrings(); // Creat= e a DataTable with one column for each //= label across DataTabl= e AddressTable =3D new DataTable(); AddressT= able.Columns.Add("Address1"); AddressT= able.Columns.Add("Address2"); AddressT= able.Columns.Add("Address3"); string[]= Row =3D new string[AddressTable.Columns.Count]; int addr= idx =3D 0; // Set a= n infinite loop, break is called inside while (a= ddridx < AddressArray.Length) { &nb= sp; // Loop once for every Template column &nb= sp; for (int i =3D 0; i < Row.Length; i++) &nb= sp; { &nb= sp; // If there's a value left in mAddr= essValues, use it &nb= sp; // Otherwise, fill an empty s= tring &nb= sp; &nb= sp; if (addridx < AddressArray.Lengt= h) &nb= sp; Row[i] =3D = AddressArray[addridx]; &n= bsp; else &nb= sp; Row[i] =3D = String.Empty; &nb= sp; addridx++; &nb= sp; } &nb= sp; // Add the data row after all columns are filled &nb= sp; AddressTable.Rows.Add(Row); } // Creat= e an instance of WordTemplate WordTemp= late wt =3D new WordTemplate(); // Open = the template document string t= emplatePath =3D @"..\..\WordTemplateFiles\MailLabelTemplate.docx"= ; wt.Open(= templatePath); // Set t= he LabelRow repeat block with the AddressTable table wt.SetRe= peatBlock(AddressTable, "LabelRow"); // Proce= ss the template to populate the values wt.Proce= ss(); // Save = the document wt.Save(= @"..\..\WordOutputFiles\MailLabels_output.docx"); } //Create a datatable with all then customer information and return = it private DataTable GetCustomersData(){ DataTable dt =3D new DataTable(); dt.Columns.Add("ContactName"); dt.Columns.Add("Address"); dt.Columns.Add("City"); dt.Columns.Add("Region"); dt.Columns.Add("PostalCode"); dt.Columns.Add("Country"); dt.Rows.Add(new Object[] { "Incomparable Bicycle Store&quo= t;, "99 Edgewater Drive", "Norwood", "MA", "2062", "United States"= }); dt.Rows.Add(new Object[] { "Wholesale Bikes", "5= 8 Teed Drive", "Randolph", "MA", "2368", "United States"= }); dt.Rows.Add(new Object[] { "Bikes Anyone?", "Ame= s Plaza", "Saugus", "MA", "1906", "United States"= }); dt.Rows.Add(new Object[] { "Purchase Mart", "Wre= ntham Village", "Wrentham", "MA", "2093", "United States"= }); return dt; } // Get the address items from the DataTable, format // them, and return them in a string array private string[] GetCustomerAddressStrings() { // Get the DataTables with the customer information DataTable dt =3D GetCustomersData(); /* this is the format string for an address */ string FormatTemplate =3D "{0}\n" + // Nam= e "{1}\n" + // Street "{2}, {3} {4}\n" + // City, State Zip "{5}"; // Country ArrayList al =3D new ArrayList(); // Copy the DataTable row values into strings // and put them in an ArrayList foreach (DataRow dr in dt.Rows) { string address =3D String.Format(FormatTemplate, dr["ContactName"], dr["Address"], dr["City"], dr["Region"], dr["= PostalCode"], dr["Country"]); al.Add(address); } // Return the values as a string array (string[]) return (string[])al.ToArray(typeof(string)); }=20
Template: MailLabelTemplate.docx
Output: MailLabels_output.docx
=20