Thursday 29 December 2011

Replacing content controls in the word document by using OPENXML 2.0 concept

create one 
   
string strFilePath = Server.MapPath("template.docx");
        string docOutputPath = @"d:\output.docx";
        File.Copy(strFilePath, docOutputPath);
        using (WordprocessingDocument myDocument = WordprocessingDocument.Open(docOutputPath, true))
        {

           string newXml = "<root>" +
                        "<SdkName>asdf</SdkName>" +
                        "<Version>1.0</Version>" +
                        "</root>";
             //AddCustomXMLPart() 
            MainDocumentPart main = myDocument.MainDocumentPart;
            main.DeleteParts<CustomXmlPart>(main.CustomXmlParts);

            //add and write new XML part
            CustomXmlPart customXml = main.AddCustomXmlPart(CustomXmlPartType.CustomXml);
            using (StreamWriter ts = new StreamWriter(customXml.GetStream()))
            {
                ts.Write(newXml);
            }
        }

No comments:

Post a Comment

Please Give Your Valuable Comments on this Topic