8

谁能指导我如何使用开放的 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" };
4

2 回答 2

12

如果您正在编辑现有文档,您的技术将完全有效。问题是新文档没有预定义“标题 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 简直是小菜一碟!</sarcasm> 我发誓,我的眼睛都在转动,让我头疼。

于 2013-11-25T16:22:35.380 回答
0

我认为样式名称取决于您的 Microsoft Office 语言,例如:

标题 1

in English is: "Heading 1"
in Hungarien is: "Címsor 1"  -> style id: "Cmsor1"

通过查看您的 Word 文档 xml 样式文件,您将能够找到上述信息。

脚步:

  1. 重命名任何word文档,例如“sample.docx”为“sample.zip”
  2. 打开“sample.zip”。
  3. 打开 zip 文件中的“word”文件夹。
  4. 打开“style.xml”文件。
  5. 搜索您要查找的样式名称或属性。

风格层次非常重要。

于 2014-09-11T11:38:54.720 回答