We're sorry Aspose doesn't work properply without JavaScript enabled.

# SetImage does not appear to update the document

I am trying to replace images in a word document. The image does not appear to get changed after I save the document. I’m using c# and my code looks like: theShape.ImageData.SetImage(imagePath);

If I set a breakpoint before and after the call to SetImage, SourceFullName does not appear to be updated to the new image path. I am using version 6.0.1.0 with .NET 2.0.

Thanks for any help,

Hi

Thanks for your request. SourceFullName gets or sets the path and name of the source file for the linked image. See the following link for more information.
https://reference.aspose.com/words/net/aspose.words.drawing/imagedata/sourcefullname/
Could you also attach your document for testing?
Best regards.

OK…I’m trying to update the image as a stored image, not a linked image. Using COM my code looked like:

theField.Code.Text = imagePath;
theField.Update();


The update caused the image to be refreshed in the document.

Also if I look in the document after setImage has been called, the path is not updated in the field.

Hi
Thanks for your request. You should use code like the following:

// Open document
Document doc = new Document(@"Test063\in.dot");
// Get collection of shapes from the document
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
string newImgName = @"C:\Temp\test.jpg";
// Change path to image
foreach (Shape shape in shapes)
{
{
shape.ImageData.SourceFullName = newImgName;
}
}
// Save document
doc.Save(@"Test063\out.doc");


But note that Aspose.Words does not update INCLUDEPICTURE fields. You should update fields inside the document manually (ctrl+A and F9). Also you can use macro to update fields. See the following link to learn more.
https://support.microsoft.com/en-us/topic/the-filename-field-does-not-automatically-update-when-you-open-a-document-in-word-de2bfb95-d990-1ced-a618-5ac0a2ec1be4
Best regards.

So there’s no way to update the INCLUDEPICTURE fields without using word? The reason I’m evaluating your product is because we are trying to remove the dependency our server has on word to update fields and images in documents. Using word we are able to use the INCLUDEPICTURE field, change the image path and update the field on the server side, then delete the image. The client then downloads the document and the images have been updated. There would be no way to run this macro on the client side to update the images because they are stored on the server. The client has no way of accessing the server image paths.

Hi
Thanks for your request. I think you can replace linked images with not linked as shown in the following code:

// Open document
Document doc = new Document(@"Test063\in.dot");
DocumentBuilder builder = new DocumentBuilder(doc);
// Get collection of shapes from the document
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
// We will store linked shapes in this list
ArrayList shapesToRemove = new ArrayList();
string newImgName = @"C:\Temp\test.jpg";
// Change path to image
foreach (Shape shape in shapes)
{
{
// Insert image at INCLUDEPICTURE position
builder.MoveTo(shape);
builder.InsertImage(newImgName);
}
}
// Remove linked images
foreach (Shape shape in shapesToRemove)
{
shape.Remove();
}
doc.Save(@"Test063\out.doc");


In addition, you can insert images into the document during mail merge. See the following link for more information:
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback/
Hope this could be useful for you.
Best regards.

Alexey,

Thanks for your response. This gets me part of the way there, however once this code is used to insert the image, the field data is gone and there is no way for me to recognize that the image needs to be updated the second time the dogument is “populated” by my server side code. I’m using the INCLUDEPICTURE field path to identify if the image needs to be updated. Is there any way to insert the image retaining the path so it can be updated multiple times? It seems the using merge field code would also remove this information so will not do what we need either.

Thanks for your help,