当 WordProcessingDocument 从 byte[] 创建时,不会创建功能区
当我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我应该通过将流写入文件来验证流。以下是代码:-
I should have verified the stream by writing the same to a file. Following is the code:-