Copy Image between documents using OpenXml and C#
I have two documents, I need to copy a picture from one document to the other. I can't use altChunks because I need to do further editing on the file.
I tried taking a clone of the sdtBlock that the image is in and appending that to the other document.
Like..
sdtBlock = document2.Decendants<StdBlock>开发者_JAVA百科;.First().Clone(); //Block with image and text etc...
WordprocessingDocument oDoc = WordprocessingDocument.Open(document1, true);
Body body = oDoc.MainDocumentPart.Document.Body;
body.InsertAfter(sdtBlock, body.Elements<Paragraph>().First()); //insert block into new doc
That works for everything except the image. The image appears as either a red X or the document shows as corrupt. If you take the image out this method works fine.
Looking on msdn I think its because I need to create a relationship for that image?
Can anyone show me how this is possible?
Thanks!
Yes, you need to copy the image part across, and add a rel pointing to it. You need to make sure the relId in the paragraph matches the relId of the rel you added.
Have a look at DocumentBuilder in http://powertools.codeplex.com/ for how to do this.
精彩评论