Remove Shape (Image) from a Word document programmatically

Hi,
There is an issue on removing the image from the word document programmatically. Could you please help us?
We have added an image programmatically in the word document. And we need to remove the same image (mapped to the bookmark) programmatically.
So, here is the code for adding an image to the word:

Image ImageForCallout = Image.FromStream(GetImageforCharts(parenturl, loginName));
Document doc = new Document(storageLocation);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("Image1");
builder.InsertImage(ImageForCallout, 80, 50);
doc.Save(storageLocation);

We tried one method (doc.Range.Bookmarks.Remove(“Image1”):wink: to remove the image but it throws error. So, how to remove the image from the word document using Aspose word object programmatically?
Thanks.

@manoj1197

Please set the value of Bookmark.Text property to empty string to remove the bookmark’s contents. The Bookmark.Remove method removes the bookmark from the document. It does not remove text inside the bookmark.

If you face any issue, please share your input document here for testing. We will investigate the issue and provide you more information on this.

Document doc = new Document(MyDir + “Bookmark.doc”);

// Use the indexer of the Bookmarks collection to obtain the desired bookmark.
Bookmark bookmark = doc.Range.Bookmarks["MyBookmark"];
bookmark.Text = "";

// Remove the bookmark. The bookmarked text is not deleted.
bookmark.Remove();

Hi Tahir,

Thanks for your quick response. I tried the logic.

Bookmark bookmark = doc.Range.Bookmarks["MyBookmark"];

In the above line, bookmark throws the null reference error.

Could you please help if I am missing something?

Thanks.

Regards,
Manoj

Hi Manoj,

Thanks for your inquiry. Please use the correct bookmark name in the code. You need to change the “MyBookmark” with the bookmark name that is used in your document.

Please share your input document. We will then share the code example according to your requirement.

Please find the code snippet and the attached word template. The attached word template contains an image which is tagged to the Bookmark called “Signature”.

/// Insert Image to the word document.
public static void AddImageToWord(string storageLocation, string parenturl, string loginName)
{
    try
    {
        object save = true;
        Image ImageForCallout = Image.FromStream(GetImageforCharts(parenturl, loginName));

        Document doc = new Document(storageLocation);
        DocumentBuilder builder = new DocumentBuilder(doc);
        int count = doc.Range.Bookmarks.Count;
        builder.MoveToBookmark("Image1");
        builder.InsertImage(ImageForCallout, 80, 50);
        doc.Save(storageLocation);
    }
    catch (Exception ex)
    {
        Utility.HandleException(Utility.GetCurrentUrl(), ex);
    }
}

/// 
/// Gets the image.
/// 
public static MemoryStream GetImageforCharts(string siteUrl, string fileName)
{
    byte[] fileContentsArray = null;
    MemoryStream imageStream = null;
    try
    {
        using (SPSite site = new SPSite(siteUrl))
        // using (SPSite site = SPContext.Current.Site)
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["ImageLibrary"];
                SPListItemCollection items = docLib.Items;
                foreach (SPListItem item in items)
                {
                    if (item.DisplayName.ToLower().Contains(fileName))
                    {
                        SPFile file = item.File;
                        using (Stream fileContents = file.OpenBinaryStream())
                        {
                            var length = fileContents.Length;
                            fileContentsArray = new byte[length];
                            fileContents.Read(fileContentsArray, 0, Convert.ToInt32(length));
                        }
                    }
                    // string url = item.Url;
                }
            }

        }

        imageStream = new MemoryStream(fileContentsArray);
        return imageStream;
    }
    catch (Exception)
    {
        throw;
    }
}

@manoj1197

Thanks for sharing the detail. Please use following code example to remove the bookmark image. Hope this helps you.

Document doc = new Document(MyDir + "Manoj.doc");
Bookmark bookmark = doc.Range.Bookmarks["Signature"];
bookmark.Text = "";
bookmark.Remove();
doc.Save(MyDir + "17.4.0.doc");

is it possible to remove any given image found in the docx file without knowing the identifier?

Hi Manoj,

Thanks for your inquiry. The image is imported into Aspose.Words’ DOM as Shape node. Please use Shape.Remove method to remove it from the document.

Please let us know if you have any more queries.