Welcome to OpenXML Developer Sign in | Join | Help

Inserting Images Streamed from a database into an Open Word Document

Last post 10-07-2008, 4:42 AM by beast. 2 replies.
Sort Posts: Previous Next
  •  08-22-2008, 3:03 PM 3600

    Inserting Images Streamed from a database into an Open Word Document

    I have an open word document that has an add-in that allows for searching other documents in the database (i.e. - building a composite word document).

    The user can then insert a word document returned from the search into the open document.

    This uses a content control and all works except for my images in the document returned from the search.

    For each image in the inserted document, I perform an additional database query to return a stream of the image.

    However, I do not know how I get this image into my open document and also in the media folder with the corresponding relationship.

    Any information or pointers would be greatly appreciated.

     

     

  •  08-27-2008, 4:57 AM 3614 in reply to 3600

    Re: Inserting Images Streamed from a database into an Open Word Document

    Hi

    This is a bit long winded; but should help. I have left out preambling code.
    The first method in the modWordClass was borrowed from somewhere that now escapes me, I am not the original author of it.

    MarkC


       /*
        * resize both photos
       */
       modWordClass.resizeJpgImage(childImageIn, childImageOut, 459, 610);
       modWordClass.resizeJpgImage(familyImageIn, familyImageOut, 459, 610);

        /*
        * create a working document from our template document
       */
       File.Copy(inDocx, outDocx, true);
       /*
       * open the working document for editing
       */
      modWordDoc.modWordClass.replacePageOneImages(outDocx, childImageOut, familyImageOut, backgroundImageOut);

    -------------------------------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using DocumentFormat.OpenXml.Packaging;
    using System.IO;
    using System.Xml;
    using System.Linq.Expressions;

    namespace modWordDoc
    {
        public class modWordClass
        {
            public static System.Drawing.Image FixedSize(System.Drawing.Image imgPhoto, int Width, int Height)
            {
                int sourceWidth = imgPhoto.Width;
                int sourceHeight = imgPhoto.Height;
                int sourceX = 0;
                int sourceY = 0;
                int destX = 0;
                int destY = 0;

                float nPercent = 0;
                float nPercentW = 0;
                float nPercentH = 0;

                nPercentW = ((float)Width / (float)sourceWidth);
                nPercentH = ((float)Height / (float)sourceHeight);
                if (nPercentH < nPercentW)
                {
                    nPercent = nPercentH;
                    destX = System.Convert.ToInt16((Width -
                                  (sourceWidth * nPercent)) / 2);
                }
                else
                {
                    nPercent = nPercentW;
                    destY = System.Convert.ToInt16((Height -
                                  (sourceHeight * nPercent)) / 2);
                }

                int destWidth = (int)(sourceWidth * nPercent);
                int destHeight = (int)(sourceHeight * nPercent);

                Bitmap bmPhoto = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                                 imgPhoto.VerticalResolution);

                Graphics grPhoto = Graphics.FromImage(bmPhoto);
                grPhoto.Clear(System.Drawing.Color.Red);
                grPhoto.InterpolationMode =
                        InterpolationMode.HighQualityBicubic;

                grPhoto.DrawImage(imgPhoto,
                    new System.Drawing.Rectangle(destX, destY, destWidth, destHeight),
                    new System.Drawing.Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                    GraphicsUnit.Pixel);

                grPhoto.Dispose();
                return bmPhoto;
            }

            public static void replaceAndFlushImage(string ImageOut, ImagePart replaceImage)
            {
                FileStream loFS;
                Stream imageStream = replaceImage.GetStream(FileMode.Open, FileAccess.ReadWrite);
                using (imageStream)
                {
                    loFS = File.OpenRead(ImageOut);
                    using (loFS)
                    {
                        long lnFilesize = loFS.Length;
                        byte[] lbBytes = new byte[lnFilesize];
                        loFS.Read(lbBytes, 0, (int)lnFilesize);
                        imageStream.Write(lbBytes, 0, (int)lnFilesize);
                        imageStream.Flush();
                    }
                }
            }

            public static void resizeJpgImage(string ImageIn, string ImageOut, int width, int height)
            {
                System.Drawing.Image imgPhoto;
                System.Drawing.Image imageIn = System.Drawing.Image.FromFile(ImageIn);
                using (imageIn)
                {
                    imgPhoto = modWordDoc.modWordClass.FixedSize(imageIn, 459, 610);
                    using (imgPhoto)
                    {
                        imgPhoto.Save(ImageOut, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }
            }

            public static void replacePageOneImages(string outDocx, string childImageOut, string familyImageOut, string backgroundImageOut)
            {
                WordprocessingDocument doc = WordprocessingDocument.Open(outDocx, true);
                using (doc)
                {
                    /*
                    * replace the background image of the document
                    */
                    var theHeads = doc.MainDocumentPart.HeaderParts;
                    foreach (HeaderPart headerPart in theHeads)
                    {
                        foreach (ImagePart headImage in headerPart.ImageParts)
                        {
                            string imageUri = headImage.Uri.ToString();
                            if (imageUri == @"/word/media/image3.jpeg")
                            {
                                replaceAndFlushImage(backgroundImageOut, headImage);
                            }
                        }
                    }
                    /*
                    * replace the child and family images in the document
                    */
                    foreach (ImagePart oneImage in doc.MainDocumentPart.ImageParts)
                    {
                        string ridPath = oneImage.Uri.ToString();
                        switch (ridPath)
                        {
                            case @"/word/media/image1.jpeg":
                                replaceAndFlushImage(childImageOut, oneImage);
                                break;

                            case @"/word/media/image2.jpeg":
                                replaceAndFlushImage(familyImageOut, oneImage);
                                break;
                        }
                    }
                }
            }
           
        }
    }

  •  10-07-2008, 4:42 AM 3761 in reply to 3614

    Re: Inserting Images Streamed from a database into an Open Word Document

    Hi

     

    You can try using third-party tool for inserting images into the word documents. For example see the following link

    http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/inserting-an-image.html

     

    WBR,

    Alex

View as RSS news feed in XML