none
Trying to add (or better: replace !) an image in a Word document with the SDK 2.0 RRS feed

  • Question

  • Hi,

    I'm trying to insert an image using this code:

    var

     

     

    imagePart = mainDocumentPart.AddNewPart<ImagePart>("image/jpeg", "rId1"); // Create the relationship
    GenerateImagePart(imagePart, fileName)
    ; // Fill imagePart.GetStream ()
    GenerateMainDocumentPart(imageFileName).Save(mainDocumentPart); // Adding xml with new pic.BlipFill(new a.Blip() { Embed = "rId1" }, new a.Stretch(new a.FillRectangle()))

    The fact is that the file I obtain is corrupted. I can see the imagePart package, the Embed = "rId1" is correctly set, but the I can't see the relation in the docx archive.

    Any idea ?

    Best regards

    Tuesday, November 10, 2009 5:26 PM

Answers

  • Hi, PhilTheGap,

    We are very interested in your usage of the SDK. We'll appreciate you much if you could explain us more details about your scenario, say, what is the purpose of this solution? why do you have to create a memory stream and return byte[] rather than save the WordprocessingDocument directly to a file.

    I guess you are trying to get data from a data source and then create a report with a template, and then save the report data to your database. Is it right?

    I tried your code, and it really generated invalid document.
    And for your solution, I write a complete code sample for you. You could try the following, hoping it will help you detect and resolve the problem. By the way, if it helps, don't forget to let us know your scenario:)

    As to the validation feature I mentioned, please try the DocumentFormat.OpenXml.OpenXmlValidator class, you will get a surprise I believe!

    Code Sample:

    using System;
    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml;
    using System.IO;
    using DocumentFormat.OpenXml.Packaging;
    using wp = DocumentFormat.OpenXml.Drawing.Wordprocessing;
    using a = DocumentFormat.OpenXml.Drawing;
    using pic = DocumentFormat.OpenXml.Drawing.Pictures;
    
    namespace OpenXmlSdkForumAnswers
    {
        class AddImagePart
        {
            public static void Do()
            {
                
                string filename = @"report.docx";
                byte[] reportData = GetWordReport();
                File.WriteAllBytes(filename, reportData);
                Console.WriteLine("File " + filename + " created");
            }
    
            private static byte[] GetWordReport()
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    var template = GetTemplateData();
                    stream.Write(template, 0, template.Length);
                    using (WordprocessingDocument docx = WordprocessingDocument.Open(stream, true))
                    {
                        // Some changes on docx
                        docx.MainDocumentPart.Document = GenerateMainDocumentPart();
    
                        var imagePart = docx.MainDocumentPart.AddNewPart<ImagePart>("image/jpeg", "rIdImagePart1");
                        GenerateImagePart(imagePart);
                    }
                    stream.Seek(0, SeekOrigin.Begin);
                    return stream.ToArray();
                }
            }
    
            private static byte[] GetTemplateData()
            {
                using (MemoryStream targetStream = new MemoryStream())
                using (BinaryReader sourceReader = new BinaryReader(File.Open(@"HelloWorld.docx", FileMode.Open)))
                {
                    byte[] buffer = new byte[4096];
    
                    int num = 0;
                    do
                    {
                        num = sourceReader.Read(buffer, 0, 4096);
                        if (num > 0)
                            targetStream.Write(buffer, 0, num);
                    }
                    while (num > 0);
                    targetStream.Seek(0, SeekOrigin.Begin);
                    return targetStream.ToArray();
                }
            }
    
            private static void GenerateImagePart(OpenXmlPart part)
            {
                using (Stream imageStream = File.Open("image.jpg", FileMode.Open))
                {
                    part.FeedData(imageStream);
                }
            }
    
            private static Document GenerateMainDocumentPart()
            {
                var element =
                    new Document(
                        new Body(
                            new Paragraph(
                                new Run(
                                    new RunProperties(
                                        new NoProof()),
                                    new Drawing(
                                        new wp.Inline(
                                            new wp.Extent() { Cx = 9753600L, Cy = 7315200L },
                                            new wp.EffectExtent() { LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                                            new wp.DocProperties() { Id = (UInt32Value)1U, Name = "Picture 0", Description = "Forest Flowers.jpg" },
                                            new wp.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 = "Forest Flowers.jpg" },
                                                            new pic.NonVisualPictureDrawingProperties()),
                                                        new pic.BlipFill(
                                                            new a.Blip() { Embed = "rIdImagePart1", CompressionState = a.BlipCompressionValues.Print },
                                                            new a.Stretch(
                                                                new a.FillRectangle())),
                                                        new pic.ShapeProperties(
                                                            new a.Transform2D(
                                                                new a.Offset() { X = 0L, Y = 0L },
                                                                new a.Extents() { Cx = 9753600L, Cy = 7315200L }),
                                                            new a.PresetGeometry(
                                                                new a.AdjustValueList()
                                                            ) { Preset = a.ShapeTypeValues.Rectangle }))
                                                ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                                        ) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U }))
                            ) { RsidParagraphAddition = "00A2180E", RsidRunAdditionDefault = "00EC4DA7" },
                            new SectionProperties(
                                new PageSize() { Width = (UInt32Value)11906U, Height = (UInt32Value)16838U },
                                new PageMargin() { Top = 1440, Right = (UInt32Value)1800U, Bottom = 1440, Left = (UInt32Value)1800U, Header = (UInt32Value)851U, Footer = (UInt32Value)992U, Gutter = (UInt32Value)0U },
                                new Columns() { Space = (UInt32Value)425U },
                                new DocGrid() { Type = DocGridValues.Lines, LinePitch = 312 }
                            ) { RsidR = "00A2180E", RsidSect = "00A2180E" }));
                return element;
            }
        }
    }
    

    • Marked as answer by PhilTheGap Tuesday, November 17, 2009 4:42 PM
    Tuesday, November 17, 2009 4:16 AM
    Moderator

