2013-07-29 6 views
5

Qualcuno può guidarmi come aggiungere stili predefiniti sul paragrafo utilizzando l'Elaborazione testo XML aperta? Ho provato varie soluzioni disponibili nei forum ma nulla funziona per me. Ecco cosa voglio ottenere:OpenXML Aggiunge lo stile di paragrafo (Titolo1, Titolo2, Testa 3 ecc.) Ad un documento di elaborazione testo

   // Create a document by supplying the filepath. 
       WordprocessingDocument wordDocument = WordprocessingDocument.Create("E:/Test/Executive.Docx", WordprocessingDocumentType.Document); 

       // Add a main document part. 
       MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); 

       // Create the document structure and add some text. 
       mainPart.Document = new Document(); 
       Body body = mainPart.Document.AppendChild(new Body()); 
       Paragraph para = body.AppendChild(new Paragraph()); 

       Run run = para.AppendChild(new Run()); 
       run.AppendChild(new Text("Executive Summary")); 
       if (para.Elements<ParagraphProperties>().Count() == 0) 
        para.PrependChild<ParagraphProperties>(new ParagraphProperties()); 

       // Get the ParagraphProperties element of the paragraph. 
       ParagraphProperties pPr = para.Elements<ParagraphProperties>().First(); 

       // Set the value of ParagraphStyleId to "Heading3". 
       pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "Heading1" }; 

risposta

7

La tua tecnica funzionerebbe totalmente se stessi modificando un documento esistente. Il problema è che un nuovo documento non ha un "Titolo 1" predefinito. Dovresti aggiungerlo. Quindi, si hanno due scelte:

1. Lavorare con un documento di modello esistente

Creare un modello di documento (TemplatePath) da utilizzare come base. Nel codice, copialo nella destinazione finale (FinalPath) e aggiungi testo/qualsiasi cosa ad esso, applicando gli stili. L'intestazione 1 sarà già nel modello.

if (File.Exists(FinalPath)) 
    File.Delete(FinalPath); 
File.Copy(TemplatePath, FinalPath); 
WordprocessingDocument wordDocument = WordprocessingDocument.Open(FinalPath, true); 
Paragraph para = body.AppendChild(new Paragraph()); 
Run run = para.AppendChild(new Run()); 
run.AppendChild(new Text("Executive Summary")); 
para.ParagraphProperties = new ParagraphProperties(new ParagraphStyleId() { Val="Heading1" }); 

2. Creare il nuovo documento da zero

Se si esegue questa operazione, non avrà alcun stili incorporati. Quindi crea uno stile, chiamalo "Titolo 1" e applicalo al tuo paragrafo.

WordprocessingDocument wordDocument = WordprocessingDocument.Create(FinalPath, WordprocessingDocumentType.Document); 
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); 
mainPart.Document = new Document(); 
Body body = mainPart.Document.AppendChild(new Body()); 
Paragraph para = body.AppendChild(new Paragraph()); 
Run run = para.AppendChild(new Run()); 
run.AppendChild(new Text("Executive Summary")); 
StyleDefinitionPart styleDefinitionsPart = wordDocument.AddStylesDefinitionPart(); 
Styles styles = styleDefinitionsPart.Styles; 
Style style = new Style() { 
    Type = StyleValues.Paragraph, 
    StyleId = styleid, 
    CustomStyle = true 
}; 
StyleName styleName1 = new StyleName() { Val = "Heading1" }; 
style.Append(styleName1); 
StyleRunProperties styleRunProperties1 = new StyleRunProperties(); 
styleRunProperties1.Append(new Bold); 
styleRunProperties1.Append(new Italic()); 
styleRunProperties1.Append(new RunFonts() { Ascii = "Lucida Console" };); 
styleRunProperties1.Append(new FontSize() { Val = "24" }); // Sizes are in half-points. Oy! 
style.Append(styleRunProperties1); 
styles.Append(style); 
pPr.ParagraphStyleId = new ParagraphStyleId(){ Val = "Heading1" }; 
para.PrependChild<ParagraphProperties>(new ParagraphProperties()); 

<sarcasmo> Vedi? OpenXML è un gioco da ragazzi! </sarcasmo > Giuro, i miei occhi stanno rotolando così forte che mi viene il mal di testa.

+1

StyleDefinitionPart deve essere StyleDefinitionsPart –

0

(Sry, il mio inglese)

Credo che i nomi degli stili è dipende la vostra lingua, che cosa usa la tua parola.

Titolo 1 in inglese stile id: "Intestazione 1" in Hungarien: "Címsor 1" -> stlye id: "Cmsor1"

ho visto che, il file docx xml stile.

Come i Solove questo:

  1. "sample.docx" rinomina "sample.rar"
  2. Aprire il "sample.rar" con winrar.
  3. Aprire la cartella "word".
  4. Aprire il file "style.xml".
  5. E cercare il nome dello stile o proprietà, ciò che è necessario.

La gerarchia di stile è molto importante!

Anche per me è un lavoro da tavolo.

Problemi correlati