I'm generating Word(.docx) document and experiencing an issue of how to insert diagrams, images instead of custom tags in document that are already there. But initially I create document from several .RTF files, using "Altchunk" approach, inserting .RTFs into content controls in template doc. Those .RTF files contain our custom tags, they look like "<ElementType="Diagram" Name="Somename">" and appear in document just with the new line like shown below.(And we don't know, in advance, how many those tags will be in doc) .......
<ElementType="Diagram" Name="Some name of the diagram">
....... Googled it but can't find good approach of how to replace tags with images. Because first I need to read tag in order to recognize the name of image, so I know what image will go instead of tag, then remove tag(just text) from document and on its place put an image I just fetched, knowing the name of the image. So it is pretty complicated, but I hope someone have good ideas to share. Thanks
I would like to describe this issue a little bit more specific:
1) Regarding altchunk method, I just find content controls which I put in word document that I use as template, and when I have rtf string, I insert altchunk after that CC.
my method looks like the following:
private void ReplaceContentControlWithRTF(MainDocumentPart mainPart, string tagName, string rtfString)
{
SdtBlock sb = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == tagName).Single();
string altChunkId = "altChunkId" + altChunkIdNumber++;
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Rtf, altChunkId);
MemoryStream s = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(rtfString));
chunk.FeedData(s);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
DocumentFormat.OpenXml.OpenXmlElement parent = sb.Parent;
parent.InsertAfter(altChunk, sb);
sb.Remove();
mainPart.Document.Save();
}