当 WordProcessingDocument 从 byte[] 创建时,不会创建功能区

发布于 2024-10-09 13:33:07 字数 2405 浏览 9 评论 0原文

当我从 docx 文件的路径打开 WordProcessingDocument 时,会创建功能区。但是,当我从 docx 文件的字节内容打开相同的内容时,它不会被创建。下面不显示 docx 文件中的功能区:-

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

下面显示功能区:-

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() 获取功能区的 XML。我正在删除添加到功能区元素中的 xmlns="" (来自问题 此处)。

有人可以提出一些想法为什么没有添加功能区。即使是 customUI 文件夹也不会添加到 word zip 文件中。有人可以帮忙吗。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫林﹌晚霞¤ 2024-10-16 13:33:07

我应该通过将流写入文件来验证流。以下是代码:-

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


                }

         }

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


                }

         }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文