Add Style to Docx in Novacode

Using Novacode DocX, I can add a paragraph with a heading of 1 as follows:

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

But I canโ€™t add a โ€œTitleโ€ style:

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

I looked into the generated Xml files and I see that the "Heading" style is not in the Styles.xml file, whereas if I set it as the heading style in Word and saved, then the heading style will appear in the styles of the Xml file.

So, how do I get DocX to include the Title style, or how do I add style to the DocX styles?

+6
source share
4 answers

I also encounter this problem. Comparing the word \ document.xml and word \ styles.xml files in the docx (zipped) file. I found that Paragraph.StyleName should be assigned a style identifier, not a style name!

for example: one style in styles.xml:

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

You must assign " a9 " not " mystyle " in Paragraph.StyleName to get the correct style.

I hope for help.

+2
source

Includes several styles that are not implemented in this folder. If you look in the source in the _Enumerations.cs file, an enumeration called HeadingType appears. There are notes in this listing that Title and several others are not implemented as they differ from headings. It seems that the note says that you can try yourself, but for this you will need to add a custom docx assembly to your project.

I would advise you to find out what characteristics of the font and install them manually or in the configuration file. A simple example.

 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(); } } 
+1
source

I ran into the same problem. I could solve it like this:

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

  • in VS, create a Novacode.DocX โ€‹โ€‹object and apply .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 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"); } } } 

Result: Screenshot of my application word

However, a new problem: I had to add styles in the native language of the Word application, I want to use a document with the Dutch language. "Kop1" is the equivalent of "Heading1", "Titel" - "Title", etc. When I use "Heading1" or "Title", this leads to an error. But, since you are in control of a .dotx document, this may be a solvable problem ...

+1
source

I had a similar problem. I solved this by manually setting the styles in docx (open the dialog via STRG + SHIFT + ALT + S in Word) and used this docx file as a kind of template. That way you can use the Paragraph.StyleName attribute and related style.

0
source

Source: https://habr.com/ru/post/984312/


All Articles