Replacing embedded PNG

Hi,

We have an add-in to Microsoft Word that lets the user insert a “temporary” image. The doc file is then uploaded to our server and we want to replace the image with a new image using Aspose.Words.

In MS-Word, the image is initially being imported in to the document as a PNG file, and we “tag” it so we can find it later with the AlternativeText attribute. The final portion of the OpenXML document is below. The problem is, when we open this document with Aspose.Words, this section is imported as a DrawingML. I really don’t mind if that happens since during processesing we want to rip the entire section out and replace it with a new image. However, since it is being seen as a DrawingML and not a Shape, I don’t have access to the AlternativeText attribute (see desc=“grid31” below).

If it helps, we are using AddIn Express to do the insert with the following code:

Object oLinkToFile = false;

Object oSaveWithDocument = true;

Object oRange = selection.Range;

Word.InlineShape shape = selection.InlineShapes.AddPicture(globals.GetConfigPath()+ fn, ref oLinkToFile, ref oSaveWithDocument, ref oRange);

shape.AlternativeText = “grid” + GridID;

The section of the OpenXML that contains the image we are trying to find is at the bottom of this message.

Thanks for your help!

take care,

jasen

<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0" xmlns:wp="<http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing>">
<wp:extent cx="2761905" cy="2685714" />
<wp:effectExtent l="0" t="0" r="635" b="635" />
<wp:docPr id="1" name="Picture 1" descr="grid31" />
<wp:cNvGraphicFramePr />
<a:graphic xmlns:a="<http://schemas.openxmlformats.org/drawingml/2006/main>">
<a:graphicData uri="<http://schemas.openxmlformats.org/drawingml/2006/picture>">
<pic:pic xmlns:pic="<http://schemas.openxmlformats.org/drawingml/2006/picture>">
<pic:nvPicPr>
<pic:cNvPr id="0" name="" />
<pic:cNvPicPr />
</pic:nvPicPr>
<pic:blipFill>
<a:blip xmlns:r="<http://schemas.openxmlformats.org/officeDocument/2006/relationships>" r:embed="rId4" />
<a:stretch>
<a:fillRect />
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0" />
<a:ext cx="2761905" cy="2685714" />
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst />
</a:prstGeom>
</pic:spPr>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>

Hi Jasen,

Thanks for your inquiry.

The current version of Aspose.Words the DrawingML class does not yet provide public methods to interface with the object’s properties. These features are planned for the next few versions. We will keep you informed of when this is avaliable.

In the mean time I can only suggest to use bookmarks instead to mark your images or prehaps since your images are just .png pictures try using an older version of Aspose.Words for now which should still load them as Shape objects with visible properties.

Thanks,

Thanks Adam.

This is very disappointing. Are you saying there is essentially no way to load an image into the Shape object? The bookmarking idea is problematic as ongoing editing by the author sometimes leaves random bookmarks in weird places.

I simply need to be able to find an image on a document and replace it with another one. It doesn’t seem like a crazy task that I am trying to accomplish, and I am open to alternative ways of doing it if you have any ideas.

thanks!

jasen

Hi Jasen,

Thanks for your request. I think, the best way to achieve this is using Mail Merge feature. Please see the following link to learn how to insert images upon Mail Merge:

https://docs.aspose.com/words/net/types-of-mail-merge-operations/

Hope this could help you. Please let me know if you need more assistance, I will be glad to help you.

Best regards,

Hi Alexi,

First let me say I appreciate the response and the attempt to help out here! Thank you!

I don’t think a mail merge is going to work. Our users creating the document within word are clicking a button on the toolbar and we then insert a “temporary” image that is used by the user as a reference and visual placeholder to the final image. I can’t replace that process with a simple text/merge field. It will throw off the complete look and feel during design time.

I’m still looking for an answer to what I think should be straight forward unless I am missing something:

In Aspose.Words, how do I find and image, and then replace that image with a new one?

The secondary question based on what I have read so far is how to I actually get Aspose.Words to use the Shape object that you have created when it loads a Word document into memory?

Thanks!

jasen

Hi

Thank you for additional information. Could you please attach a document where you need to replace an image? I will check it and provide you more information.

Best regards,

Hi,

