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 Apr 26, 2017 1:06 am

Hi There,
I have followed your article that describes how to insert an image at a book mark and I have that working.
"How-to-insert-an-image-at-bookmark-in-word-documents.html"
What I want to do is replace the book mark with the image. I have the bookmark inside a boarder and if I add the image after the bookmark it appears outside the boarder.

This is what I have from the example
Code: Select all
               bookmarkNavigator.MoveToBookmark(Properties.Settings.Default.BookMark1);
                Image image = Image.FromFile(textBoxImageFileName.Text);
                //Add a section and name it section0
                Section section0 = document.AddSection();
                //Add a paragraph for section0
                Paragraph paragraph = section0.AddParagraph();
                //Add a picture into paragraph
                DocPicture picture = paragraph.AppendPicture(image);
                //Add paragraph into docuemnt
                bookmarkNavigator.InsertParagraph(paragraph);
                // Remove Section0
                document.Sections.Remove(section0);


To replace the bookmark with text is quite simple
Code: Select all
                bookmarkNavigator.MoveToBookmark(Properties.Settings.Default.BookMark2);
                bookmarkNavigator.ReplaceBookmarkContent(textBoxSPNumber.Text, true);


How Can I replace the bookmark with an image or other wise get my image to land inside the boarder?

Thanks
David

dpollard
 
Posts: 5
Joined: Tue Apr 11, 2017 7:05 am

Wed Apr 26, 2017 2:26 am

Dear David,

Thanks for your inquiry.
Sorry that I didn't know what "boarder" was. I created a sample file which had a table with a bookmark inside a cell and then inserted an image into bookmark using your code. It worked fine, and the image was inside table cell.
Could you please provide us a sample file for investigation ? Then we will provide you with the corresponding code.

Thanks,
Betsy
E-iceblue support team
User avatar

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

Wed Apr 26, 2017 3:59 am

Hi Betsy,
I should have explained more about the boarder. I didn't create this word template and I also assumed it was a single cell table.
On closer inspection I see it is a boarder on a paragraph. I have attached a cut down example of the template I am using plus the result I'm getting.

If there is no easy solution I'll try changing my template to try and use a single cell table.

(The boss has been using this template as it is for years with Access 97 to do the automation)

Thanks
David

dpollard
 
Posts: 5
Joined: Tue Apr 11, 2017 7:05 am

Wed Apr 26, 2017 8:03 am

Dear David,

Thanks for the information.
I checked the sample file you provided and knew the "boarder". And found the reason is that new paragraph is inserted in the original paragraph which has the bookmark. You only need to insert the image inside the original paragraph. Please change your code as below.
Code: Select all
            BookmarksNavigator bn = new BookmarksNavigator(document);
            bn.MoveToBookmark("Photo", true, true);
            Image image = Image.FromFile(@"F:\image\image1.jpg");
            IParagraphBase pb = bn.InsertParagraphItem(ParagraphItemType.Picture);
            pb.OwnerParagraph.AppendPicture(image);

In addition, I found the result PDF had a little issue that the image covered the bottom border. I have posted it to our Dev team, once there is any progress, we will let you know.

Sincerely,
Betsy
E-iceblue support team
User avatar

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

Thu Apr 27, 2017 3:47 am

Hi Betsy,
Thanks for the update. I'll give this code a try shortly.
One thing I have been wondering is can I set the size of the image.
Looking back at the old Access 97 Office integration this didn't need to be done and the image never went over the margins.

Thanks
David

dpollard
 
Posts: 5
Joined: Tue Apr 11, 2017 7:05 am

Thu Apr 27, 2017 5:28 am

Hi Betsy,
The code you supplied works and now my image comes out within the paragraph boarder.
My output document has no footer and it seems to be a long way from the bottom of the page so I think that isn't going to be an issue for me at least not in my current implementation.

However the resize picture issue is going to be a problem.
Looking back at some documents my users have created using our old application I notice the pictures vary in size when I copy and past them into a picture viewer. Yet they look the same size in word after the old applications has generated the document.

If I insert these same two pictures into word using your tools one fits within the margins and the other one does not.
i.e. 470 x 313 pixels fits
and 767 x 511 does not fit.

I tried using the following code to set the image size but I received an error that the With and Height are read only properties.

Code: Select all
       Image image = Image.FromFile(textBoxPictureLocation.Text);
                image.Width = 470;
                image.Height = 313;



I then found another example in your help named DocPicture that appears to allow me to set the image size
However I couldn't figure out how to use it with the IParagraphBase code you supplied.

Code: Select all
//Insert Image and Set Its Size
DocPicture Pic = p.AppendPicture(Image.FromFile(@"E:\Work\Documents\SampleImage\Sample.jpg"));
Pic.Width = 750;
Pic.Height = 468;


I feel I nearly have a working solution now. Once I can prove the concept I should have no problem getting my boss to approve the purchase of a license.

Thanks
David

dpollard
 
Posts: 5
Joined: Tue Apr 11, 2017 7:05 am

Thu Apr 27, 2017 5:58 am

Dear David,

Many thanks for the detail.
Please reference the following code to resize the image when using IParagraphBase code I supplied.
Code: Select all
            DocPicture picture = pb.OwnerParagraph.AppendPicture(image);
            picture.Height = 468;
            picture.Width = 750;

Hope this helps. If you still have issues, please let me know.

Sincerely,
Betsy
E-iceblue support team
User avatar

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

Thu Apr 27, 2017 11:32 pm

Hi Betsy,
In the finish my code was surprisingly simple.
I'll post it here in case someone else goes down the same path as me. Perhaps they will come across this post.
It is not 100% clear to me why this combination of objects works but I hope that will become clear over time.
Code: Select all
 Image image = Image.FromFile(textBoxPictureLocation.Text);
 IParagraphBase paragraphBase = bookmarkNavigator.InsertParagraphItem(ParagraphItemType.Picture);
 // Note: Photos must have aspect ratio of 1.5 - 1 (Width = 1.5x the Height)
 DocPicture picture = paragraphBase.OwnerParagraph.AppendPicture(image);
 picture.Width = 412;
 picture.Height = 275;


Thank you very much for your assistance.
David

dpollard
 
Posts: 5
Joined: Tue Apr 11, 2017 7:05 am

Fri Apr 28, 2017 2:00 am

Dear David,

Thanks for sharing the information.
Please feel free to contact us if you have any question. We will be happy to help you.

Sincerely,
Betsy
E-iceblue support team
User avatar

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

Return to Spire.Doc