簡體   English   中英

在Novacode中添加樣式到Docx

[英]Add style to Docx in Novacode

使用Novacode的DocX,我可以添加一個帶有Heading 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樣式中添加樣式?

我也遇到了這個問題。 通過比較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. 創建一個.dotx Word模板,並在其中填寫您想要使用的樣式中的文本/單詞(例如,標題為'標題',標題為1'樣式'標題1',標題1為'樣式'標題1'等

  2. 在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文檔,這可能是一個可解決的問題......

轉而我們有幾個未在此庫中實現的樣式。 如果查看_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();
    }
}

我遇到了類似的問題。 我通過手動設置docx中的樣式(通過STRG + SHIFT + ALT + S在Word中打開對話框)解決了這個問題,並將此docx文件用作一種模板。 這樣,您可以使用Paragraph.StyleName屬性和連接的樣式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM