当我从 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 文件中。任何人都可以帮忙。