Image handling with Aspose.Words

Hello there,

Can anyone please guide me on how to replace image in dotx (Word Template) file at run-time.

I am creating a new document from template file as:

Document objDocument = new Document(dir + "UDK1.dotx");

and then I am iterating through the nodes as:

NodeCollection docNodes = objDocument.GetChildNodes(NodeType.Any, true);
foreach (Node objNode in docNodes)
{
    Response.Write(objNode.ToString() + " ==>> " + objNode.ToTxt() + “”);
}

But I can’t find anything for the image object in my document.
Document ‘UDK1.dotx’ is attached with my message.
Another template document(‘UDK2.dotx’) in which I tried with ‘Picture Content Control’ is also attached.
(Note: File Attachment is not allowing me to upload a .dotx file, so uploading it as .docx file)

Please guide.

Hi

Thanks for your inquiry. Images in Word documents are Shapes. So you can use the following code to get all shapes from your document:

// Get all shapes.
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

Then, you can loop through all Shapes, find Image shapes and replace image.For example see the following code:

// Open document.
Document doc = new Document(@"Test001\in.doc");

// Get all shapes.
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

// Loop through all Shapes.
foreach (Shape shape in shapes)
{
    // Check if Shape is an image.
    if (shape.ShapeType == ShapeType.Image)
    {
        // Here we can replace image.
        shape.ImageData.SetImage(@"C:\Temp\test.jpg");
    }
}

// Save output document.
doc.Save(@"Test001\out.doc");

Hope this helps.

Best regards.

Thanks a lot Alexey for your reply.

Well, your reply will solve my problem to a great extent,
however I also want a way to find which Image/Shape it is before I go to replace one.

So, essentially want a way to do something like:

if (shape.ShapeType == ShapeType.Image && shape.Id == "shapeUniqueId")
// OR if(shape.ShapeType == ShapeType.Image && shape.Title == "shapeUniqueTitle")
{
    // Here we can replace image.
    shape.ImageData.SetImage(@"C:\Temp\test.jpg");
}

We do have Title property for ‘Picture Content Control’ in Word Template (.dotx) file.
But, I am not really sure if we can set the Id of an image in word document.

In short, I really want a way to identify which particular an image is, so that I can replace it with right kind of image.

Any comments?

Thank you!

Hey Alexey,

I have got the shape.Name property so
if I have an actual Image that I want to replace in my word document, then
I can use like

if (shape.Name == "DemoImage.jpg")

Can you please suggest a way we can identify a ‘Picture Content Control’ in Word Template (.dotx) file.

Hi

Thanks for your inquiry. You can identify your images using Shape.Name. But, unfortunately, in MS Word you cannot sent name of the shape from UI. You can achieve this only from macro.

Best regards.

Is there any way we can identify a ‘Picture Content Control’ at run-time using Aspose Words Object Model?

We do specify values for Title and Tag property for ‘Picture Content Control’ while designing a template, but I am not able to find a way to get these properties dynamically.

Please help.

Thank you!

Hi

Thanks for your inquiry. Unfortunately, Aspose.Words does not support Content Controls. I linked your request to the appropriate issue. You will be notified as soon as this feature is supported.

Best regards.

The issues you have found earlier (filed as 4295) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(3)

Hi,

I show this thread and downloaded the latest DLLs to make sure I can handle the ‘Picture content control’. My case is slightly different from the issue described in this thread. I have a .doc or .docx file which has the ‘picture content control’. This control has both title and tag property set. I want to read these properties and than based on these properties I want to replace the image with byte[] data I have for the new image. Can someone please provide any pointer in this direction?

Thx

Thanaml.

Hello Thanaml,

Thanks for your request. Using the latest version of Aspose.Words you can get collection of StructuredDocumentTags using the following code:

// Open document
Document doc = new Document("C:\\Temp\\in.docx");

// Get all StructuredDocumentTags
Node[] tags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).ToArray();

Then you can loop through this collection and get/insert image. But there is one problem, currently there is no way to get SDT properties (like Title, Tag etc) so there is no way to identify SDT object.

