2012-08-13 10 views
5

Utilizzo di Word Ho creato un Docx con normal.dot standard come test. Ciao livello mondiale complessità.Open XML: Word - Ottenere tutti i paragrafi contrassegnati come stile "Titolo1"

Desidero ottenere all the paragraphs che sono in stile "Heading1" style in Word.

Posso ottenere tutti i paragrafi, ma non so come filtrare fino a Titolo1.

using (var doc = WordprocessingDocument.Open(documentFileName, false)) 
{ 
    paragraphs = doc.MainDocumentPart.Document.Body 
        .OfType<Paragraph>().ToList(); 
} 

risposta

8
[Test] 
    public void FindHeadingParagraphs() 
    { 

     var paragraphs = new List<Paragraph>(); 

     // Open the file read-only since we don't need to change it. 
     using (var wordprocessingDocument = WordprocessingDocument.Open(documentFileName, false)) 
     { 
      paragraphs = wordprocessingDocument.MainDocumentPart.Document.Body 
       .OfType<Paragraph>() 
       .Where(p => p.ParagraphProperties != null && 
          p.ParagraphProperties.ParagraphStyleId != null && 
          p.ParagraphProperties.ParagraphStyleId.Val.Value.Contains("Heading1")).ToList(); 
     } 
    } 
+0

ottengo un'eccezione null, solo aggiunto:! = Null && p.ParagraphProperties.ParagraphStyleId – Kiquenet

Problemi correlati