在Novacode中添加样式到Docx

时间:2015-03-27 10:11:19

标签: c# docx

使用Novacode的DocX,我可以添加一个带有标题1样式的段落:

var p = docX.InsertParagraph("My Heading");
p.StyleName = "Heading1";

但我无法添加"标题"式:

var p = docX.InsertParagraph("My Heading");
p.StyleName = "Title";

我查看了生成的Xml文件,我看到"标题"样式不在Styles.xml文件中,而如果我在Word中将其设置为标题样式并保存,则标题样式将出现在样式Xml文件中。

那么我如何让DocX包含Title样式,或者如何在DocX样式中添加样式?

4 个答案:

答案 0 :(得分:4)

我也遇到了这个问题。通过比较docx(zipped)文件中的word \ document.xml和word \ styles.xml文件。我发现应该为Paragraph.StyleName分配样式id而不是样式名称!

例如: styles.xml中的一种样式:

<w:style w:type="paragraph" w:customStyle="1" w:styleId="a9">
    <w:name w:val="mystyle" /> 

您应该将“ a9 ”而不是“ mystyle ”分配给Paragraph.StyleName以获得正确的样式。

希望有所帮助。

答案 1 :(得分:2)

我遇到了同样的问题。我可以解决它如下:

  1. 创建一个 .dotx Word 模板,并在您要使用的样式中添加文字/单词(例如标题中的标题&#39;标题&#39;,标题1&#39; style&#39;标题1&#39;,标题1&#39;样式&#39;标题1&#39;等

  2. 在VS中
  3. 创建一个Novacode.DocX对象并将.dotx应用为模板:

        static void Main(string[] args)
        {
        // Insert a paragrpah:
        string Title = "Hello World!";
        string Header1 = "Countries in Europe";
        string Header2 = "Belgium";
        string Header3 = "France";
        string Para1 = "Belgium, officially the Kingdom of Belgium, is a sovereign state in Western Europe bordered by France, the Netherlands, Germany, Luxembourg, and the North Sea. It is a small, densely populated country which covers an area of 30,528 square kilometres (11,787 sq mi) and has a population of about 11 million people. Straddling the cultural boundary between Germanic and Latin Europe, Belgium is home to two main linguistic groups: the Dutch-speaking, mostly Flemish community, which constitutes about 59% of the population, and the French-speaking, mostly Walloon population, which comprises 41% of all Belgians. Additionally, there is a small group of German-speakers who live in the East Cantons located around the High Fens area, and bordering Germany.";
        string Para2 = "France, is a country with territory in western Europe and several overseas regions and territories. The European, or metropolitan, area of France extends from the Mediterranean Sea to the English Channel and the North Sea, and from the Rhine to the Atlantic Ocean. Overseas France include French Guiana on the South American continent and several island territories in the Atlantic, Pacific and Indian oceans. France spans 643,801 square kilometres (248,573 sq mi) and had a total population of almost 67 million people as of January 2017. It is a unitary semi-presidential republic with the capital in Paris, the country's largest city and main cultural and commercial centre. Other major urban centres include Marseille[XVI], Lyon, Lille, Nice, Toulouse and Bordeaux.";
    
        using (MemoryStream docStream = new MemoryStream())
        {
            using (Novacode.DocX doc = Novacode.DocX.Create(docStream, Novacode.DocumentTypes.Document))
            {
                // Build the document 
                // apply template
                doc.ApplyTemplate(@"C:\tmp\wordTemplate.dotx", false);
                // insert text with styles
                doc.InsertParagraph("Hello World", false).StyleName = "Titel";
                doc.InsertParagraph(Header1, false).StyleName = "Kop1";//dutch for Heading1
                doc.InsertParagraph(Header2, false).StyleName = "Kop2";//dutch for Heading2
                doc.InsertParagraph(Para1, false).StyleName = "Standaard";//dutch for 'Standard', style 'Normal' in an English Word version
                doc.InsertParagraph(Header3, false).StyleName = "Kop2";
                doc.InsertParagraph(Para2, false).StyleName = "Standaard";
    
                // Same the doc to MemoryStream
                doc.SaveAs(@"C:\tmp\ExampleDoc.docx");
            }
    
        }
    }
    
  4. 结果:Screenshot of my word application

    然而,新问题:我不得不在Word应用程序的本机语言中添加样式,我希望将文档用于(荷兰语)。 &#39; Kop1&#39;相当于&#39; Heading1&#39;,&#39; Titel&#39;是&#39;标题&#39;等。当我使用&#39; Heading1&#39;或者&#39;标题&#39;,这会导致错误。 但是,由于您可以控制.dotx文档,这可能是一个可解决的问题......

答案 2 :(得分:1)

转而我们有几个未在此lib中实现的样式。如果查看_Enumerations.cs文件中的源代码,则会出现一个名为HeadingType的枚举。在这个枚举中有一些注释标题和其他几个没有实现,因为它们与标题不同。看起来这个说明你可以尝试自己,但这需要在项目中包含自定义构建的docx。

我建议您找出哪些字体特征并手动设置或在配置文件中设置。简单的例子。

static void AddTitle()
{
    Console.WriteLine("\tAddTitle()");
    using (var document = DocX.Create(@"docs\Title.docx"))
    {
        var title = document.InsertParagraph("this should be a title");
        title.FontSize(28).Font(new FontFamily("Calibri Light"));
        document.InsertParagraph("title is above!");
        document.Save();
    }
}

答案 3 :(得分:0)

我有类似的问题。我通过手动设置docx中的样式(通过STRG + SHIFT + ALT + S在Word中打开对话框)解决了这个问题,并将此docx文件用作一种模板。这样,您可以使用Paragraph.StyleName属性和连接的样式。