Monday, 28 January 2013

Open XML 2.0 and Word Processing

I am sure those who have tried his hands on Open XML SDK, must have used Word Content Control Toolkit available at codeplex.

The exe is prepared by Microsoft itself and pretty old but can be used to create word document quickly through code. It makes template creation for Mail Merge like solution very easy.

Use the tool to map your content controls with custom xml and use the below code to replace your content with xml data.

Below code has ContentXML variable that has required data.

using (MemoryStream ms = new MemoryStream())
                ms.Write(_templateDocument, 0, _templateDocument.Length);
                using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(ms, true))

                    MainDocumentPart main = wordDocument.MainDocumentPart;
                    Document document = main.Document;

                    //add and write new XML part
                    CustomXmlPart customXmlPart =   main.AddCustomXmlPart(CustomXmlPartType.CustomXml);
                    using (StreamWriter ts = new StreamWriter(customXmlPart.GetStream()))



No comments:

Post a Comment