2016-06-27 4 views

Antwort

1

Um dies zu tun:

  1. alle
  2. prüfen Absatz finden, wenn der Absatz ein Titel mit Heading1 ist (Sie haben die Existenz eines paragraphStyleId() innerhalb eines ParagraphProperties zu überprüfen (.. .)
  3. Wenn es ein Absatz mit Heading1 Stil ist, Ihre Lesezeichen einfügen.

Sie sollen wie

dies mit einem Code in der Lage zu tun
 int bookmarkId = 0; 
     // MyDocuments.Body is a WordProcessDocument.MainDocumentPart.Document.Body 
     foreach(Paragraph para in MyDocuments.Body.Descendants<Paragraph>()) 
     { 
      // if the paragraph has no properties or has properties but no pStyle, it's not a "Heading1" 
      ParagraphProperties pPr = para.GetFirstChild<ParagraphProperties>(); 
      if (pPr == null || pPr.GetFirstChild<ParagraphStyleId>() == null) continue; 
      // if the value of the pStyle is not Heading1 => skip the paragraph 
      if (pPr.GetFirstChild<ParagraphStyleId>().Val != "Heading1") continue; 

      // it's a paragraph with Heading1 style, insert the bookmark 

      // the bookmark must have a start and an end 
      // the bookmarkstart/end share the same id 
      BookmarkStart bms = new BookmarkStart() { Name = "yourbookmarkname", Id = bookmarkId.ToString() }; 
      BookmarkEnd bme = new BookmarkEnd() { Id = bookmarkId.ToString() }; 
      ++bookmarkId; 

      // Insertion of bookmarkstart after the paragraphProperties 
      pPr.InsertAfterSelf(bms); 

      // The bookmarkend can be inserted after the bookmarkstart or after the object the bookmark must surrounding 
      // here we will insert it after bms. If you want to surround an object, find the object within the paragraph and insert it after 
      bms.InsertAfterSelf(bme); 

Weitere Informationen zu den Lesezeichen Klassen hier: https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.bookmarkstart(v=office.14).aspx

+0

Dank Maxime Porte. – MemoryLeak

Verwandte Themen