Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Novacode中将样式添加到Docx_C#_Docx - Fatal编程技术网

C# 在Novacode中将样式添加到Docx

C# 在Novacode中将样式添加到Docx,c#,docx,C#,Docx,使用Novacode的DocX,我可以添加标题1样式的段落,如下所示: var p = docX.InsertParagraph("My Heading"); p.StyleName = "Heading1"; 但我不能添加“标题”样式: 我查看了生成的Xml文件,发现Styles.Xml文件中没有“Title”样式,而如果在Word和save中将其设置为Title样式,则Title样式将显示在Styles Xml文件中 那么,如何让DocX包含标题样式,或者如何向DocX样式添加样式 有几种

使用Novacode的DocX,我可以添加标题1样式的段落,如下所示:

var p = docX.InsertParagraph("My Heading");
p.StyleName = "Heading1";
但我不能添加“标题”样式:

我查看了生成的Xml文件,发现Styles.Xml文件中没有“Title”样式,而如果在Word和save中将其设置为Title样式,则Title样式将显示在Styles Xml文件中


那么,如何让DocX包含标题样式,或者如何向DocX样式添加样式

有几种样式没有在这个库中实现。如果查看_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();
    }
}

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

我也遇到了这个问题。通过比较docx(压缩)文件中的word\document.xml和word\styles.xml文件。我发现应该使用样式id而不是样式名称来指定段落.StyleName

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

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

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


希望有帮助。

我遇到了同样的问题。我可以用以下方法解决这个问题:

  • 创建一个.dotx Word模板,并用您想要使用的样式(例如,样式“Title”中的标题、“style”标题1中的标题1、“style”标题1中的标题1)中的文本/单词填充模板

  • 在VS中,创建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");
            }
    
        }
    }
    
  • 结果:

    然而,新的问题是:我必须添加我想与文档一起使用的Word应用程序的母语样式(荷兰语)。“Kop1”相当于“Heading1”,“Titel”相当于“Title”,等等。当我使用“Heading1”或“Title”时,这会导致一个错误。
    但是,由于您控制着.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");
            }
    
        }
    }