Replace Images in Word with Texts in C#

This topic is from Stackoverflow (original post), let us see how Spire.Doc can help developers to resolve their problems related to programming with Office technology. According to the description, the poster wants to replace each image in the Word file with “Here was image {image index}” correspondingly. However, we would like to provide a solution with below sample code.

Test File:

Replace Images in Word with Texts in C#

Code Snippet for Replace Images with Texts

Step 1: Create a new Word document and load the test file.

Document document = new Document(@"..\..\test.docx");

Step 2: Get all pictures in the Word document and replace them with the text "Here was image {image index}".

int j = 1;
foreach (Section sec in document.Sections)
{
    foreach (Paragraph para in sec.Paragraphs)
    {
        List pictures = new List();
        foreach (DocumentObject docObj in para.ChildObjects)
        {
            if (docObj.DocumentObjectType == DocumentObjectType.Picture)
            {
                pictures.Add(docObj);
            }
        }
        foreach (DocumentObject pic in pictures)
        {
            int index = para.ChildObjects.IndexOf(pic);
            TextRange range = new TextRange(document);
            range.Text = string.Format("Here was image {0}", j);
            para.ChildObjects.Insert(index, range);
            para.ChildObjects.Remove(pic);
            j++;
        }
    }
}

Step 3: Save and launch the file.

          document.SaveToFile(@"..\..\result.docx", FileFormat.Docx);
          System.Diagnostics.Process.Start(@"..\..\result.docx");

Result:

Replace Images in Word with Texts in C#

Full C# Code:

[C#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;
using System.Drawing;

namespace ReplaceImageWithText
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document(@"..\..\test.docx");
            int j = 1;
            foreach (Section sec in document.Sections)
            {
                foreach (Paragraph para in sec.Paragraphs)
                {
                    List pictures = new List();
                    foreach (DocumentObject docObj in para.ChildObjects)
                    {
                        if (docObj.DocumentObjectType == DocumentObjectType.Picture)
                        {
                            pictures.Add(docObj);
                        }
                    }
                    foreach (DocumentObject pic in pictures)
                    {
                        int index = para.ChildObjects.IndexOf(pic);
                        TextRange range = new TextRange(document);
                        range.Text = string.Format("Here was image {0}", j);
                        para.ChildObjects.Insert(index, range);
                        para.ChildObjects.Remove(pic);
                        j++;
                    }
                }
            }
            document.SaveToFile(@"..\..\result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start(@"..\..\result.docx");
        }
    }
}