none
Inserting image with OpenXml 2.0, Error with contents in Word 2007 RRS feed

All replies

  • Hello Sean, 

    Thanks for posting. I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience. Have a nice day.


    Bessie Zhao [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.

    Monday, January 17, 2011 7:22 AM
  • Great, looking forward to your response.

    Thanks

    Tuesday, January 18, 2011 8:37 PM
  • The message you report is due to some unexpected content in the document when Word 2007 attempts to open it.  To determine what should be there you can compare the contents of a Word 2007 xml document with the Word 2010 document you can build successfully.  Through the user interface of Word 2007 create a new .docx file with an embedded image. Save the file, then change its extension to .zip. Then unpackage it by extracting the contents.  See the MSDN article:

    - How to: Manipulate Office Open XML Formats Documents
    http://msdn.microsoft.com/en-us/library/aa982683(office.12).aspx
    to get more information on unpackaging. The components are files that show the XML and  .rels relationships of all of the items in the document. Do the same steps with a similar Word 2010 .docx file and compare it with the components in the Word 2007 file. There is probably a different url in the _rels file that defines the differences.  Your code has hard-coded something in the document that fits Word 2010 but points to the wrong place with Word 2007. Otherwise, the same file should be  be opened with either version of Word.

    For more information about the general issue you can refer to the following content:
    Adding Images to Documents in Word 2007 by Using the Open XML SDK ...
    http://msdn.microsoft.com/en-us/library/ee342530(v=office.12).aspx


    Chris Jensen
    • Proposed as answer by cjatmsModerator Wednesday, January 19, 2011 5:17 PM
    • Unproposed as answer by Sean Sharepoint Thursday, January 20, 2011 10:21 PM
    Wednesday, January 19, 2011 5:17 PM
    Moderator
  • I have followed your advice and have examined the differences. To reiterate: When I build a doc without a picture, it works in both 2007 and 2010. Only when I build the doc with a picture does it bomb in Word 2007. 

     

    So I built the doc without the pic, openned it up, and just copied and pasted a picture in there, and then extracted the document.xml file. The main difference is the namespace declarations on the top of the file. 

    When I create the doc programmatically, with and without a picture the namespace is: 

    <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">

    Yet, when I add a picture after the fact, this is the namespace:

    <w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml">

    Is this the issue? 

    Thursday, January 20, 2011 10:24 PM
  • Also posting the image xml for both docs:

    Image xml as built by code that works in 2010 but not 2007:

    <w:drawing>
     <wp:inline distT="0" distB="0" distL="0" distR="0" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
     <wp:extent cx="1524000" cy="1524000" />
     <wp:effectExtent l="19050" t="0" r="9525" b="0" />
     <wp:wrapThrough wrapText="bothSides" />
     <wp:docPr id="1" name="rId1" descr="Description_rId1" />
     <wp:cNvGraphicFramePr>
     <a:graphicFrameLocks noChangeAspect="1" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" />
     </wp:cNvGraphicFramePr>
     <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
     <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
     <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
      <pic:nvPicPr>
      <pic:cNvPr id="2" name="rId1" descr="Description_rId1" />
      <pic:cNvPicPr />
      </pic:nvPicPr>
      <pic:blipFill>
      <a:blip r:embed="rId1" cstate="print" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" />
      <a:stretch>
      <a:fillRect />
      </a:stretch>
      </pic:blipFill>
      <pic:spPr>
      <a:xfrm>
      <a:off x="0" y="0" />
      <a:ext cx="1524000" cy="1524000" />
      </a:xfrm>
      <a:prstGeom prst="rect">
      <a:avLst />
      </a:prstGeom>
      </pic:spPr>
     </pic:pic>
     </a:graphicData>
     </a:graphic>
     </wp:inline>
     </w:drawing>
    
    Image XML when picture is copied and pasted in:
    
    <w:drawing>
     <wp:inline distT="0" distB="0" distL="0" distR="0">
     <wp:extent cx="952500" cy="790575"/>
     <wp:effectExtent l="19050" t="0" r="0" b="0"/>
     <wp:docPr id="1" name="ctl00_PlaceHolderLeftNavBarTop_PictureUrlImage" descr="User Photo"/>
     <wp:cNvGraphicFramePr>
     <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
     </wp:cNvGraphicFramePr>
     <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
     <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
     <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
      <pic:nvPicPr>
      <pic:cNvPr id="0" name="ctl00_PlaceHolderLeftNavBarTop_PictureUrlImage" descr="User Photo"/>
      <pic:cNvPicPr>
      <a:picLocks noChangeAspect="1" noChangeArrowheads="1"/>
      </pic:cNvPicPr>
      </pic:nvPicPr>
      <pic:blipFill>
      <a:blip r:embed="rId7" cstate="print"/>
      <a:srcRect/>
      <a:stretch>
      <a:fillRect/>
      </a:stretch>
      </pic:blipFill>
      <pic:spPr bwMode="auto">
      <a:xfrm>
      <a:off x="0" y="0"/>
      <a:ext cx="952500" cy="790575"/>
      </a:xfrm>
      <a:prstGeom prst="rect">
      <a:avLst/>
      </a:prstGeom>
      <a:noFill/>
      <a:ln w="9525">
      <a:noFill/>
      <a:miter lim="800000"/>
      <a:headEnd/>
      <a:tailEnd/>
      </a:ln>
      </pic:spPr>
     </pic:pic>
     </a:graphicData>
     </a:graphic>
     </wp:inline>
     </w:drawing>
    
    
    Thursday, January 20, 2011 10:38 PM
  • Hello Sean,

    Since you have not posted all of the project that builds the Word 2010 .docx file any comment has to be made after visually inspecting the snippets in your to posts, above. Those are helpful but don't expose the reason for the message you're seeing.

    You might see the differences that cause the problem by building a simple Word 2010 document with two paragraphs through the user interface, saving it with one name, then between the paragraphs, either through the Word user interface, inserting a shape, or manually pasting a picture - e.g a .jpg. Save the document with a different name.


     The first test is to try to open each in Word 2007.  (when I do that either document opens successfully.)  Next, change the extension on each document from .docx to zip. Using WinZip or another tool extract the contents of the document. 
    You will get a _rels folder, a docProps folder, a word folder, and a [Content_Types].xml folder. In the word folder there will be three folders - _rels, media, and theme, and 6 .xml files. The files fontTable.xml, settings.xml, styles.xml, stylesWithEffects.xml and webSettings.xml will be the same for both the document with picture and the one without the picture.
    The key purpose of looking at the xml of each of these test documents is to compare them to the xml of the document into which your program pastes or inserts the same picture.  If all three appear to be functionally equivalent then explore the differences among the other components of the documents, starting with the [Content_Types].xml, and the .rels files, both in the external _rels folder, and more importantly the _rels folder inside the word folder - that contains the relIDs of the elements referred to in the document.xml file.

    This research can divulge the factor that causes the problem. If not you may need the assistance of a support engineer. Because of its complexity your question falls into the paid support category which requires a more in-depth level of support.  Please visit the below link to see the various paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone


    Chris Jensen
    Friday, January 21, 2011 10:38 PM
    Moderator
  • Comparing the extracted xml snippet from your earlier post, I made the comparison of some of the elements with those from a .docx with an image built by Word 2010.

    In the following block

    <w:drawing>
     <wp:inline distT="0" distB="0" distL="0" distR="0" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
     <wp:extent cx="1524000" cy="1524000" />
     <wp:effectExtent l="19050" t="0" r="9525" b="0" />
     <wp:wrapThrough wrapText="bothSides" />
     <wp:docPr id="1" name="rId1" descr="Description_rId1" />
    
     <wp:effectExtent l="19050" t="0" r="9525" b="0" />
    says the top and the bottom define an empty line. Is that what you want?

    <wp:docPr id="1" name="rId1" descr="Description_rId1" />

    Says you have a ref id of 1 pointing to a part named rid1. Is that appropriate, based on your rels file's reference IDs?

    The following is the first of the xml within the <drawing ></drawing> element of the document.xml  created by the exercise in the tutorial from the C:\2007 Office System Developer.docx document built using Office 2010, then exported to an Office 2007 system (where it opened successfully). Then the .docx was unwrapped to get the contents of the document.xml part.

    w:drawing>
    - <wp:inline distT="0" distB="0" distL="0" distR="0">
      <wp:extent cx="2371725" cy="1800225" />
      <wp:effectExtent l="0" t="0" r="9525" b="9525" />
      <wp:docPr id="1" name="Picture 1" />
    - <wp:cNvGraphicFramePr>
      <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1" />
      </wp:cNvGraphicFramePr>
    - <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
    - <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
    - <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
    - <pic:nvPicPr>
      <pic:cNvPr id="0" name="Eagle1.gif" />
      <pic:cNvPicPr />
      </pic:nvPicPr>
    - <pic:blipFill>
    - <a:blip r:embed="rId8">
    - <a:extLst>
    - <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
      <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" />
      </a:ext>
      </a:extLst>
      </a:blip>
    - <a:stretch>
      <a:fillRect />
      </a:stretch>
      </pic:blipFill>
    - <pic:spPr>
    - <a:xfrm>
      <a:off x="0" y="0" />
      <a:ext cx="2371725" cy="1800225" />
      </a:xfrm>
    - <a:prstGeom prst="rect">
      <a:avLst />
      </a:prstGeom>
      </pic:spPr>
      </pic:pic>
      </a:graphicData>
      </a:graphic>
      </wp:inline>
      </w:drawing>
    A comparison of this element to the one in your snippet shows differences that might not be significant, but a single attribute that doesn't fit the Word 2007 parser is probably the cause of the message you see when you try to open your document in Word 2007.

    Monday, January 24, 2011 10:32 PM
    Moderator
  • Hello,

    I've ran into the same problem. I've determined that the SDK generates <wp:inline> tags that refer to Word 2010 namespace, like this:

    <wp:inline distT="0" distB="0" distL="0" distR="0" wp14:editId="50D07946" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing">

    Naturally, Word 2007 does not understand them.

    So I'm looking for a way to generate document compatible with Word 2007 using a newer SDK. I tried using OpenSettings.MarkupCompatibilityProcessSettings with FileFormatVersions.Office2007, but it did not help. Any suggestions?

    Monday, December 23, 2013 1:43 PM
  • Did you solve this issue?

    I'm trying to add an image to an existing Word 2010 document using OpenXML SDK 2.5. But when I add the image the image does not get embedded. When I open the document, the image shows the placeholder with a red cross (as if the image cannot be found). I'm using the following code:

    string mimetype = String.Empty;
    // :
    //Find mime type of the image here
    // :
    imagePart = wpd.MainDocumentPart.AddImagePart(mimetype);
    
    using (FileStream stream = new FileStream(filename, FileMode.Open))
    {
        image = new Bitmap(stream);
        cy = Convert.ToUInt32(image.PhysicalDimension.Height);
        cx = Convert.ToUInt32(image.PhysicalDimension.Width);
        imagePart.FeedData(stream);
        stream.Close();
     }
     Paragraph para = FindParagraphInAppendix(....); //Find the paragraph to add
     if (para != null)
        AddImageToParagraph(para, wpd.MainDocumentPart.GetIdOfPart(imagePart),txt, filename,cx,cy);
     else
          wpd.MainDocumentPart.Document.Body.Append(GetImageWithPara(wpd.MainDocumentPart.GetIdOfPart(imagePart), filename, cx, cy));
    wpd.Package.CreateRelationship(imagePart.Uri, System.IO.Packaging.TargetMode.External,wpd.MainDocumentPart.GetIdOfPart(imagePart));
    wpd.MainDocumentPart.Document.Save();

    In the code for AddImageToParagraph I'm adding the image as follows:

    Pic.BlipFill blipFill1 = new Pic.BlipFill();
    A.Blip blip1 = new A.Blip() { Embed = relationshipid,
                                 CompressionState = A.BlipCompressionValues.Print };
    A.BlipExtensionList blipExtensionList1 = new A.BlipExtensionList();

    When I open the file generated using Winzip, the document.xml.rels file does not contain a relationship ID associated with the embedded image.

    When I open the using OpenXML Productivity tool and validate the XML I get the error : "The relationship 'R75a8cc179...' referenced by attribute 'hxxp://schemas.openxmlformats..../relationships:embed' does not exist

    But what strikes as odd is imagePart.AddImagePart is not creating a <Relationship> tag in the resulting document.xml.rels. Using a debugger and stepping through, I checked the output of wpd.GetReferenceRelationship(wpd.MainDocumentPart.GetIdOfPart(imagePart)) and it throws a System.Generic.Collections.KeyNotFoundException.

    Can you help?



    • Edited by nopsled Wednesday, January 29, 2014 1:12 AM
    Tuesday, January 28, 2014 2:26 PM