All replies


  • Thanks for you question.

    It seems that you used the tool DocumentReflector to help you write code with Open XML SDK V2.
    The relationship of the image part should be in "\word\_rels\document.xml.rels". You can change the extension name of the docx document to be .zip, and then open the zip archieve. Then you would find the "\word\_rels\document.xml.rels", where the image part relationship is supposed in.

    The code you pasted looks correct. But things might be wrong in GenerateMainDocumentPart. One way to detect the issues is to use the validation feature provided by the SDK.

    Please let us know if you still have questions.

    Thanks!

    Wednesday, November 11, 2009 2:05 AM
    Moderator
  • Thank you for your answer.

    The relation ship is not created, and thus I can't load the document with Word (I've got the read cross in the image frame).

    However I assumed that the code:
    imagePart = mainDocumentPart.AddNewPart<ImagePart>("image/jpeg", "rId1"); // Create the relationship
    or better
    var imagePart = element.AddImagePart(ImagePartType.Jpeg, "rId1"); // or null to generate the Id
    should automatically create the relationShip.

    I really don't know what to do... What about the "validation feature" you mention ?

    Thanks

    Monday, November 16, 2009 11:36 AM
  • I wonder whether this code is correct:

     

     

    void Main () {
      byte
    [] data = FormFiller.GetWordReport();
      string filename = @"D:/Users/administrateur/Documents/report.docx";
      File.WriteAllBytes(filename, data);
      MessageBox.Show("File " + filename + " created");
    }

     

     

    public static byte[] GetWordReport()
    {
       using (MemoryStream stream = new MemoryStream())
       {
          stream.Write(original,
    0, original.Length); // Create a Wordprocessing document object.     
          using (WordprocessingDocument docx = WordprocessingDocument.Open(stream, true))
          {
              // Some changes on docx
              ... adding an image
              docx.MainDocumentPart.Document.Save(); // save main document back in package
              // get package bytes
              stream.Seek(0, SeekOrigin.Begin);
              byte[] data = stream.ToArray();
              return data;
          }
       }
    }

    Monday, November 16, 2009 3:05 PM
  • Since your senario is create a wordprocessing document instread of open an existing one, I suggest you to use "WordprocessingDocument.Creat()" to replace "WordprocessingDocument.Open()". Following is a code snippet to create a word document which only contains a picture.

    =======================================================

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Wordprocessing;
    using System.IO;
    using ap = DocumentFormat.OpenXml.ExtendedProperties;
    using wp = DocumentFormat.OpenXml.Drawing.Wordprocessing;
    using a = DocumentFormat.OpenXml.Drawing;
    using pic = DocumentFormat.OpenXml.Drawing.Pictures;
    using m = DocumentFormat.OpenXml.Math;
    using ovml = DocumentFormat.OpenXml.Vml.Office;
    using v = DocumentFormat.OpenXml.Vml;

    namespace Forum
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (WordprocessingDocument package = WordprocessingDocument.Create(@"D:\share\test.docx", WordprocessingDocumentType.Document))
                {
                    var mainDocumentPart = package.AddMainDocumentPart();
                    GenerateMainDocumentPart().Save(mainDocumentPart);

                    var imagePart = mainDocumentPart.AddNewPart<ImagePart>("image/jpeg", "rId1");
                    GenerateImagePart(imagePart);

                }

            }

            public static void GenerateImagePart(OpenXmlPart part)
            {
                using (Stream targetStream = part.GetStream())
                using (BinaryReader sourceReader = new BinaryReader(File.Open(@"D:\share\Desert.jpg", FileMode.Open)))
                {
                    byte[] buffer = new byte[4096];

                    int num = 0;
                    do
                    {
                        num = sourceReader.Read(buffer, 0, 4096);
                        if (num > 0)
                            targetStream.Write(buffer, 0, num);
                    }
                    while (num > 0);
                }
            }

            private static Document GenerateMainDocumentPart()
            {
                var element =
                    new Document(
                        new Body(
                            new Paragraph(
                                new Run(
                                    new RunProperties(
                                        new NoProof()),
                                    new Drawing(
                                        new wp.Inline(
                                            new wp.Extent() { Cx = 9753600L, Cy = 7315200L },
                                            new wp.EffectExtent() { LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                                            new wp.DocProperties() { Id = (UInt32Value)1U, Name = "Picture 0", Description = "Forest Flowers.jpg" },
                                            new wp.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 = "Forest Flowers.jpg" },
                                                            new pic.NonVisualPictureDrawingProperties()),
                                                        new pic.BlipFill(
                                                            new a.Blip() { Embed = "rId4", CompressionState = a.BlipCompressionValues.Print },
                                                            new a.Stretch(
                                                                new a.FillRectangle())),
                                                        new pic.ShapeProperties(
                                                            new a.Transform2D(
                                                                new a.Offset() { X = 0L, Y = 0L },
                                                                new a.Extents() { Cx = 9753600L, Cy = 7315200L }),
                                                            new a.PresetGeometry(
                                                                new a.AdjustValueList()
                                                            ) { Preset = a.ShapeTypeValues.Rectangle }))
                                                ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                                        ) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U }))
                            ) { RsidParagraphAddition = "00A2180E", RsidRunAdditionDefault = "00EC4DA7" },
                            new SectionProperties(
                                new PageSize() { Width = (UInt32Value)11906U, Height = (UInt32Value)16838U },
                                new PageMargin() { Top = 1440, Right = (UInt32Value)1800U, Bottom = 1440, Left = (UInt32Value)1800U, Header = (UInt32Value)851U, Footer = (UInt32Value)992U, Gutter = (UInt32Value)0U },
                                new Columns() { Space = (UInt32Value)425U },
                                new DocGrid() { Type = DocGridValues.Lines, LinePitch = 312 }
                            ) { RsidR = "00A2180E", RsidSect = "00A2180E" }));
                return element;
            }
        }

    }

    =============================================================


    Z.J.
    Tuesday, November 17, 2009 2:14 AM
  • Hi, PhilTheGap,

    We are very interested in your usage of the SDK. We'll appreciate you much if you could explain us more details about your scenario, say, what is the purpose of this solution? why do you have to create a memory stream and return byte[] rather than save the WordprocessingDocument directly to a file.

    I guess you are trying to get data from a data source and then create a report with a template, and then save the report data to your database. Is it right?

    I tried your code, and it really generated invalid document.
    And for your solution, I write a complete code sample for you. You could try the following, hoping it will help you detect and resolve the problem. By the way, if it helps, don't forget to let us know your scenario:)

    As to the validation feature I mentioned, please try the DocumentFormat.OpenXml.OpenXmlValidator class, you will get a surprise I believe!

    Code Sample:

    using System;
    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml;
    using System.IO;
    using DocumentFormat.OpenXml.Packaging;
    using wp = DocumentFormat.OpenXml.Drawing.Wordprocessing;
    using a = DocumentFormat.OpenXml.Drawing;
    using pic = DocumentFormat.OpenXml.Drawing.Pictures;
    
    namespace OpenXmlSdkForumAnswers
    {
        class AddImagePart
        {
            public static void Do()
            {
                
                string filename = @"report.docx";
                byte[] reportData = GetWordReport();
                File.WriteAllBytes(filename, reportData);
                Console.WriteLine("File " + filename + " created");
            }
    
            private static byte[] GetWordReport()
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    var template = GetTemplateData();
                    stream.Write(template, 0, template.Length);
                    using (WordprocessingDocument docx = WordprocessingDocument.Open(stream, true))
                    {
                        // Some changes on docx
                        docx.MainDocumentPart.Document = GenerateMainDocumentPart();
    
                        var imagePart = docx.MainDocumentPart.AddNewPart<ImagePart>("image/jpeg", "rIdImagePart1");
                        GenerateImagePart(imagePart);
                    }
                    stream.Seek(0, SeekOrigin.Begin);
                    return stream.ToArray();
                }
            }
    
            private static byte[] GetTemplateData()
            {
                using (MemoryStream targetStream = new MemoryStream())
                using (BinaryReader sourceReader = new BinaryReader(File.Open(@"HelloWorld.docx", FileMode.Open)))
                {
                    byte[] buffer = new byte[4096];
    
                    int num = 0;
                    do
                    {
                        num = sourceReader.Read(buffer, 0, 4096);
                        if (num > 0)
                            targetStream.Write(buffer, 0, num);
                    }
                    while (num > 0);
                    targetStream.Seek(0, SeekOrigin.Begin);
                    return targetStream.ToArray();
                }
            }
    
            private static void GenerateImagePart(OpenXmlPart part)
            {
                using (Stream imageStream = File.Open("image.jpg", FileMode.Open))
                {
                    part.FeedData(imageStream);
                }
            }
    
            private static Document GenerateMainDocumentPart()
            {
                var element =
                    new Document(
                        new Body(
                            new Paragraph(
                                new Run(
                                    new RunProperties(
                                        new NoProof()),
                                    new Drawing(
                                        new wp.Inline(
                                            new wp.Extent() { Cx = 9753600L, Cy = 7315200L },
                                            new wp.EffectExtent() { LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                                            new wp.DocProperties() { Id = (UInt32Value)1U, Name = "Picture 0", Description = "Forest Flowers.jpg" },
                                            new wp.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 = "Forest Flowers.jpg" },
                                                            new pic.NonVisualPictureDrawingProperties()),
                                                        new pic.BlipFill(
                                                            new a.Blip() { Embed = "rIdImagePart1", CompressionState = a.BlipCompressionValues.Print },
                                                            new a.Stretch(
                                                                new a.FillRectangle())),
                                                        new pic.ShapeProperties(
                                                            new a.Transform2D(
                                                                new a.Offset() { X = 0L, Y = 0L },
                                                                new a.Extents() { Cx = 9753600L, Cy = 7315200L }),
                                                            new a.PresetGeometry(
                                                                new a.AdjustValueList()
                                                            ) { Preset = a.ShapeTypeValues.Rectangle }))
                                                ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                                        ) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U }))
                            ) { RsidParagraphAddition = "00A2180E", RsidRunAdditionDefault = "00EC4DA7" },
                            new SectionProperties(
                                new PageSize() { Width = (UInt32Value)11906U, Height = (UInt32Value)16838U },
                                new PageMargin() { Top = 1440, Right = (UInt32Value)1800U, Bottom = 1440, Left = (UInt32Value)1800U, Header = (UInt32Value)851U, Footer = (UInt32Value)992U, Gutter = (UInt32Value)0U },
                                new Columns() { Space = (UInt32Value)425U },
                                new DocGrid() { Type = DocGridValues.Lines, LinePitch = 312 }
                            ) { RsidR = "00A2180E", RsidSect = "00A2180E" }));
                return element;
            }
        }
    }
    

    • Marked as answer by PhilTheGap Tuesday, November 17, 2009 4:42 PM
    Tuesday, November 17, 2009 4:16 AM
    Moderator
  • Hi Jun,

    thank you for your answer. But indeed I need to open a template.

    Philippe
    Tuesday, November 17, 2009 4:41 PM
  • Hi Ethan,

    you really save my day ! Your code is running. My scenario is in fact a merging one: I use a template with merge fields to insert a text or a picture (by the way I suppose that there are not "picture specific" merge fields in Word ?). So I've adapted your code and it's ok.

    By the way,  how do you save the WordprocessingDocument directly to a file (rather than create a memory stream and return byte[] ) ?

    I still don't know why my old code didn't work, but I don't really care.

    The lesson I got from this experience is the following: the SDK 2.0 is still complex, and there is a lack of technical documents on that subject (not to mention books !!). I lost three days for inserting a picture (perhaps I was unlucky !), and my boss wonders whether we need to switch back to Word Automation...

    Thanks again for your help Ethan !!!!

    Phil
    Tuesday, November 17, 2009 4:54 PM
  • You're welcome, Phil!

    Just as what you mentioned, the SDK is still on CTP stage and there are lack of technical documents on it, but we are trying to make the SDK better, and collabrate with developers like you who investigated the SDK at early stage to make more articles about it.

    So please keep in touch with us if you get any questions about the SDK. And we're looking forward to your feedbacks.

    As to the so-called directly saving a document, please try this:
         WordprocessingDocument doc = WordprocessingDocument(filename, WorldprocessingDocumentTypes.Document)
    And then once the doc instance is disposed, the content of the document will be saved to the specified filename. Please have a try.

    Ethan
    Wednesday, November 18, 2009 2:35 AM
    Moderator
  • Hi Ethan.

    I work for a big Aircraft maker as sofdtware architect.I make Open Xml Solutions (vb and c#)based on the sdk 2.
    I push the sdk solution as the "technical standard" solution to générate office documents.

    I use also one Codeplex project named docx.

    I'm interressed to have best practices, or coding standards or presentation materials.

    Feel free to contact me

    Josselin.
    contact@josselinjouannet.com


    Bon Codage Josselin
    Monday, November 23, 2009 10:24 AM
  • Hi Ethan yin,
    The above code which you have posted its really saved my working time . Since last 2 days I am working on this topic but unable to complete the task . Its good because of this post I got result . But I have one more query "IF want to insert image or picture to any part of document with litle modification of size of image .For eg I want to insert image after some paragraph or in between to paragraph . Plz Can you help me  It would great help for me as I am new to this technology. thank you in advance
    Hi, PhilTheGap,

    We are very interested in your usage of the SDK. We'll appreciate you much if you could explain us more details about your scenario, say, what is the purpose of this solution? why do you have to create a memory stream and return byte[] rather than save the WordprocessingDocument directly to a file.

    I guess you are trying to get data from a data source and then create a report with a template, and then save the report data to your database. Is it right?

    I tried your code, and it really generated invalid document.
    And for your solution, I write a complete code sample for you. You could try the following, hoping it will help you detect and resolve the problem. By the way, if it helps, don't forget to let us know your scenario:)

    As to the validation feature I mentioned, please try the DocumentFormat.OpenXml.OpenXmlValidator class, you will get a surprise I believe!

    Code Sample:

    using
     System;
    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml;
    using System.IO;
    using DocumentFormat.OpenXml.Packaging;
    using wp = DocumentFormat.OpenXml.Drawing.Wordprocessing;
    using a = DocumentFormat.OpenXml.Drawing;
    using pic = DocumentFormat.OpenXml.Drawing.Pictures;

    namespace OpenXmlSdkForumAnswers
    {
    class AddImagePart
    {
    public static void Do()
    {

    string filename = @"report.docx" ;
    byte [] reportData = GetWordReport();
    File.WriteAllBytes(filename, reportData);
    Console.WriteLine("File " + filename + " created" );
    }

    private static byte [] GetWordReport()
    {
    using (MemoryStream stream = new MemoryStream())
    {
    var template = GetTemplateData();
    stream.Write(template, 0, template.Length);
    using (WordprocessingDocument docx = WordprocessingDocument.Open(stream, true ))
    {
    // Some changes on docx
    docx.MainDocumentPart.Document = GenerateMainDocumentPart();

    var imagePart = docx.MainDocumentPart.AddNewPart<ImagePart>("image/jpeg" , "rIdImagePart1" );
    GenerateImagePart(imagePart);
    }
    stream.Seek(0, SeekOrigin.Begin);
    return stream.ToArray();
    }
    }

    private static byte [] GetTemplateData()
    {
    using (MemoryStream targetStream = new MemoryStream())
    using (BinaryReader sourceReader = new BinaryReader(File.Open(@"HelloWorld.docx" , FileMode.Open)))
    {
    byte [] buffer = new byte [4096];

    int num = 0;
    do
    {
    num = sourceReader.Read(buffer, 0, 4096);
    if (num > 0)
    targetStream.Write(buffer, 0, num);
    }
    while (num > 0);
    targetStream.Seek(0, SeekOrigin.Begin);
    return targetStream.ToArray();
    }
    }

    private static void GenerateImagePart(OpenXmlPart part)
    {
    using (Stream imageStream = File.Open("image.jpg" , FileMode.Open))
    {
    part.FeedData(imageStream);
    }
    }

    private static Document GenerateMainDocumentPart()
    {
    var element =
    new Document(
    new Body(
    new Paragraph(
    new Run(
    new RunProperties(
    new NoProof()),
    new Drawing(
    new wp.Inline(
    new wp.Extent() { Cx = 9753600L, Cy = 7315200L },
    new wp.EffectExtent() { LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
    new wp.DocProperties() { Id = (UInt32Value)1U, Name = "Picture 0" , Description = "Forest Flowers.jpg" },
    new wp.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 = "Forest Flowers.jpg" },
    new pic.NonVisualPictureDrawingProperties()),
    new pic.BlipFill(
    new a.Blip() { Embed = "rIdImagePart1" , CompressionState = a.BlipCompressionValues.Print },
    new a.Stretch(
    new a.FillRectangle())),
    new pic.ShapeProperties(
    new a.Transform2D(
    new a.Offset() { X = 0L, Y = 0L },
    new a.Extents() { Cx = 9753600L, Cy = 7315200L }),
    new a.PresetGeometry(
    new a.AdjustValueList()
    ) { Preset = a.ShapeTypeValues.Rectangle }))
    ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
    ) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U }))
    ) { RsidParagraphAddition = "00A2180E" , RsidRunAdditionDefault = "00EC4DA7" },
    new SectionProperties(
    new PageSize() { Width = (UInt32Value)11906U, Height = (UInt32Value)16838U },
    new PageMargin() { Top = 1440, Right = (UInt32Value)1800U, Bottom = 1440, Left = (UInt32Value)1800U, Header = (UInt32Value)851U, Footer = (UInt32Value)992U, Gutter = (UInt32Value)0U },
    new Columns() { Space = (UInt32Value)425U },
    new DocGrid() { Type = DocGridValues.Lines, LinePitch = 312 }
    ) { RsidR = "00A2180E" , RsidSect = "00A2180E" }));
    return element;
    }
    }
    }



    Thursday, April 15, 2010 8:45 AM
  • Hi Ethan Yin,
    The above code which you have posted its really saved my working time . Since last 2 days I am working on this topic but unable to complete the task . Its good because of this post I got result . But I have one more query "IF want to insert image or picture to any part of document with litle modification of size of image .For eg I want to insert image after some paragraph or in between to paragraph . Plz Can you help me  It would great help for me as I am new to this technology. thank you in advance
    Thursday, April 15, 2010 8:46 AM
  • {$content}

    Thanks for your help! The code you provided is very useful.
    Monday, November 8, 2010 4:00 PM
  • Hi,
     
    There is a sample called CS/VBManipulateImagesInWordDocument that could

    1.List all images in a document.

    2.Extract an image from a document

    3.Delete an image in the document

    4.Replace an image.

     
    You can download it from (http://1code.codeplex.com/ or use our sample browser tool http://1code.codeplex.com/releases/view/64539) and have a try.
     
    If there is any feedback, feel free to tell me.


    Ruiz Yi [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 11, 2011 3:35 AM
  • Thank you !
    Philippe
    Monday, August 29, 2011 9:21 AM
  • Hi Ruiz,

    now if you have the same code for Excel, I would be grateful...

    Best regards


    Philippe
    Monday, August 29, 2011 9:31 AM