2015-03-27 17 views
6

Utilizzando DocX di Novacode, posso aggiungere un paragrafo con stile Titolo 1 in questo modo:aggiungere stile alla Docx in Novacode

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

Ma non posso aggiungere un "Titolo" stile:

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

Ho cercato nei file Xml generati e vedo che lo stile "Titolo" non è nel file Styles.xml, mentre se lo imposto come stile Titolo in Word e salvo, lo stile Titolo appare nel file Xml degli stili.

Quindi, come posso far sì che DocX includa lo stile Titolo, o come faccio ad aggiungere uno stile agli stili DocX?

risposta

1

Ci sono diversi stili che non sono implementati in questa libreria. Se si cerca nell'origine nel file _Enumerations.cs, c'è un enum chiamato HeadingType. In questo enum ci sono note che il titolo e molti altri non sono implementati in quanto sono diversi dai titoli. Sembra che la nota dice che puoi provarci, ma che richiederebbe una build personalizzata di docx da includere nel tuo progetto.

Ti suggerisco di scoprire quali sono le caratteristiche dei caratteri e di impostarle manualmente o in un file di configurazione. Il semplice esempio.

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

Ho avuto un problema simile. L'ho risolto impostando manualmente gli stili in un docx (Apri il dialogo tramite STRG + MAIUSC + ALT + S in Word) e ho utilizzato questo file docx come una sorta di modello. In questo modo, puoi usare l'Attributo Paragraph.StyleName e lo stile connesso.

+0

Ho un problema simile ma non riesco a collegare uno stile a un tavolo, qualche idea? –

2

Ho incontrato anche questo problema. Confrontando i file word \ document.xml e word \ styles.xml nel file docx (zippato). Ho trovato che il Paragraph.StyleName dovrebbe essere assegnato con l'id stile non il nome dello stile!

per esempio: uno stile styles.xml:

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

È necessario assegnare "a9" non "mystyle" per Paragraph.StyleName per ottenere lo stile corretto.

Sperare utile.

1

Mi sono imbattuto nello stesso problema. Potrei risolverlo nel modo seguente:

  1. creare un modello didotx Word, e riempirlo con testo/parole gli stili che si desidera utilizzare (ad esempio, titolo in stile 'Titolo', Titolo 1 a 'stile 'Intestazione 1', Intestazione 1 in' stile 'Intestazione 1', ecc

  2. in VS, creare un oggetto Novacode.DocX ​​e applicare il dotx come modello:

    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"); 
         } 
    
        } 
    } 
    

Risultato: Screenshot of my word application

Tuttavia, nuovo problema: ho dovuto aggiungere gli stili nella lingua nativa dell'applicazione Word con cui voglio usare il documento (olandese). 'Kop1' è l'equivalente di 'Titolo1', 'Titolo' è 'Titolo', ecc. Quando uso 'Titolo1' o 'Titolo', questo genera un errore. Ma, dato che hai il controllo del documento .dotx, questo potrebbe essere un problema risolvibile ...

Problemi correlati