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 Dec 01, 2017 7:11 pm

I'm attempting to use GetBookmarkContent to get contents of a bookmark from one word document and copy it to another word document. I'm using TextBodyPart which doesn't not seem to retain the formatting of the word document within the bookmark. Is there another method to resolve this?

Document noteDocument = new Document();
noteDocument.LoadFromFile(noteValue);
BookmarksNavigator bmNavigator = new BookmarksNavigator(noteDocument);
bmNavigator.MoveToBookmark("SampleNote");
bmNavigator.ReplaceBookmarkContent();
TextBodyPart textBodyPart = bmNavigator.GetBookmarkContent( );

textBodyPart does not retain formatting. In addition, how do I get the part into another word document?

Thank You!

tholland
 
Posts: 22
Joined: Fri Dec 01, 2017 6:38 pm

Mon Dec 04, 2017 3:31 am

Hello,

After an initial test with the latest version Spire.Doc Pack(hot fix) Version:6.0.63 using the following code, I can successfully copy the bookmark content with the formatting to another document. Please see below.
Code: Select all
            Document doc = new Document(path);
            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
            bookmarkNavigator.MoveToBookmark("SampleNote");
            TextBodyPart part = bookmarkNavigator.GetBookmarkContent();

            //Load another document and get the last paragraph
            Document doc1 = new Document(@"sample1.docx");
            Paragraph para = doc1.Sections[0].Body.LastParagraph as Paragraph;

            foreach (var items in part.BodyItems)
            {
                if (items is Paragraph)
                {
                    foreach (var item in (items as Paragraph).ChildObjects)
                    {
                       //append the items in the bookmark content to the new docuemnt
                        //and retain the format
                        if (item is TextRange)
                        {
                            para.ChildObjects.Add((item as TextRange).Clone());
                        }

                    }
                }
            }
            doc1.SaveToFile("12313.docx", FileFormat.Docx);


If there's still any issue, please write back and share your sample document for a better invesigation.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Dec 07, 2017 9:47 am

Hello tholland,

Has the issue been resolved?
Your feedback would be greatly appreciated.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Dec 15, 2017 1:35 pm

Yes! Thank you very much!

tholland
 
Posts: 22
Joined: Fri Dec 01, 2017 6:38 pm

Mon Dec 18, 2017 1:39 am

Hello,

Thanks for your reply.
Please feel free to contact us if you need any help.

Sincerely,
Jane
E-iceblue spport team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc