在Novacode中为Docx添加样式

8
使用 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中将其设置为标题样式并保存,则标题样式会出现在styles Xml文件中。

那么我该如何让DocX包含标题样式,或者如何添加一个样式到DocX样式中呢?

4个回答

6
我也遇到了这个问题。通过比较docx(压缩)文件中的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" /> 

你应该将 Paragraph.StyleName 分配为 "a9" 而不是"mystyle",以获得正确的样式。
希望有所帮助。

2

我也遇到了相同的问题,但我通过以下方式解决:

  1. create a .dotx Word template, and fill it with text/words in the styles you want to use (e.g. Title in the style 'Title', Heading 1 in 'style 'Heading 1', Heading 1 in 'style 'Heading 1', etc.

  2. in VS, create a Novacode.DocX object and apply the .dotx as a template:

        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");
            }
    
        }
    }
    
结果:我的Word应用程序的屏幕截图 然而,新问题是:我必须添加Word应用程序本地语言(荷兰语)中的样式,以便使用该文档。例如,“Kop1”相当于“Heading1”,“Titel”是“Title”等。如果使用“Heading1”或“Title”,则会出现错误。但是,由于您控制.dotx文档,这个问题是可以解决的...

1

原来这个库中没有实现几种样式。如果你查看_Enumerations.cs文件中的源代码,会发现有一个名为HeadingType的枚举。在这个枚举中,有一些注释说明Title和其他几个样式没有被实现,因为它们与标题不同。看起来注释说你可以自己尝试实现,但这需要在你的项目中包含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();
    }
}

0

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


我有一个类似的问题,但似乎无法给表格附加样式,有什么想法吗? - Simon Price
我是这样做的,但是当打开DocX文件时,样式似乎被正确分配,但它们没有应用(换句话说,如果您在样式中设置颜色,它不会出现)。 - johnnyontheweb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接