none
Replace a bookmark with an image RRS feed

  • Question

  • 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.

    Thank you!

    Friday, April 10, 2015 7:07 AM

Answers

  • Then change 

    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);

    • Marked as answer by L.HlModerator Monday, April 20, 2015 9:14 AM
    Friday, April 10, 2015 7:13 AM

All replies

  • Recommend U a third party tool,download  free edition of this word component and add reference to your project,

    Since there is only one image in your document, you can refer to the code to meet your requirement:

    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);

    Friday, April 10, 2015 7:11 AM
  • Recommend U a third party tool,download  free edition of this word component and add reference to your project,

    Since there is only one image in your document, you can refer to the code to meet your requirement:

    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);

    But 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.
    Friday, April 10, 2015 7:12 AM
  • Then change 

    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);

    • Marked as answer by L.HlModerator Monday, April 20, 2015 9:14 AM
    Friday, April 10, 2015 7:13 AM