1

I am adding images to a DOCX files using the WordprocessingDocument method found here (Open XML) http://msdn.microsoft.com/en-us/library/bb497430.aspx.

I can add images, but the sizing is not correct.

MainDocumentPart mainPart = doc.MainDocumentPart;
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
using (System.IO.FileStream stream = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
      {
           imagePart.FeedData(stream);
      }


return AddImageToBody(doc, mainPart.GetIdOfPart(imagePart), fileName);

private static Drawing AddImageToBody(WordprocessingDocument wordDoc, string relationshipId, string filename)
    { 
       long imageWidthEMU = 1900000;
       long imageHeightEMU = 350000;

       double imageWidthInInches = imageWidthEMU / 914400.0;
       double imageHeightInInches = imageHeightEMU / 914400.0;

        new DW.Extent();

        //Define the reference of the image.
        var element =
             new Drawing(
                 new DW.Inline(
                     new DW.Extent() { Cx = imageWidthEMU, Cy = imageHeightEMU },

As you can see, you specify the sizes (length + width) manually. I am unable to get them dynamically. How can you get the right correct image size to pass to this code?

Thanks.

jn1kk
  • 5,012
  • 2
  • 45
  • 72

1 Answers1

1

Your problem solved here: Inserting Image into DocX using OpenXML and setting the size

There is no way to solve it via instruction in the xml file. OOXML offers only <a:fill> and <a:tile> options.

Community
  • 1
  • 1
Aleksey
  • 26
  • 1