OpenXML Add paragraph style (Heading1,Heading2, Head 3 Etc) to a word processing document

不打扰是莪最后的温柔 提交于 2019-11-30 09:41:15

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. Work with an existing template document

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. Create your new document from scratch

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

(Sry, my english)

I think the style names is depends your language, what use your word.

Heading 1 in english style id: "Heading 1" in Hungarien: "Címsor 1" -> stlye id: "Cmsor1"

I saw that, the docx xml style file.

How i solove this:

  1. "sample.docx" rename "sample.rar"
  2. Open the "sample.rar" with winrar.
  3. Open "word" folder.
  4. Open "style.xml" file.
  5. And search the style name or properties, what you need.

The style hierarchy is very important!

It's work for me table style also.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!