Aspose Words Merge Image BEHIND TEXT

Hi All,

I have a requirement to use an aspose words merge field – set as an image – where the image sits “behind” the text. I’ve attached a screenshot of (1) the setting in Word that I’m trying to emulate and (2) the desired end result.

Please note that I cannot insert an image as a watermark to satisfy this requirement, because the placement of the image must flow with the text (In this case, the signature line may be higher/lower on the page, based on the results of other merge fields, and thus, I cannot estimate its placement.).

Thanks!

Jim

Hi Jim,

Thanks for your inquiry. I have attached a template Word document that contains a MERGEFIELD here for your reference. You can implement IFieldMergingCallback interface, get the value of MERGEFIELD and add an image by placing/running the following code inside IFieldMergingCallback.FieldMerging event:

Document doc = new Document(@"C:\Temp\ImageTemplate.docx");
doc.MailMerge.FieldMergingCallback = new HandleImageMergeField();
doc.MailMerge.Execute(new string[] { "MyField" }, new object[] { "C:\\Temp\\Aspose.Words.png" });
doc.Save(@"C:\Temp\out.docx");
private class HandleImageMergeField : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        DocumentBuilder builder = new DocumentBuilder(args.Document);
        builder.MoveToMergeField(args.FieldName);
        Shape img = builder.InsertImage(args.FieldValue.ToString());
        img.BehindText = true;
        img.WrapType = WrapType.None;
        img.RelativeHorizontalPosition = RelativeHorizontalPosition.Character;
        img.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing. 
    }
}

I hope, this helps.

Best regards,

Thank you. This helped a lot. I noticed a couple things that look like small errors (I think). Here they are:

First, this are image fields. So, we should be handling an ImageFieldMergingArgs event. Like this:

private void IFieldMergingCallback_ImageFieldMerging(ImageFieldMergingArgs e)
{

Second, probably should convert the FieldValue to a byte explicitly before loading into a Shape. Like this:

byte[] myByte = (byte[])e.FieldValue;

Then:

builder.InsertImage(myByte, 50, 50);

Finally, one last question on relative vertical positioning. I need the signature that we are inserting centered vertically in relationship to the character that they are be inserted “next” to. Right now, they are too far “down” on the page.

Please see a screen shot of how the signatures are “too low,” and a screen shot of an example of the Word setting that I am trying to emulate.

Thank you,

Jim

Hi Jim,

Thanks for your inquiry. I think, you can simulate the desired behavior by setting the position of the top edge of the containing block of the shape.

img.Top = img.Top - img.Height / 2;
img.BehindText = true;
img.WrapType = WrapType.None;
img.RelativeHorizontalPosition = RelativeHorizontalPosition.Character;
img.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;

I hope, this helps.

Best regards,

This turned out to be a very good solution. I ultimately decided to hard-code the top off-set, because my images were always the same size.

Thank you for the very good support.

Jim