I have attached a copy of the simplest document which has the image placeholder in it. If you look at it via an OpenXML viewer you will see that it is tagged with description=“grid31” which we wanted to use on the server to isolate that image and replace with a real chart.

If there is alternative way of both tagging the image via some kind of OpenXML call, and finding that image based on the tag via Aspose, I’m open to alternatives… just that bookmarking probably can’t be one of them.

thanks,

jasen

Hi

Thank you for additional information. Unfortunately, the current version of Aspose.Words does not allow getting properties of DrawingML objects. So, I am afraid, you cannot identify the particular DrawingML object by its description. Also, I think, replacing all DrawingML objects is not an option for you because the document can contain many images.

So, unfortunately, the only options I can suggest you are using bookmarks or using mergefields as placeholders for images.

Best regards,

Hi guys –

Thanks, I think we established everything that you just said. However, I am curious as to what I could do on the front end of this to ensure the Aspose.Words actually sees the image as a Shape object rather than a DrawingML document?

Gotta be honest, I’m absolutely amazed that this capability is not provided.

jasen

Hi Jasen,

Thanks for your inquiry. If it is acceptable for you, you can use DOC format instead of DOCX. In DOC file, your image will be recognized as Shape.

Best regards.

Hi Alexey,

we have the same Problem. We need to find and replace an Image (like JPG), wich is a
DrawingML. Is it possible to implement a way to find the image by title
and replace it? Or is it possible to insert a picture as Shape in Word 2010 (docx)?

A bookmark or MergeField cannot be positionend like a DrawingML. Right?

Best regards

Norbert

Hi Norbert,

Thanks for your inquiry. As I mentioned above, unfortunately, there is no way to identify the particular DrawingML object yet. Currently, you can identify Shapes by name:
https://reference.aspose.com/words/net/aspose.words.drawing/shapebase/name/

But, the disadvantage of this approach is that you cannot set shape name from MS Word UI. You can set it only using macro or using Aspose.Words.

Best regards,

Hi Alexey,

thanks for your answer. Is there any intention to implement this to Aspose.Words in the future? In Word 2010 i can set a Title for the Image in the UI. If it would possible to iterate through the DrawingML-nodes of my document and identify the right object by title, i could replace it :o]

Thanks in advance.
Norbert

Hi Norbert,

Thanks for your inquiry. Sure, in one of future versions we will expose more properties of DrawingML objects.

Best regards,

Ok,

Thanks for the continued feedback.

So now I am saving the document as Word 2003-2007.

Aspose is successfully finding the proper image.

I have a new image that is a file of the same type.

How do I replace the embedded image with the new one?

thanks,

jasen

Hi

Thanks for your inquiry. You can use ImageData.SetImage method to achieve this:

https://reference.aspose.com/words/net/aspose.words.drawing/imagedata/setimage/

Hope this helps.

Best regards,

Hello,

We are currently experiencing a similar issue with Aspose.Words 10.0.0.

The thing is that our product manages media items (such as images) added to Word documents through Aspose by verifying the Name property of the Shape with the information stored within a database. However, since these media items are inserted as a DrawingML node rather then a Shape, we can’t access the Name property within the DrawingML node properties, which is vital for our media item version management.

For the time being we are using the work around given within the following forum post:
https://forum.aspose.com/t/78903

Would it be possible to give verification if the matter could be resolved within the next few weeks or perhaps an indication in which version it might be released? Even if it is by simply adding a public Name Property on the DrawingML nodetype.

We are planning to release an update for our software within the next 3 weeks and we would really like to resolve the issue within this particular release.

With kind regards,

Tom Pouwelse
Software Engineer Infoland BV

Hi Tom,

Thanks for your request. I will consult with our developers regarding exposing properties of DrawingML objects and let you know once I get some feedback from them.

Best regards,

Hi,

Is it possible to give an estimate for this fix? (Weeks/months?).

We could go for the workaround and save it as .doc, but we risk breaking the customers formatting if they use .docx specific features of Word. So we prefer not to take that risk.

Best regards,

Robert Wielink

Sr. Software Engineer

Infoland B.V.

Hi Robert,

Thanks for your request. Unfortunately, we have to postpone this issue for now. We should investigate all use-cases for creating a public API. We will back to this issue in a month or so. I apologize for inconvenience.

Best regards,