none
Bookmark whole text between HEADINGS RRS feed

  • Question

  • See below code, which will create bookmark where style is HEADING1. How can we modify this so that text between HEADING1 and next HEADING1 element gets selected under BOOKMARK1.

    So Bookmark should include text starting from HEADING1... and should end before HEADING2 starts

      using (var doc = WordprocessingDocument.Open(@"D:\DocFiles\Scan.docx", true))
                    {
                        paragraphs = doc.MainDocumentPart.Document.Body
                                        .OfType<Paragraph>().ToList();
                        int bookmarkId = 0;
                        // MyDocuments.Body is a WordProcessDocument.MainDocumentPart.Document.Body
                        foreach (Paragraph para in paragraphs.ToList())
                        {
                            // 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 = "BookmarkbyLokesh" + bookmarkId, Id = bookmarkId.ToString() };
                        BookmarkEnd bme = new BookmarkEnd() { Id = bookmarkId.ToString() };
                        
                        ++bookmarkId;
    
                        // Insertion of bookmarkstart after the paragraphProperties
                        pPr.InsertAfterSelf(bms);
                        bms.InsertAfterSelf(bme);
    
                        // 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
    
                    }
                    doc.Save();
                    doc.Close();

    Friday, September 7, 2018 4:23 PM

All replies

  • Hi Lokesh,

    If you want to get whole text between headings via bookmark, your reading should be guided by Table of Contents first.

    Since in table, you can put bookmarks as anchor and use hyperlink in head line of the table to visit these anchors.

    For more information, please review the following link:

    Reading text part under Heading 1, Heading 2, Heading 3.... of a word document using OpenXml sdk

    BookmarkStart Class

    Hopefully it helps you. Looking forward to hearing from you.

    Best Regards,

    Yuki


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Monday, September 10, 2018 9:14 AM
    Moderator
  • Hi Lokesh,

    Thanks for your asking. Please remember to mark the replies(Include your solution) as answers if they helped and please help us close the thread.

    Thank you for understanding. If you have any question, or update, please feel free to let us know.
    I wish you a happy life!

    Best Regards,

    Yuki


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Thursday, September 13, 2018 2:26 AM
    Moderator