Search code examples
c#imagems-wordinsertion

Inserting images in word using C# between the sentence


I have a MS word file which has some sentences and I need to insert some images in between the lines. When I am using the AddPicture method in Microsoft.Office.Interop.Word I am able to insert the image but not at a particular position.

I did not find any method to insert other than AddPicture to insert an image into existing word file. I am trying to insert an image after a particular line after apple there should an image of apple.

Here I am creating a paragraph and trying to add the image. This is my initial file:

enter image description here

This contains paragraphs containing the words apple, mango, and grape.

This is the output of my code (below)

enter image description here

The image should be inserted after the apple line Required output:

Required Output

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Metadata;
using Word =Microsoft.Office.Interop.Word;
using System.IO;
namespace ConsoleApp2 
{
    class Program
    {
        static void Main(string[] args)
        {
            Word.Application ap = new Word.Application();
            Word.Document document = ap.Documents.Open(@"C:\Users\ermcnnj\Desktop\Doc1.docx");
            //document.InlineShapes.AddPicture(@"C:\Users\ermcnnj\Desktop\apple.png");
            String read = string.Empty;
            List<string> data = new List<string>();
            for (int i = 0; i < document.Paragraphs.Count; i++)
            {
                string temp = document.Paragraphs[i + 1].Range.Text.Trim();
                if (temp != string.Empty && temp.Contains("Apple"))
                {
                    var pPicture = document.Paragraphs.Add();
                    pPicture.Format.SpaceAfter = 10f;
                    document.InlineShapes.AddPicture(@"C:\Users\ermcnnj\Desktop\apple.png", Range: pPicture.Range);
                }

            }

        }
    }
}

The above is the code I am using.


Solution

  • The following code snippet illustrates how this can be done. Note that. for the sake of clarity, it's simplified to set only the text to be found - there are a lot of additional properties that might need to be specified; read up on the Find functionality in Word's Language Reference.

    If a search term is found, the Range associated with Find changes to the found term and further action can be taken. In this case, a new (empty) paragraph is inserted after the found term. (The question specifies that the term is the entire content of a paragraph, so that's what this example assumes!) The Range is then moved to this new paragraph and the InlineShape inserted.

    Note how the graphic is assigned to an InlineShape object. If anything needs to be done to this object, work with the object variable ils.

    Word.Application ap = new Word.Application();
    Word.Document document = ap.Documents.Open(@"C:\Users\ermcnnj\Desktop\Doc1.docx");
    
    Word.Range rng = document.Content;
    Word.Find wdFind = rng.Find;
    
    wdFind.Text = "apple";
    bool found = wdFind.Execute();
    
    if (found)
    {
        rng.InsertAfter("\n");
        rng.MoveStart(Word.WdUnits.wdParagraph, 1);
        Word.InlineShape ils = rng.InlineShapes.AddPicture(@"C:\Test\avatar.jpg", false, true, rng);
     }