Question

Ribbon gets created when I open the WordProcessingDocument from the path of a docx file. However It does not get created when I open the same from the byte content of the docx file. The below does not show up the ribbon in the docx file:-

public void AddRibbonToTemplate(byte[] templateContent)
        {
                string sanitizedRibbonXml = GetRibbonXML().ToString().Replace("xmlns=\"\"", "");
                MemoryStream stream = new MemoryStream();
                stream.Write(templateContent, 0, templateContent.Length);
                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(stream, true))
                {
                    MainDocumentPart mainPart = myDoc.MainDocumentPart;

                    if (myDoc.GetPartsCountOfType<RibbonExtensibilityPart>() > 0)
                        myDoc.DeletePart(myDoc.GetPartsOfType<RibbonExtensibilityPart>().First());

                    RibbonExtensibilityPart ribbonExtensibilityPart = myDoc.AddNewPart<RibbonExtensibilityPart>();
                    ribbonExtensibilityPart.CustomUI = new DocumentFormat.OpenXml.Office.CustomUI.CustomUI(sanitizedRibbonXml);
                    myDoc.CreateRelationshipToPart(ribbonExtensibilityPart);

                }
                stream.Close();
         }

The below shows the ribbon:-

public void AddRibbonToTemplate(string documentFileName)
        {
            string sanitizedRibbonXml = GetRibbonXML().ToString().Replace("xmlns=\"\"", "");
            using (WordprocessingDocument myDoc = WordprocessingDocument.Open(documentFileName, true))
            {
                MainDocumentPart mainPart = myDoc.MainDocumentPart;

                if (myDoc.GetPartsCountOfType<RibbonExtensibilityPart>() > 0)
                    myDoc.DeletePart(myDoc.GetPartsOfType<RibbonExtensibilityPart>().First());

                RibbonExtensibilityPart ribbonExtensibilityPart = myDoc.AddNewPart<RibbonExtensibilityPart>();
                ribbonExtensibilityPart.CustomUI = new DocumentFormat.OpenXml.Office.CustomUI.CustomUI(sanitizedRibbonXml);
                myDoc.CreateRelationshipToPart(ribbonExtensibilityPart);
            }
        }

GetRibbonXml() gets the XML of the ribbon. I am removing the xmlns="" which gets added in the ribbon element (from question here).

Could somebody throw some idea why the ribbon does not get added. Even the customUI folder does not get added in the word zip file. Could anybody help.

Was it helpful?

Solution

I should have verified the stream by writing the same to a file. Following is the code:-

public void AddRibbonToTemplate(byte[] templateContent)
        {
                string sanitizedRibbonXml = GetRibbonXML().ToString().Replace("xmlns=\"\"", "");
                using (MemoryStream stream = new MemoryStream())
                {
                    stream.Write(templateContent, 0, (int)templateContent.Length);
                    using (WordprocessingDocument myDoc = WordprocessingDocument.Open(stream, true))
                    {
                        MainDocumentPart mainPart = myDoc.MainDocumentPart;

                        if (myDoc.GetPartsCountOfType<RibbonExtensibilityPart>() > 0)
                            myDoc.DeletePart(myDoc.GetPartsOfType<RibbonExtensibilityPart>().First());

                        RibbonExtensibilityPart ribbonExtensibilityPart = myDoc.AddNewPart<RibbonExtensibilityPart>();
                        ribbonExtensibilityPart.CustomUI = new DocumentFormat.OpenXml.Office.CustomUI.CustomUI(sanitizedRibbonXml);
                        myDoc.CreateRelationshipToPart(ribbonExtensibilityPart);

                    }
                    using (FileStream fileStream = new FileStream("Test20.docx",
                    System.IO.FileMode.CreateNew))
                    {
                        stream.WriteTo(fileStream);
                    }


                }

         }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top