Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri Jan 31, 2020 3:42 pm

Hello
I tested v6.12 and v8.1.10 versions of spire.doc

My bookmark encloses the paragraph mark at the end of line, and DeleteBookmarkContent removes text, shapes, tables, even other eventually paragraphs contained in bookmark but not the first one. It only removes it's content.

And I cannot find the way to simply delete this last paragraph

I have tried this way :
TextBodyPart part = nav.GetBookmarkContent();
Paragraph paragraph = part.BodyItems[0] as Paragraph;
But I do not know how to remove this paragraph, because I only know : section.Paragraphs.Remove(xxx)
And I cannot find the section of the remaining paragraph.

Code: Select all
    private void CleanUpBodyPart(BookmarksNavigator nav, string bookmarkName)
    {
      nav.MoveToBookmark(bookmarkName);
      nav.DeleteBookmarkContent(false);
      nav.Document.Bookmarks.Remove(nav.CurrentBookmark);
    }

I have attached a document with which I did the test.
The bookmark name is "CircleModel"

danielhalte1
 
Posts: 23
Joined: Sat Dec 14, 2019 12:50 am

Mon Feb 03, 2020 6:15 am

Hello,

Sorry for the late reply as weekend.
The DeleteBookmarkContent method will just remove the content in the bookmark but can't remove the owner paragraph. Please refer to the following code to remove the corresponding paragraph. Any question, just feel free to contact us.
Code: Select all
    static void Main(string[] args)
    {
        Document doc = new Document();
        doc.LoadFromFile("Heading1.docx");
        BookmarksNavigator navigator = new BookmarksNavigator(doc);
        CleanUpBodyPart(navigator, "CircleModel");
        doc.UpdateTableOfContents();
        doc.SaveToFile("result.docx", FileFormat.Docx);
    }

    private static void CleanUpBodyPart(BookmarksNavigator nav, string bookmarkName)
    {
        nav.MoveToBookmark(bookmarkName);
        BookmarkStart start = nav.CurrentBookmark.BookmarkStart;
        Paragraph paragraph = start.OwnerParagraph;         
        Section s = paragraph.OwnerTextBody.Owner as Section;
        nav.DeleteBookmarkContent(false);
        nav.Document.Bookmarks.Remove(nav.CurrentBookmark);
        s.Paragraphs.Remove(paragraph);
    }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Feb 03, 2020 8:29 am

Thanks for your solution.
This shows me how to find the paragraph section.
That was what i was missing.

Question : can I mark this topic as resolved ?

danielhalte1
 
Posts: 23
Joined: Sat Dec 14, 2019 12:50 am

Mon Feb 03, 2020 9:53 am

Hello,

Glad to hear that it helped you. And sorry at present our forum doesn't support marking the topic as resolved.
If there is anything we can do for you, just feel free to contact us.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc