Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenXML: Issue adding images to documents
    text
    copied!<p>Up until now, this block of code has been using to build documents with text for several months with no snags. I am now trying to dynamically add images. I've spent about two days staring at code and researching and am at an end. I suspect the issue is that relationships are not being created (more details below.) Maybe not?</p> <pre><code> //set stuff up... WordprocessingDocument doc = WordprocessingDocument.Open(fsPat, true, new OpenSettings(){ AutoSave = true, MarkupCompatibilityProcessSettings = new MarkupCompatibilityProcessSettings(MarkupCompatibilityProcessMode.ProcessAllParts, DocumentFormat.OpenXml.FileFormatVersions.Office2007), MaxCharactersInPart = long.MaxValue }); MainDocumentPart mainPart = doc.MainDocumentPart; . .Other stuff goes here . //now the fun... Run r2 = new Run(); // Add an ImagePart. ImagePart ip = mainPart.AddImagePart(ImagePartType.Png); string imageRelationshipID = mainPart.CreateRelationshipToPart(ip); // using (Stream imgStream = ip.GetStream()) { System.Drawing.Bitmap b = new System.Drawing.Bitmap("myfile.png"); b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png); } Drawing drawing = BuildImage(imageRelationshipID, "name"+imageRelationshipID.ToString(), 17, 17); r2.Append(drawing); p.Append(r2); </code></pre> <p>The image part is essentially copied from <a href="http://blog.stuartwhiteford.com/?p=33" rel="nofollow">http://blog.stuartwhiteford.com/?p=33</a>) and is running in a loop presently. I also copied his BuildImage() function and use it as-is.</p> <p>When I open the resulting docx, I see red Xs where the images are saying "This image cannot currently be displayed."</p> <p>When I open the zip, the images will appear in root/media, but not root/word/media as I'd expect. I also cannot find the images referenced in any of the relationship files. Ideally they'd be in root/word/_rels/document.xml.rels. You'll notice I changed how imageRelationshipID is set hoping to fix this. It didn't.</p> <p>Please help. Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload