0
votes

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();

}
1

1 Answers

1
votes

My answer got quite long, so I'll separate the contents with some headings in order to make it easier to read :). You should consider each of them unless you already found a better approach.

Concatenating documents

I suppose that when you use altchunk method, you have then an xml of the first document (an original one) and all appended documents are encoded. Better approach would be to concatenate contents of documents, because you have then easy access to the xml tree of the whole document. It's quite easy if you don't use images, hyperlinks etc. in concatenated documents, because they need to be handled in a special way and can make it more complicated.

Alternatively, you can try to insert images to each of the document BEFORE concatenating them.

Use Content Controls as placeholders in the document

Now, supposing you already can access document, you should place Content Controls in Word documents, in places you want your images to appear and set Tag of each of them to a value you should then search in your code in order to find the right place to insert the picture. You can find some basic information about Content Controls here: http://office.microsoft.com/en-us/help/content-controls-HA010030750.aspx.

Insert XML tags and image's bytes

In order to insert an image to a document, you have not only to insert some XML tags in a place you want the image to appear, but also add bytes containing the image as an ImagePart. I've found a tutorial explaning how to do it here (although I haven't followed it myself): http://msdn.microsoft.com/en-us/library/ee342530%28v=office.12%29.aspx.

Consider using OpenXML SDK 2.0 Productivity Tool

In order to make your work with OpenXML SDK simpler, I recommend you to use SDK Productivity Tool from Microsoft. It can be donwloaded as a part of the OpenXML SDK and it allows you to open any MS Office OpenXML document, show its contents in XML manner and can even display C# code required to recreate it. You can download it from here: http://www.microsoft.com/download/en/details.aspx?id=5124.

If you have any other questions, feel free to ask :).