Message-ID: <634759784.8541.1711649421176.JavaMail.web05$@web05> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_8540_1587491616.1711649421176" ------=_Part_8540_1587491616.1711649421176 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Insert Image Demo

Insert Image Demo

Intro

WordTemplate can insert = images from byte arrays or ADO.NET objects into your Word documents.


The AdventureWorks SQL query includes the ProductPhoto.T= humbNailPhoto field, which is a binary image stored in the databa= se. By using special merge field syntax (click "View template" to= see the template document and merge fields), you can import the image from= the DataColumn into the document at runtime.

Code

=20
   public void GenerateDocument()
        {
            //Retrie=
ve the datatable with a a helper method
            DataTabl=
e dt =3D GetData();
            // A lis=
t of the mergefield names in the template file
            string[]=
 fieldNames =3D { "ProductNumber", "ProductName", "=
;ProductPrice", "ProductImage" };
 
            // Name =
of the template bookmark that marks the repeatblock
            string r=
epeatBlockName =3D "ProductRow";
 
            WordTemp=
late wt =3D new WordTemplate();
 
            // Open =
the template document
            wt.Open(=
@"..\..\WordTemplateFiles\InsertImageTemplate.docx");
 
            // Set t=
he repeat block by providing the data source,
             //=
 a list of mergefield names in the proper order, and
             //=
 the name of the repeat block bookmark
            
            wt.SetRe=
peatBlock(dt, repeatBlockName);
 
            // Call =
process() after setting the data source and/or repeat blocks
            wt.Proce=
ss();
 
            //Save t=
he output to the desired location
            wt.Save(=
@"..\..\WordOutputFiles\InsertImage_output.docx");
 
            return;
 
        }

        //Gets all data for product catalog including p=
ictures.
        private DataTable GetData()
        {
            DataTable dt =3D GetProductData(@=
"..\..\WordData\InsertImageData.csv");
            dt.Columns.Add("ProductImage=
", typeof(byte[]));
            for (int k =3D 0; k < dt.Rows.=
Count; k++)
            {

                string imageName =
=3D dt.Rows[k].ItemArray[3].ToString();
                dt.Rows[k][4] =3D GetPictures(imageName);
            }
            return dt;
        }



        // Get a DataTable of product data excluding ph=
otos
        private DataTable GetProductData(string csvFile=
Name)
        {


            DataTable dt =3D new DataTable();
            using (GenericParserAdapter parse=
r =3D new GenericParserAdapter(csvFileName))
            {
                parser.ColumnDelimi=
ter =3D ',';
                parser.FirstRowHasH=
eader =3D true;

                dt =3D parser.GetDa=
taTable();
            }
            return dt;
        }
       
        private byte[] GetPictures(string picName)
        {

            //Opens image and returns byte ar=
ray
            byte[] imageArray =3D File.ReadAl=
lBytes(@"..\..\WordImages\InsertImageDemoImages\" + picName);

            //ReadAllBytes() is able to find =
the files.  
            return imageArray;


        }

=20

 Downloads

------=_Part_8540_1587491616.1711649421176--