Insert and Link Picture in Word Document using C# .NET Code | Embed or Link to Image File

Hello,
I use a sample code as below to retrieve an image from a SharePoint image Library to embed and link it to a word document. The word doc displays the image, but the image was not embeded to the doc. I think it is because it does not have permission to download the image from SharePoint Library. I then write code to download the image to local and convert it to byte array. Then, I use shape.ImageData.ImageBytes = bytes[]. The image was embeded to the doc, however the link doesn’t work. When I change the source image, the image in word is not changed. Could anyone please provide a solution? Any help would be appreciated.

Shape linkedAndStored = new Shape(builder.Document, ShapeType.Image);
linkedAndStored.WrapType = WrapType.Inline;
linkedAndStored.ImageData.SourceFullName = imageFileName;
linkedAndStored.ImageData.SetImage(imageFileName);

builder.InsertNode(linkedAndStored);
builder.Writeln();

Thanks!

Hi Eric,

Thanks for your inquiry. Aspose.Words embeds the image when you set it using ImageData.SetImage method. When only using ImageData.SourceFullName property, Aspose.Words inserts an INCLUDEPICTURE field in Word document. This INCLUDEPICTURE field is used to insert a picture from a file. However, when you use both ImageData.SourceFullName property and ImageData.SetImage method, Aspose.Words will insert INCLUDEPICTURE field, and when you open the output Word document with Microsoft Word, it displays image which is specified by using ImageData.SetImage method. You can press Alt+F9 keys in MS Word to confirm this. Please let me know if I can be of any further assistance.

Best regards,

Thanks Awais.
The generated word document does behave as you said. When I insert and link an image to a word document manually, the image is downloaded and linked. If I change the source image (linked image), the word document will display the updated image. I am wondering whether there is a way using Aspose.words to mimic this functionality.
Best Regards,
Eric

Hi Eric,

Thanks for your inquiry. Could you please list complete steps you have done in Microsoft Word to generate your expected Word document here for our reference. Also, please attach this Word document here for testing. We will then provide you code to achieve the same using Aspose.Words.

Best regards,

Thanks Awais.
The steps are as below. I attached the test documents and images for your reference. Looks like the difference between word documents and aspose.words generated documents is that word documents display the updated image, generated documents display the embeded image. When I unzip the word document, under the media folder, it still has the old image just as generated documents.

  1. Create a folder “images” under C drive, copy two images birds.jpg and facebook.jpg to it.
  2. Start word.
  3. Click Insert tab.
  4. Click Picture.
  5. Select the picture “birds.jpg” from the images folder.
  6. Click the down arrow beside Insert button, and select “Insert and Link”.
  7. Save the word document, and close it.
  8. Remove the birds.jpg, and change the name of image “facebook.jpg” to “birds.jpg”
  9. Open the word document, the image becomes facebook image.

Best Regards,
Eric

Hi Eric,

Thanks for the details. Please use the following simple code to achieve this:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape linkedAndStored = new Shape(builder.Document, ShapeType.Image);
linkedAndStored.ImageData.SourceFullName = MyDir + @"images/birds.jpg";
builder.InsertNode(linkedAndStored);
builder.Writeln();
doc.Save(MyDir + @"out.doc");

I hope, this helps.

Best regards,

Hi Awais,
Thanks for your reply. I tried this code before. It will link to the image without download. When the document doesn’t have access to the image, the image will not display. Our current requirement is “embed and link”. When users work offline, they can still see the images. Could you please provide a solution?
Thanks,
Eric

Hi Eric,

Thanks for the additional information. I have logged your requirement in our issue tracking system as WORDSNET-10320. Our development team will further look into the details of this problem and we will keep you updated on the status of this issue. We apologize for your inconvenience.

Best regards,

Hi Awais,
Thank you very much. I am looking forward to hearing from you soon.
Best Regards,
Eric

Hi Eric,

Thanks for being patient. Please see the following examples:

string MyDir = "C:\\Temp\\images\\";
DocumentBuilder builder = new DocumentBuilder();
string imageFileName = MyDir + "birds.jpg";
builder.Write("Image linked, not stored in the document: ");
Shape linkedOnly = new Shape(builder.Document, ShapeType.Image);
linkedOnly.WrapType = WrapType.Inline;
linkedOnly.ImageData.SourceFullName = imageFileName;
builder.InsertNode(linkedOnly);
builder.Writeln();
builder.Write("Image linked and stored in the document: ");
Shape linkedAndStored = new Shape(builder.Document, ShapeType.Image);
linkedAndStored.WrapType = WrapType.Inline;
linkedAndStored.ImageData.SourceFullName = imageFileName;
linkedAndStored.ImageData.SetImage(imageFileName);
builder.InsertNode(linkedAndStored);
builder.Writeln();
builder.Write("Image stored in the document, but not linked: ");
Shape stored = new Shape(builder.Document, ShapeType.Image);
stored.WrapType = WrapType.Inline;
stored.ImageData.SetImage(imageFileName);
builder.InsertNode(stored);
builder.Writeln();
builder.Document.Save("out.docx");

Second variant “Image linked and stored in the document” is what you are looking for. The only difference after renaming image (facebook to birds) you have to open the document and press Ctrl+A and then F9 to update the image.

Best regards,

Hi Awais,
Thanks for your reply. I saw this sample code before and tried, but the behavior is different from word documents as I mentioned previously. For the “Image linked and stored in the document” section, the image is stored, and a link is added to the document. However, when I change the source image, the document still displays the old image. Based on my understanding, it sounds like a behavior by design. I am wondering whether Aspose.words could provide a solution to mimic the same behavior as word?
Regards,
Eric

Hi Eric,

Thanks for your inquiry. I have logged your comment against this issue in our issue tracking system. We will keep you informed of any updates and let you know once it is resolved.

Best regards,

Hi Eric,

I am afraid, currently there is no way to insert image into the document as DrawingML. This issue depends on the resolution of another internal issue WORDSNET-10335. So, WORDSNET-10320 is postponed until the resolution of WORDSNET-10335. After WORDSNET-10335 is implemented, we will try to implement this mechanism in the public API. There is no estimate for this now. We will keep you informed of any developments and let you know once this issue is resolved.

Best regards,

Hi Awais,
Thanks for letting me know. I am looking forward to hearing from you.
Regards,
Eric

@learningjack,

We suggest you to please [upgrade to the latest (20.9) version of Aspose.Words for .NET]https://releases.aspose.com/words/net/ and try running the following code again on your end:

string MyDir = "C:\\images\\";
DocumentBuilder builder = new DocumentBuilder();

string imageFileName = MyDir + "birds.jpg";

builder.Write("Image linked, not stored in the document: ");

Shape linkedOnly = builder.InsertImage(imageFileName);
linkedOnly.WrapType = WrapType.Inline;
linkedOnly.ImageData.SourceFullName = imageFileName;

builder.Writeln();

builder.Write("Image linked and stored in the document: ");

Shape linkedAndStored = builder.InsertImage(imageFileName);
linkedAndStored.WrapType = WrapType.Inline;
linkedAndStored.ImageData.SourceFullName = imageFileName;
linkedAndStored.ImageData.SetImage(imageFileName);

builder.Writeln();

builder.Write("Image stored in the document, but not linked: ");

Shape stored = builder.InsertImage(imageFileName);
stored.WrapType = WrapType.Inline;
stored.ImageData.SetImage(imageFileName);

builder.Writeln();

builder.Document.Save("C:\\Temp\\out.docx");