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.

Sun Feb 08, 2015 6:20 am

Hi
I have an existing document with an image in it (the image is surrounded by a named bookmark). How can I replace the existing image with another? I can navigate to teh bookmark, but how do I replace the image?
The new image is a system.drawing.image that is read from a database.

Forensicare
 
Posts: 22
Joined: Fri Aug 02, 2013 3:29 am

Mon Feb 09, 2015 2:59 am

Dear Forensicare,

Sorry for the delay in posting a reply.

Since there is only one image in your document, you can refer to the code to meet your requirement:
Code: Select all
Document doc = new Document("Sample.docx", FileFormat.Docx2010);
Paragraph p = doc.Sections[0].Paragraphs[0];

DocPicture pic = null;

for (int i = 0; i < p.ChildObjects.Count; i++)
{
    if (p.ChildObjects[i] is DocPicture)
    {
        pic = p.ChildObjects[i] as DocPicture;
        break;
    }
}

//You can replace this line with the image from database
Image img = Image.FromFile("02.png");
p.AppendPicture(img);
p.ChildObjects.Remove(pic);

doc.SaveToFile("Result.docx", FileFormat.Docx2010);
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Mon Feb 09, 2015 3:41 am

Hi
there is more than one image. The one I want to replace is surrounded by a bookmark. Alternatively, can you advise how to insert an image at a particular bookmark without inserting a preceding paragraph return.

Forensicare
 
Posts: 22
Joined: Fri Aug 02, 2013 3:29 am

Mon Feb 09, 2015 3:59 am

Dear Forensicare,

For your requirement, please refer to the code below:
Code: Select all
Document doc = new Document("Sample.docx", FileFormat.Docx2010);

Paragraph p = doc.LastParagraph;
string bookmarkName = "B1";
Image img = Image.FromFile("01.png");
DocPicture pic = null;
int index = 0;

for (int i=0;i<p.ChildObjects.Count;i++)
{
    if (p.ChildObjects[i] is BookmarkStart)
    {
        BookmarkStart bs = p.ChildObjects[i] as BookmarkStart;
        if (bookmarkName == bs.Name)
        {
            pic = new DocPicture(doc);
            pic.LoadImage(img);
            index = i;
            break;
        }
    }
}

p.ChildObjects.Insert(index, pic);

doc.SaveToFile("Result.docx", FileFormat.Docx2010);
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Fri Mar 06, 2015 4:53 am

Hi
This does not work for my situation. I have attached the file. The requirement is to replace the bookmark with an image without inserting a paragraph. The bookmark is called "PHOTO".

Forensicare
 
Posts: 22
Joined: Fri Aug 02, 2013 3:29 am

Fri Mar 06, 2015 8:48 am

Hello Forensicare,

Thanks for your inquiry.

Please refer to the code below to meet your requirement:
Code: Select all
Document doc = new Document();
doc.LoadFromFile("BradmaInpatient.docx");
Bookmark bookmark = doc.Bookmarks["PHOTO"];
int bookmarkIndex = bookmark.BookmarkStart.OwnerParagraph.ChildObjects.IndexOf(bookmark.BookmarkStart);
DocPicture docpic = new DocPicture(doc);
docpic.LoadImage(Image.FromFile("01.png"));
bookmark.BookmarkStart.OwnerParagraph.ChildObjects.Insert(bookmarkIndex, docpic);
doc.Bookmarks.Remove(bookmark);
doc.SaveToFile("SS4384.docx", FileFormat.Docx);
Best Regards,
Burning
E-iceblue Support Team
User avatar

burning.liu
 
Posts: 406
Joined: Mon Aug 04, 2014 6:47 am

Thu Jun 11, 2015 1:48 am

Hi burning.liu
Thank you for this, it did finally work, however I found another bug, I think, in the Replace function with the latest hotfix. In the same file, if the string to replace is less thatn 5 characters in length, the following error occurs when you do the .SaveToFile:
"Length cannot be less than zero. Parameter name: length"
So: this causes the error when you try to save the file:
dim x as string = "XYZ"
doc.Replace("[LASTNAME]", x, false, true)
This does not:
dim x as string = "XYZ"
x = x.PadRight(6)
doc.Replace("[LASTNAME]", x, false, true)

Zero length strings also do not cause the error.

Forensicare
 
Posts: 22
Joined: Fri Aug 02, 2013 3:29 am

Thu Jun 11, 2015 6:19 am

Hello,

Thanks for your inquiry.
I have noticed the issue you mentioned, and I have transferred it to our Dev team, we will let you know as soon as there is any update.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Wed Jun 17, 2015 8:19 am

Hello,

Sorry to keep you waiting.
We just published a new hotfix of Spire.Doc:Spire.Doc Pack(hot fix) Version:5.4.36, in which the issue you mentioned has get resolved. Please download it and have a try.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Fri Jun 19, 2015 9:05 am

Hello,

Have you tried the hot fix ? Has your issue been resolved ? Could you please give us some feedback at your convenience ?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Sat Jan 23, 2016 3:46 am

Hi Betsy
This resolved my issue. Thank you

Forensicare
 
Posts: 22
Joined: Fri Aug 02, 2013 3:29 am

Mon Jan 25, 2016 1:38 am

Hello,

Thanks for your feedback.
Please feel free to contact us if there is any question.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Return to Spire.Doc