I’m in the process of implementing a server-side mail merge for our clients using Aspose. So far I’ve been very pleased with its near-parity with MS Word, especially compared to other similar products.
However, I’ve run into a small issue regarding INCLUDEPICTURE fields: When Aspose does the merge, it formats those fields and adds a “* MERGEFORMATINET” field format to the end even though it doesn’t exist on the original field.
This is a small problem for us because that format flag seems to cause Word to stretch the image to the bounding box instead of leaving it with its original height/width ratio. (I guess MERGEFORMATINET is not the default for MS Word if no format is specified.)
For example, this:
{ INCLUDEPICTURE "{IF TRUE "A:\\{ MERGEFIELD FolderName }\\{ MERGEFIELD Filename }.jpg" }"\d }
becomes this after merge:
{ INCLUDEPICTURE \d "A:\\Test\\MyImage.jpg" \* MERGEFORMATINET }
I can see that the field is essentially re-written because the “\d” has also moved to the front, though that doesn’t really matter.
Is there any way to prevent this addition of the “MERGEFORMATINET” format flag when an INCLUDEPICTURE field is processed?
Thanks,
B.J.
@bhardage
To ensure a timely and accurate response, please attach the following resources here for testing:
- Your input Word document.
- Please attach the output Word file that shows the undesired behavior.
- Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.
As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.
PS: To attach these resources, please zip and upload them.
@bhardage
We have tested the scenario using the latest version of Aspose.Words for .NET 21.2 and have not found the shared issue. So, please use Aspose.Words for .NET 21.2.
We have used following code example to test your case. Please check the attached input Word document. input.zip (13.9 KB)
Aspose.Words.LoadOptions loadOptions = new Aspose.Words.LoadOptions();
loadOptions.PreserveIncludePictureField = true;
Document doc = new Document(MyDir + "input.docx", loadOptions);
string[] names = { "imagepath" };
object[] paths = { @"C:\\temp\\image.jpg" };
doc.MailMerge.Execute(names, paths);
doc.Save(MyDir + "21.2.docx");
@tahir.manzoor
When you say you haven’t found the issue, I assume you are referring to the stretching of the image, not the addition of the “\* MERGEFORMATINET” which is clearly happening.
After some additional research, it seems to only be happening when referring to an image on one of my mapped network drives (it may also happen when pointing to an image URL, I don’t know).
I’ve attached an example solution per your instructions. Included there are the input doc and image with which I tested by placing the image on my P:\
drive, which for me is a mapped network drive.
In this example, the MERGEFORMATINET format flag causes the image to stretch, whereas removing that flag will make it look normal.
AsposeIncludeImageExample.zip (54.2 KB)
@bhardage
We have tested the scenario using the latest version of Aspose.Words for .NET 21.2.0 and have not found the shared issue. So, please use Aspose.Words for .NET 21.2.0.
@tahir.manzoor Did you take a look at the example project I attached? It’s using the latest version and the flag is being added.
@bhardage
We tested the scenario using the same application. Please make sure that the path of picture is correct and accessible to your application.
@tahir.manzoor I’ve made a video reproducing the issue if you would please take a look. This is using the sample project I attached earlier. If you can’t access the link, please let me know.
https://1drv.ms/v/s!AhXkD17Jmwy-mS32xce3opg0K-Fj
@bhardage
Please check the attached output DOCX generated by using your application. Output 21.2.zip (16.1 KB)
We have followed below steps to generate the document.
-
Open the input Word document and change P to D drive. We do not have Drive with P name. So we put the image at D drive. step 1.png (21.8 KB)
-
Execute application to generate the document. See the path of image. Step 2.png (19.1 KB)
-
Press Alt + F9 to view the image. Step 3.jpg (108.5 KB)
This issue seems not related to MERGEFORMATINET.
@bhardage
After further investigation, we have reproduce this issue using following steps. We have logged this issue as WORDSNET-21865 and you will be notified via this forum thread once this issue is resolved.
- Put the image at D drive.
- Execute the application.
- In the input document. the image path is
P:\<<mail merge field>>.
- Output document has image path as P:\sample-image.jpg.
- Change P to D (d:\sample-image.jpg) and update the field.
- Press Alt + F9 to view the image.
- The image is stretched.
Please put the image at local disk instead of network, test this case, and share your feedback.
@tahir.manzoor Yes, the image doesn’t stretch if it’s a local path, as I mentioned in an earlier post. I cannot alter this use case, however, as the template documents to be merged are created by our clients. The real issue, though, seems to be with the automatic adding of the MERGEFORMATINET flag, which Aspose is doing.
I appreciate you opening the issue and look forward to its resolution.
@bhardage
Thanks for sharing the detail. We will inform you via this forum thread once there is an update available on this issue.
@bhardage
It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-21865) as ‘Not a Bug’.
Most likely application process does not have an access to the file, but MS Word has. In your scenario it may happen if mapped network drive was created without admin privileges but the application (or IDE) runs under administrator. So, Aspose.Words output contains image with specified 216x216pt size (there is no access to the file) and file path. And MS Word displays image (there is access to the file) with specified size.
You should be sure the file is available under the application process. Moreover, you should not use \d
switch and such special path because generated document will look corrupt (without image) outside environment where file is available.
@tahir.manzoor I don’t understand.
Aspose leaves INCLUDEPICTURE fields alone regardless of access to the specified path. The issue is the addition of the MERGEFORMATINET format flag, which Aspose is adding. Are you saying that Aspose is only adding the MERGEFORMATINET format flag if it can’t access the resource?
@bhardage
Regarding MERGEFORMATINET format flag issue, we logged separate issue as WORDSNET-21989. We will analysis this issue further and provide you more information on it. We apologize for your inconvenience.
@bhardage
It is MS Word which converts a shape into the INCLUDEPICTURE
field, not Aspose.Words. If a shape lacks the embedded image data, MS Word also adds the MERGEFORMATINET
switch. The original field is removed by calling RemoveContainingFields
. So, there a shape instead of original field.
The root problem is that the image is supposedly not accessible from the application. Aspose.Words does not know its size and cannot set it properly. It sets the default dimensions. When you update fields in MS Word, the image gets retrieved and (because of the MERGEFORMATINET
switch) the wrong dimensions are retained. When you remove the switch and updates the field, the new (correct) dimensions are set. That’s our assumption.
What to do?
- Please make sure that the image file is accessible from the application, not from the console. Use something like
File.Exists()
to make sure.
- If you want to get rid of the
INCLUDEPICTURE
field, please donot use RemoveContainingFields
. But it won’t help if the image is not accessible (#1
).
- Please do not use the
\d
switch. But again, it won’t help if the image is not accessible (#1
).
You are using mail merge. We suggest you please use mail merge feature to insert the image into document instead of INCLUDEPICTURE field.