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

Free Support Forum - aspose.com

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,

Adam

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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.

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.drawing.imagedata.sourcefullname.html

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<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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)

{

if (shape.ImageData.IsLink)

{

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.

http://support.microsoft.com/kb/832897

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<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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)

{

if (shape.ImageData.IsLink)

{

//Insert image at INCLUDEPICTURE position

builder.MoveTo(shape);

builder.InsertImage(newImgName);

shapesToRemove.Add(shape);

}

}

//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:

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/the-mergeimagefield-event.html

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,

Adam

It appears that if I set the SourceFullName property on the new Shape created by the InsertImage calls that things behave as I want. Thanks for your help!