This project is read-only.

Insert picture in Word document

OpenXML Word

Following are the namespaces we need to add:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;

In below code main function is InsertAPicture then a sub function AddImageToBody is use to insert image in body part of Word document.

 class Program
        static void Main(string[] args)
            InsertAPicture("Sample.doc", "download.jpg");
        public static void InsertAPicture(string document, string fileName)
            using (WordprocessingDocument wordprocessingDocument =
                WordprocessingDocument.Open(document, true))
                MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;

                ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);

                using (FileStream stream = new FileStream(fileName, FileMode.Open))

                AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));

        private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
            // Define the reference of the image.
            var element =
                 new Drawing(
                     new DW.Inline(
                         new DW.Extent() { Cx = 990000L, Cy = 792000L },
                         new DW.EffectExtent()
                             LeftEdge = 0L,
                             TopEdge = 0L,
                             RightEdge = 0L,
                             BottomEdge = 0L
                         new DW.DocProperties()
                             Id = (UInt32Value)1U,
                             Name = "Picture 1"
                         new DW.NonVisualGraphicFrameDrawingProperties(
                             new A.GraphicFrameLocks() { NoChangeAspect = true }),
                         new A.Graphic(
                             new A.GraphicData(
                                 new PIC.Picture(
                                     new PIC.NonVisualPictureProperties(
                                         new PIC.NonVisualDrawingProperties()
                                             Id = (UInt32Value)0U,
                                             Name = "New Bitmap Image.jpg"
                                         new PIC.NonVisualPictureDrawingProperties()),
                                     new PIC.BlipFill(
                                         new A.Blip(
                                             new A.BlipExtensionList(
                                                 new A.BlipExtension()
                                                     Uri =
                                             Embed = relationshipId,
                                             CompressionState =
                                         new A.Stretch(
                                             new A.FillRectangle())),
                                     new PIC.ShapeProperties(
                                         new A.Transform2D(
                                             new A.Offset() { X = 0L, Y = 0L },
                                             new A.Extents() { Cx = 990000L, Cy = 792000L }),
                                         new A.PresetGeometry(
                                             new A.AdjustValueList()
                                         ) { Preset = A.ShapeTypeValues.Rectangle }))
                             ) { Uri = "" })
                         DistanceFromTop = (UInt32Value)0U,
                         DistanceFromBottom = (UInt32Value)0U,
                         DistanceFromLeft = (UInt32Value)0U,
                         DistanceFromRight = (UInt32Value)0U,
                         EditId = "50D07946"

            // Append the reference to body, the element should be in a Run.
            wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));


DocumentBuilder is a powerful class that is associated with a Document and allows dynamic document building from scratch or the addition of new elements to an existing document. It provides methods to insert text, paragraphs, lists, tables, images and other contents, specification of font, paragraph, and section formatting, and other things. Using DocumentBuilder is somewhat similar in concept to using the StringBuilder class of the .NET Framework.

DocumentBuilder complements classes and methods available in the Aspose.Words Document Object Model by simplifying most common document building tasks, such as inserting text, tables, fields and hyperlinks.

using Aspose.Words;

Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // We call this method to start building the table.
            builder.Write("Row 1, Cell 1 Content.");

            // Build the second cell
            builder.Write("Row 1, Cell 2 Content.");
            // Call the following method to end the row and start a new row.

            // Build the first cell of the second row.
            builder.Write("Row 2, Cell 1 Content");

            // Build the second cell.
            builder.Write("Row 2, Cell 2 Content.");

            // Signal that we have finished building the table.

            // Save the document to disk.
            doc.Save("DocumentBuilder.CreateSimpleTable Out.doc");


Last edited Jan 8, 2014 at 10:02 AM by asposemarketplace, version 1