OpenXML将段落样式(Heading1,Heading2,Head 3等)添加到文字处理文档中 [英] OpenXML Add paragraph style (Heading1,Heading2, Head 3 Etc) to a word processing document

查看:532
本文介绍了OpenXML将段落样式(Heading1,Heading2,Head 3等)添加到文字处理文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指导我如何使用开放XML字处理在段落上添加预定义样式吗?我已经尝试了各种论坛上可用的解决方案,但对我来说没有任何用.这是我要完成的事情:

Can anybody guide me how to add predefined styles on paragraph using open XML Word Processing? I have tried various solutions available on forums but nothing works for me. Here is what i want to accomplish:

                // 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".您必须添加它.因此,您有两种选择:

Your technique would totally work if you were editing an existing document. The problem is that a fresh document doesn't have a "Heading 1" predefined. You'd have to add it. So you have two choices:

1.使用现有的模板文档

创建一个用作基础的模板文档(TemplatePath).在代码中,将其复制到最终目标(FinalPath),然后使用样式向其中添加文本/其他内容.标题1已经在模板中.

Create a template document (TemplatePath) to use as a base. In the code, copy it to the final destination (FinalPath) and add text/whatever to it, applying styles. Heading 1 will already be in the template.

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"并将其应用于您的段落.

If you do this, it will have no built-in styles. So create a style, call it "Heading 1" and apply it to your paragraph.

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());

< sarcasm>看到了吗? OpenXML真是小菜一碟!</sarcasm>我发誓,我的眼睛转得如此之快,我头疼.

<sarcasm>See? OpenXML is a piece of cake!</sarcasm> I swear, my eyes are rolling so hard I'm getting a headache.

这篇关于OpenXML将段落样式(Heading1,Heading2,Head 3等)添加到文字处理文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