首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenXML在文字处理文档中添加段落样式(Heading1、Heading2、Head 3等)

OpenXML在文字处理文档中添加段落样式(Heading1、Heading2、Head 3等)
EN

Stack Overflow用户
提问于 2013-07-29 11:24:11
回答 2查看 13.7K关注 0票数 9

有人能指导我如何使用开放的XML文字处理在段落中添加预定义的样式吗?我已经尝试了各种解决方案,在论坛上,但没有任何对我有用。以下是我想要完成的任务:

代码语言:javascript
复制
// 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" };
EN

回答 2

Stack Overflow用户

发布于 2013-11-25 16:22:35

如果您正在编辑一个现有的文档,您的技术将完全有效。问题是新文档没有预先定义的“标题1”。你得把它加进去。所以你有两个选择:

1.使用现有的模板文档

创建用作基础的模板文档(TemplatePath)。在代码中,将其复制到最终目标(FinalPath),并向其添加文本/任何内容,并应用样式。标题1将已在模板中。

代码语言:javascript
复制
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”,并将其应用于您的段落。

代码语言:javascript
复制
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真是小菜一碟!我发誓,我的眼睛滚动得太厉害了,我头疼。

票数 15
EN

Stack Overflow用户

发布于 2014-09-11 11:38:54

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

标题1

代码语言:javascript
复制
in English is: "Heading 1"
in Hungarien is: "Címsor 1"  -> style id: "Cmsor1"

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

步骤:

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

样式层次结构非常重要。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17922763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档