I generate an SSRS report that I save as a Word document. That document contain pictures (charts as plat images). I don't really save the Word document, I have it open in memory.
I use then OpenXML to create another word document, in witch I need to copy the pictures from the SSRS report.
I try to get the drawings from the ssrs report, and insert them in the new word document
MainDocumentPart ssrsMainPart = ssrsDoc.MainDocumentPart;
var drawings = ssrsMainPart.Document.Descendants<Drawing>().ToList();
and then insert them in the new document
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
Body body = mainPart.Document.Body;
for (int i = 0; i < 10; i++)
{
if (i < drawings.Count())
{
var myDrawing = drawings.Skip(i).First();
myDrawing.Remove();
body.Append(myDrawing);
body.Append(new Paragraph());
}
}
As result I get in the resulting document only the picture placeholders, of the same size as in ssrs Word Document, but the pictures are empty.
I tried to copy the ImageData as well, using the following code:
foreach (var e in ssrsDoc.MainDocumentPart.Document.Body.Elements())
{
var clonedElement = e.CloneNode(true);
clonedElement.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().ToList().ForEach(blip =>
{
var newRelation = wordDoc.CopyImage(blip.Embed, ssrsDoc);
blip.Embed = newRelation;
});
clonedElement.Descendants<DocumentFormat.OpenXml.Vml.ImageData>().ToList().ForEach(imageData =>
{
var newRelation = wordDoc.CopyImage(imageData.RelationshipId, ssrsDoc);
imageData.RelationshipId = newRelation;
});
wordDoc.MainDocumentPart.Document.Body.AppendChild(clonedElement);
}
with CopyImage defined like this:
public static string CopyImage(this WordprocessingDocument newDoc, string relId, WordprocessingDocument org)
{
var p = org.MainDocumentPart.GetPartById(relId) as ImagePart;
var newPart = newDoc.MainDocumentPart.AddPart(p);
newPart.FeedData(p.GetStream());
return newDoc.MainDocumentPart.GetIdOfPart(newPart);
}
but after that I wasn't able to open anymore the document with Word 365, word reporting the doc as a broken one...
I'd comment here, but I don't have enough reputation. Anyway I've discovered a similar issue when attempting to spool out image file names. After getting the blip, and then obtaining its relationship ID, and then retrieving the URI, I've discovered that none of these URIs matches images.
For example, I'll have a Word file with 16 images. When manually deconstructing the .docx file as a .zip file, I can locate the images in
/word/media/. They'll be sequentially numbered fromimage1.jpegall the way toimage16.jpeg, but those aren't returned through programming.The OpenXML SDK appears to return a mishmash of varying image numbers, some with 4 digits. Base 10 counting doesn't reach any 4 digit numbers between 1 and 16. There's something going wrong in the OpenXML SDK, and rolling back to earlier versions hasn't helped me with my problem.
EDIT: By way of a potential answer: you can use OpenXML Power Tools instead. Converting the document source via their HTMLConverter permits you to get the correct image file names. This VB.net code should be convertable into C to get you the results you need...
When you run the code, it will process in the image handler, store files in the local directory
/img/and you should also get aListof images.