Your request has been linked to the appropriate issue. You will be notified as soon as the feature which allows you to get properties of STD is supported. Hopefully, it will be supported within 4-5 weeks.

Best regards,

Thx a lot for your prompt reply!

I did execute the same code but I am not sure how to reset the image data inside the structuredDocumentTag (in my case picture content control). In my case I have the new iamage data as buye[] array and I need to reset this data inside the picture content control. Can you please put a sample code or some pointers?

Also I will appreciate if this request of getting the properties of SDT can be expediated somehow as I have a critical prod release in couple of weeks.

Thx in advance for your help!!

Thanmal.

Hello Thanmal,

Thanks for your request. Please try using the following code:

// Open the document.
Document doc = new Document("C:\\Temp\\in.docx");

// Create DocumentBuidler object, it will help use to insert Image.
DocumentBuilder builder = new DocumentBuilder(doc);

// Get content contorl where you would like to insert Image.
// Currently there is no way to identify content controls by title or tag,
// so to demonstrate the technique I just get the first SDT
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);

// Remove old content from the SDT
sdt.RemoveAllChildren();

// Now, create an empty paragraph and append it to the content control
sdt.AppendChild(new Paragraph(doc));

// Move DocumentBuilder cursor to the first child of SDT (newly created paragraph)
builder.MoveTo(sdt.FirstChild);

byte[] imgBytes = File.ReadAllBytes("C:\\Temp\\img.jpg");

// Insert Image
builder.InsertImage(imgBytes);

// Save output
doc.Save("C:\\Temp\\out.docx");

Best regards,

Thx again for your reply!

While I wait for 4-5 weeks to get the feature related to SDT properties, is there any way available for me to do the following:

  1. My doc has multiple images on it. I need to identify them by some identification mechanism. Once Identified, I need to replace them with the byte[] I have for these images. Can I use something other than ‘picture content control’ which is supporetd by Aspose API totally where I can identify these images by some ID and than replace the data.

I will really appreciate your insight into it as this will allow me to complete my requirements while I wait for the SDT feature implementation.

Thx in advance for your help!

Hi

Thanks for your inquiry. As I understand you have a template document and you can control how this template should look. In this case you can try using MailMerge feature. Please see the following link to learn how to insert images during MailMerge:

If you would like to fill the document with data (images) and then fill the same document with other data (images), it is impossible to achieve using mail merge. This is how mail merge works: during mail merge, merge fields are replaced with values. So in final document, merge fields are no longer available. In MS Word, this works exactly the same.

If you need to fill document with data several times, you can consider using bookmarks. Or you can create the template document and use this template each time when you need to insert new images.

Please see the following link to learn how to work with bookmarks in Aspose.Words:

Hope this helps.

Best regards,

Thx a lot for your reply!

But because of various formatting control reasons, we would like to go with picture content control. I will appreciate of you can let me know by what timeframe I can expect the enhancement which will allow me to have the SDT properties (Tag name etc.) available so that I am able to identify the SDT control by some ID and than able to replace the image inside it.

Thx!

Hi

Thanks for your inquiry. This feature is scheduled on the end of this month. You will be notified as soon as it is supported.

Best regards,

Thx a lot for your response and accomodating this feature on a short notice!

Hi,

I have downloaded the latest version of Aspose. I assume that this f

Reply: Re: Image handling with eature is now available.

Can you please post a quick sample to demonstrate how we can get the ID and replace the picture for these controls?

Thx in advance for your help!

I meant that I assume the feature is now available. Waiting for a quick sample to demonstrate the use case where I can identify the control by ID and can replace the picture.

Thx!

Hi

Thanks for your request. Unfortunately, we did not manage to include this feature in the recently released version of Aspose.Words. We re-estimate the issue and postpone it a bit. At the moment we plan to implement this feature before our August release. I apologize for inconvenience.

Please note, that all estimate we provide in the forum are rough estimates and you cannot 100% rely on them.

Best regards,