Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added helper method

...

Code Block
   public void GenerateDocument()
        {
            WordTemplate wt = new WordTemplate();
            wt.Open(@"..\..\WordTemplateFiles\NestedCatalogTemplate.docx");

            DataTable dt = new DataTable();
            dt = GetCatalogData(@"..\..\WordData\NestedProductCatalogData.csv");
            dt.Columns.Add("LargePhoto", typeof(byte[]));
           
            //Retrieves each picture in the NestedProductCatalogImages folder
            //and get a byte[] for each one using the GetCatalogPictures Helper method. 
            //Then add this byte[] to the LargePhoto column in dt.
            for (int k = 0; k < dt.Rows.Count; k++)
            {
                string imageName = dt.Rows[k].ItemArray[6].ToString();
                DataRow dr = dt.Rows[k];
                dr[7] = GetCatalogPictures(imageName);
            }
 
            wt.SetDataSource(DateTime.Now, "HeaderData");
            //Sets the data source for the index of the catalog with bookmark string "Index"
            wt.SetRepeatBlock(GetCatalogData(@"..\..\WordData\NestedProductCatalogIndexData.csv"), "Index");

            //Sets the data source for the body of the catalog with bookmark string "Catalog"
            wt.SetRepeatBlock(dt, "Catalog");

            wt.Process();
            //Save the product catalog in the desired location on the disc
            wt.Save(@"..\..\WordOutputFiles\NestedProductCatalog_output.docx");
             }
 


//Helper method to parse the csv files with index and catalog data into datatables
        private DataTable GetCatalogData(string csvFileName)
        {
            DataTable dt = new DataTable();
            using (GenericParserAdapter parser = new GenericParserAdapter(csvFileName))
            {
                parser.ColumnDelimiter = ',';
                parser.FirstRowHasHeader = true;

                dt = parser.GetDataTable();
            }
            return dt;
        }
        //Open desired file, picName, in Images\NestedProductCatalogImages and
        //return the bytearray that makes up the image.
        private byte[] GetCatalogPictures(string picName)
        {

            //Opens image and returns byte array
            byte [] imageArray = File.ReadAllBytes(@"..\..\WordImages\NestedProductCatalogImages\"+picName);
            return imageArray;

        }
   

Downloads

Panel

 Template: NestedCatalogTemplate.docx

Output: NestedProductCatalog_output.docx