0

I have created a doc file with picture placeholder in it. Now I need to insert an image into the placeholder. I'm using the following code.

using Microsoft.Office.Interop.Word;
using System.Drawing;

namespace ImagetoDoc
{
    class Program
    {
        static void Main(string[] args)
        {
            Image im = Image.FromFile(@"C:\Users\BabyboB\Documents\google.png");   

            Application app= new Application();
            Document doc = app.Documents.Open(@"C:\Users\BabyboB\Documents\testingdoc.docx");
            doc.SelectContentControlsByTag("testing");


        }
    }
}

How to add a picture after identifying tag?

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
babybob
  • 444
  • 6
  • 22

1 Answers1

0

Try below

const string FILE_IMAGE = @"C:\Users\BabyboB\Documents\google.png";
    const string FILE_DOCX = @"C:\Users\BabyboB\Documents\testingdoc.docx";

    var app = new MsWord.Application();
    MsWord.Document doc = null;

    try
    {
        doc = app.Documents.Open(FILE_DOCX, Type.Missing);
        var testingCtrls = doc.SelectContentControlsByTag("testing");

        //assuming image jas to be added to 1st control in testingCtrls
        //it should be a content control which allows picture in it.
        //e.g. wdContentControlRichText or wdContentControlPicture.
        var testingCtrl = testingCtrls[1];
        testingCtrl.Range.InlineShapes.AddPicture(FILE_IMAGE, Type.Missing, Type.Missing, Type.Missing);

        doc.Save();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19