c# - OpenXML 将段落样式(Heading1、Heading2、Head 3 等)添加到文字处理文档

标签 c# openxml-sdk word-automation

谁能指导我如何使用开放式 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<ParagraphProperties>().Count() == 0)
    para.PrependChild<ParagraphProperties>(new ParagraphProperties());

// Get the ParagraphProperties element of the paragraph.
ParagraphProperties pPr = para.Elements<ParagraphProperties>().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<ParagraphProperties>(new ParagraphProperties());

<讽刺>看到了吗? OpenXML 简直是小菜一碟!我发誓,我的眼珠转动得厉害,我都头疼了。

关于c# - OpenXML 将段落样式(Heading1、Heading2、Head 3 等)添加到文字处理文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17922763/

相关文章:

c# - <T> 找不到通过 Activator 创建的对象类型

c# - OpenXML - 获取图像替代文本标题

c# - openxml spreadsheat 另存为

c# - 从 C# 设置 Word 2010 文档页边距

c# - NotificationHub 在注册标签 UWP 中发送通知

c# - WCF 服务不喜欢单引号

c# - Microsoft Word 互操作自动化 FilePrintSetup 错误

javascript - 为什么我的 js 脚本在从文件引用时无法加载,但在本地使用时却可以工作?

excel - 打开 XML SDK - 保存模板文件(.xltx 到 .xlsx)