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.

Wed Jul 03, 2019 6:56 am

Hi,

Is it possible to add image data into a bookmark content using ReplaceBookmarkContent method?

kaimon
 
Posts: 16
Joined: Fri May 17, 2019 4:04 am

Wed Jul 03, 2019 8:55 am

Hi,

Thanks for your inquiry.
Please refer to following code:
Code: Select all
            Document document = new Document(FilePath + "Bookmark.docx");
            Section section = document.AddSection();

            //create a temp section to add image.
            Section tempSection = document.AddSection();
            tempSection.AddParagraph().AppendPicture(Image.FromFile(@"E:\image\image1.jpg"));

            ParagraphBase replacementFirstItem = tempSection.Paragraphs[0].Items.FirstItem as ParagraphBase;
            ParagraphBase replacementLastItem = tempSection.Paragraphs[tempSection.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
            TextBodySelection selection = new TextBodySelection(replacementFirstItem, replacementLastItem);
            TextBodyPart part = new TextBodyPart(selection);

            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
            //locate the bookmark
            bookmarkNavigator.MoveToBookmark("bookmark3");
            //replace the content of bookmark
            bookmarkNavigator.ReplaceBookmarkContent(part);
            //remove temp section
            document.Sections.Remove(tempSection);
            document.SaveToFile(@"18097.docx", FileFormat.Docx);

If this doesn't meed your requirement, please provide your input file, images and the desired file. Then I will look into it and provide corresponding code for you.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Aug 14, 2019 5:43 am

Hi,

Greetings from E-iceblue.
Is the code I provided helpful for you? Has your issue been resolved?

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.Doc