简体   繁体   English

使用C#创建带有标题的Word文档

[英]Create word document with headings using c#

I want to create an word documents with headings for navigation. 我想创建带有导航标题的Word文档。 In my c# code I created a word document and added a paragraph to it. 在我的C#代码中,我创建了一个word文档,并在其中添加了一个段落。 I set the style of the added paragraph by this method: 我通过这种方法设置添加段落的样式:

paragraph1.Range.set_Style(document.Styles["Heading 1"]);
paragraph.Range.Text="paragraph 1";

The result of that code is a word paragraph that is text font and color are as "Heading 1" style but in the navigation pane of the word document there is no navigation tab. 该代码的结果是一个单词段落,该单词段落的文本字体和颜色均为“标题1”样式,但在Word文档的导航窗格中没有导航选项卡。

the solution was to set paragraph1.Range.Text before setting the style 解决的办法是在设置样式之前先设置paragraph1.Range.Text

paragraph1.Range.Text = "paragraph 1";
object style_name = "Heading 1";
paragraph1.Range.set_Style(ref style_name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM