OpenXML将段落样式(Heading1,Heading2,Head 3 Etc)添加到文字处理文档中
任何人都可以指导我如何使用开放式XML文字处理在段落上添加预定义样式吗? 我在论坛上尝试了各种解决方案,但对我来说没什么用。 这是我想要完成的:
// Create a document by supplying the filepath. WordprocessingDocument wordDocument = WordprocessingDocument.Create("E:/Test/Executive.Docx", WordprocessingDocumentType.Document); // Add a main document part. MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); // Create the document structure and add some text. mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); run.AppendChild(new Text("Executive Summary")); if (para.Elements().Count() == 0) para.PrependChild(new ParagraphProperties()); // Get the ParagraphProperties element of the paragraph. ParagraphProperties pPr = para.Elements().First(); // Set the value of ParagraphStyleId to "Heading3". pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "Heading1" };
如果您正在编辑现有文档,您的技术将完全有效。 问题是新文档没有预定义的“标题1”。 你必须添加它。 所以你有两个选择:
1.使用现有模板文档
创建模板文档(TemplatePath)以用作基础。 在代码中,将其复制到最终目标(FinalPath)并添加文本/任何内容,应用样式。 标题1已经在模板中。
if (File.Exists(FinalPath)) File.Delete(FinalPath); File.Copy(TemplatePath, FinalPath); WordprocessingDocument wordDocument = WordprocessingDocument.Open(FinalPath, true); Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); run.AppendChild(new Text("Executive Summary")); para.ParagraphProperties = new ParagraphProperties(new ParagraphStyleId() { Val="Heading1" });
2.从头开始创建新文档
如果这样做,它将没有内置样式。 因此,创建一个样式,称之为“标题1”并将其应用于您的段落。
WordprocessingDocument wordDocument = WordprocessingDocument.Create(FinalPath, WordprocessingDocumentType.Document); MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); run.AppendChild(new Text("Executive Summary")); StyleDefinitionPart styleDefinitionsPart = wordDocument.AddStylesDefinitionPart(); Styles styles = styleDefinitionsPart.Styles; Style style = new Style() { Type = StyleValues.Paragraph, StyleId = styleid, CustomStyle = true }; StyleName styleName1 = new StyleName() { Val = "Heading1" }; style.Append(styleName1); StyleRunProperties styleRunProperties1 = new StyleRunProperties(); styleRunProperties1.Append(new Bold); styleRunProperties1.Append(new Italic()); styleRunProperties1.Append(new RunFonts() { Ascii = "Lucida Console" };); styleRunProperties1.Append(new FontSize() { Val = "24" }); // Sizes are in half-points. Oy! style.Append(styleRunProperties1); styles.Append(style); pPr.ParagraphStyleId = new ParagraphStyleId(){ Val = "Heading1" }; para.PrependChild(new ParagraphProperties());
<讽刺>查看? OpenXML是小菜一碟!我发誓,我的眼睛滚得很厉害我头疼。
(Sry,我的英文)
我认为样式名称取决于您的语言,使用您的单词。
英文风格的标题为1:Hungarien中的“标题1”:“Címsor1” – > stlye id:“Cmsor1”
我看到了,docx xml样式文件。
我怎么解决这个问题:
- “sample.docx”重命名为“sample.rar”
- 用winrar打开“sample.rar”。
- 打开“word”文件夹。
- 打开“style.xml”文件。
- 并搜索样式名称或属性,您需要什么。
样式层次非常重要!
这也适用于我的桌子风格。
上述就是C#学习教程:OpenXML将段落样式(Heading1,Heading2,Head 3 Etc)添加到文字处理文档中分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1016361.html