我试图在生成word文档时,用下面的代码来渲染标题部分。
private static Paragraph BuildSubHeaderPart(string headingText)
{
Paragraph headerParagraph = new Paragraph();
var runProperty = new RunProperties();
runProperty.Append(new RunFonts() { Ascii = "Nirmala UI", HighAnsi = "Nirmala UI" }
, new FontSize { Val = new StringValue("22") }
, new Bold()
, new Color() { Val = "55B6DA" });
runProperty.Append(new Text(headingText) { Space = SpaceProcessingModeValues.Default });
headerParagraph.AppendChild(new Run()).Append(runProperty);
return headerParagraph;
}
将标题添加到正文中,如下所示
var mainDocumentPart = wordDoc.AddMainDocumentPart();
Document doc = new Document();
mainDocumentPart.Document = doc;
doc.Body = new Body();
Body body = wordDoc.MainDocumentPart.Document.Body;
body.AppendChild(BuildSubHeaderPart("MECHANICAL SYSTEMS"));
这是很好的工作,现在我正在寻找方式来应用 collapsible/Expand
使用c#代码为这个标题添加功能,如下图所示。
有什么办法可以实现这个功能,我是用Open XML与.Net Core来生成word文档的。
能否有人对这个问题提出任何建议,非常感谢。
更新代码。
Paragraph headerParagraph = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
OpenXmlUnknownElement openXmlUnknownElement1 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<w15:collapsed xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\" />");
paragraphProperties1.Append(openXmlUnknownElement1);
var runProperty = new RunProperties();
runProperty.Append(new RunFonts() { Ascii = "Nirmala UI", HighAnsi = "Nirmala UI" }
, new FontSize { Val = new StringValue("22") }
, new Bold()
, new Color() { Val = "55B6DA" });
runProperty.Append(new Text(headingText) { Space = SpaceProcessingModeValues.Default });
headerParagraph.Append(paragraphProperties1);
headerParagraph.AppendChild(new Run()).Append(runProperty);
你应该可以对一个段落使用这样的代码,但是如果你想让一个段落的所有段落都能使用这样的代码。风格 要折叠,你将需要使用类似的代码来修改样式。
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
// having the level makes it collapsible
// You'll need to change the level as appropriate in { Val = 1 }
// so either you'll need to pass an outline level parameter to your
// method and set the level (or no level)
//or perhaps have a second method to add these outline level paras.
OutlineLevel outlineLevel1 = new OutlineLevel() { Val = 1 };
//Having the w15:collapsed element makes the show as
// collapsed when you open the document.
// if you want it expanded on open, do not add
// this element
OpenXmlUnknownElement openXmlUnknownElement1 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<w15:collapsed xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\" />");
paragraphProperties1.Append(outlineLevel1);
paragraphProperties1.Append(openXmlUnknownElement1);
headerParagraph.Append(paragraphProperties);
如果你还没有遇到Office Open XML Productivity Tool,你可能会发现它很有用。https:/www.microsoft.comen-usdownloaddetails.aspx?id=30425
顺便说一下,我认为你在这里描述的是一个。标题. 在Word中,a 头部 是指页面顶部的区域(以 页脚 (在底部)]