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

Free Support Forum - aspose.com

Image with size switches are not working using NonMergeFields syntax

The input is a template in .doc/.docx. I am using Aspose.Words 18.12.0 in .Net

I am using the syntax of {{fieldname}} so UseNonMergeFields = true.

I am able to get the image to show via: {{image:image__c}} with data -> {"image__c", **S3 url**}.

With this syntax alone, it stretches the grid to fit the image.
When this syntax is used inside a TEXTBOX, it crops the image to the size of the textbox from the top left.

Looking at this doc: https://docs.aspose.com/words/net/mail-merge-template/

I should be able to use the size switches (to prevent cropping of the image when used inside a textbox), but the doc is not written for “UseNonMergeFields”. When the switches are applied to the template, those fields are just blank. No Image.

Here is what I’ve tried: (inside and outside of text boxes)
{{image:image__c fitWidth}}
{{image:image__c -fitWidth}}
{{image:image__c -fitHeight}}

All are exported as .pdf

Template and generated pdf are attached in the zip.
Docs.zip (446.1 KB)

@jason2019,

Thanks for your inquiry. Please also create a standalone simple console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

@awais.hafeez I do not have the bandwidth for that at the moment. Can you confirm if the switches I am using is correct and is supported if the image is retrieved via URL while using NonMergeFields = true?

Example:
In template: {{image:image__c -fitHeight}}
Data: { image__c , "https://blog.aspose.com/wp-content/uploads/sites/2/2011/08/Aspose.Words_.Express-Settings.jpg" }

@jason2019,

The image tags you are referring to belong to LINQ Reporting Engine. You can not use them while using normal reporting i.e. Mail Merge. As shared earlier, please create a standalone simple console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size. You can ZIP the files that should reduce the file size to KBs. Thanks for your cooperation.

@awais.hafeez what switches would work with Mail Merge Images ?

Please see console application attached.

MailMergeExample.zip (21.5 KB)

@jason2019,

I am afraid, there are no such switches in simple Mail Merge Reporting. However, you can manipulate image sizes by implementing the IFieldMergingCallback interface. Please see these sample input/output Word documents (Sample-Docs.zip (91.0 KB)) and try running the following code:

Document doc = new Document("E:\\Docs\\Sample.docx");
            
var data = new Dictionary<string, object>
{
    ["FIRST_NAME"] = "First",
    ["LAST_NAME"] = "Last",
    ["FirstName_Plus___Calc__c"] = "Test",
    ["image__c"] = "https://blog.aspose.com/wp-content/uploads/sites/2/2011/08/Aspose.Words_.Express-Settings.jpg"
};

doc.MailMerge.FieldMergingCallback = new HandleFieldMergingCallback();
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveEmptyTableRows | MailMergeCleanupOptions.RemoveUnusedRegions;
doc.MailMerge.UseNonMergeFields = true;
doc.MailMerge.Execute(data.Keys.ToArray(), data.Values.ToArray());

foreach(Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    if (shape.ShapeType == ShapeType.Image)
    {
        shape.AspectRatioLocked = false;
    }
}

doc.Save("E:\\Docs\\19.1.docx");

internal class HandleFieldMergingCallback : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        if (e.Field.GetFieldCode().Contains("Image:"))
        {
            Shape textBox = (Shape)e.Field.Start.GetAncestor(NodeType.Shape);
            if (textBox.ShapeType == ShapeType.TextBox)
            {
                textBox.TextBox.InternalMarginLeft = 0;
                textBox.TextBox.InternalMarginRight = 0;
                textBox.TextBox.InternalMarginTop = 0;
                textBox.TextBox.InternalMarginBottom = 0;

                e.ImageWidth = new MergeFieldImageDimension(textBox.Width);
                e.ImageHeight = new MergeFieldImageDimension(textBox.Height);
            }
        }
    }
}

Hope, this helps.

@awais.hafeez, this solution works to an extend to restricts the image within the box, but this loses the image’s aspect ratio. And because the image is retrieved via an URL, we do not have any information on the image yet. Is there a way to maintain the ratio of the image and make sure all of it’s content is inside the text box?