Replace a specific( addressed by Id, name ... ?!) image in a WORD-File


Hello,

I have a document which has different images and I want to replace that image with a new image, but with the same dimension(size).

Using DocIo, as a console-application.

How shall I do it ?

Thanks.

7 Replies

MJ Mohanaselvam Jothi Syncfusion Team February 28, 2018 08:25 PM UTC

Hi Peter,

Thank you for your update.

To achieve your requirement we suggest you to replace the image data in the WPicture instance using LoadImage API in Essential DocIO.

For your reference we have prepared a sample to replace the image data in WPicture based on the title. In this sample we have done the following things:
1.Loads an existing template Word document using DocIO.
2. Gets the pictures in the Word document.
3. Iterates each picture and replace the image data using LoadImage API.
4. Saves the Word document.

Note: Kindly set title for each image in your template Word document. Based on this title value, images are replaced with specific images.

Please find the sample from the below link and let us know if this helps you:
http://www.syncfusion.com/downloads/support/directtrac/136153/ze/GenerateWord_Console-251391862.zip
Please find the template Word document in “Data” folder and output Word document in “Output” folder of the above sample application.

Kindly provide us the description on whether your requirement is to preserve replaced image’s size as same as the existing image size in the template word document. Thereby we will analyze and provide you the appropriate solution at the earliest.

Please let us know if you have any other questions

Regards,
Mohanaselvam J
 



PE Peter March 1, 2018 03:26 PM UTC

Hi thanks.

I see 2 Problems here:

I dont have  EntityType.BlockContentControl:
                        //    BlockContentControl
and I dont want to set a visble title(if this is what you mean)
https://support.office.com/en-us/article/insert-a-caption-for-a-picture-bb74994c-7f8b-457c-be85-92233177a356


Is there another solution ?!



GT Gunasekaran T Syncfusion Team March 2, 2018 11:37 AM UTC

Hi Peter, 
 
Please find the solutions from below 
I don’t have EntityType.BlockContentControl: 
//    BlockContentControl  
 
We suspect you are trying this sample with older version assemblies. This API is added from the version 15.3.0.26. 
 
We recommend you, install our latest Essential Studio 2018 Volume 1 Release v16.1.0.24 which is available for download under the following link.  
  
 
To confirm this from your end in the latest version, kindly use the Syncfusion NuGet Package of 16.1.0.24, which can be downloaded within few minutes from the following link.  
 
This approach is faster than downloading and installing the complete setup for evaluation. 
 
Packages required, 
1. Syncfusion.Compression.Base46.16.1.0.24.nupkg 
2. Syncfusion.OfficeChart.Base46.16.1.0.24.nupkg 
3. Syncfusion.DocIO.Base46.16.1.0.24.nupkg 
 
Please refer the below UG documentation link to know more about install and configuration of Syncfusion Nuget in Visual Studio: 
 
and I don’t want to set a visible title(if this is what you mean) 
https://support.office.com/en-us/article/insert-a-caption-for-a-picture-bb74994c-7f8b-457c-be85-92233177a356 
 
The Title property of an image in Word document is different from the image caption and it will not be visible. Please refer the below screenshot for more information about where the Title property can be viewed or modified. 
 
  
 
Please let us know if you have any other questions. 
Regards,
Gunasekaran
 



PE Peter March 8, 2018 09:07 AM UTC

Ok, but the image loaded from file should have the same resolution as the image in the word-document(so e.g. the image from the filesystem is bigher, it should be shrinked to the size which is used in the documuent at that place)..


MJ Mohanaselvam Jothi Syncfusion Team March 9, 2018 11:21 AM UTC

Hi Peter,

Thank you for your update.

To meet your requirement, we suggest you to get the height and width of WPicture and set the same value after replacing with new image in the Word document using DocIO. For your reference, we have prepared a sample application to replace the image in the Word document and preserves the new image size (big size image) as same as existing image size in the Word document as per your requirement.


Please find the sample from the below link:
http://www.syncfusion.com/downloads/support/forum/136153/ze/GenerateWord_Console-1874295820.zip

Please refer the below UG documentation link to know more about working with images in the Word document using Essential DocIO:
https://help.syncfusion.com/file-formats/docio/working-with-paragraph#working-with-images

Please let us know if you have any other questions.

Regards,
Mohanaselvam J
 



PE Peter March 9, 2018 04:34 PM UTC

thanks.

when I run once again without closing the console, I "Exception thrown: 'System.NullReferenceException' in Syncfusion.DocIO.Base.dll"

right here pictures[i].LoadImage(Image.FromFile(...


Stacktrace:

    Syncfusion.DocIO.DLS.WPicture.ResetImageData()
    Syncfusion.DocIO.DLS.WPicture.LoadImage(Image image)




MJ Mohanaselvam Jothi Syncfusion Team March 12, 2018 11:50 AM UTC

Hi Peter,

Thank you for your update.

On further analyzing with the given details, the reported problem is raised due to WPicture collection
maintained after closing the WordDocument instance in our previous sample application. We have modified the sample application to meet your requirement and to resolve the reported problem.

Please find the modified sample from the below link and let us know if this helps you:
http://www.syncfusion.com/downloads/support/directtrac/136153/ze/GenerateWord_Console-16551942.zip

Please let us know if you have any other questions.

Regards,
Mohanaselvam J
 


Loader.
Up arrow icon