HTML contains inline images is missing while creating a document with mail merge

I am using mail merge to create a document. In the merge field I have to insert html content.
Using doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml(); i am able to insert html data. But inline image which is part of html is missing while conversion

@Rajeshraj1983

Thanks for your inquiry. 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 attach the expected output Word file that shows the desired 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.

Hi Tahir,

Thanks for your quick reply.

Please find the attached files for trouble shooting.

Also please find the sample html data which contains images.

<p>when does this matter updated<br><br></p><ol><li>fsdfsd</li><li>fgfdgf</li><li>fjfjsdfd<br><img alt=“” src=“https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png”><br><img alt=“” src=“/Utility/GetUploadedImage?Imagename=%2FKendoImages%2FImages%2FAlternatives_3.jpg”></li></ol>Aspose_Troubleshooting.zip (21.4 KB)

@Rajeshraj1983

We have tested the scenario using the latest version of Aspose.Words for .NET 19.3 and have not found the shared issue. Please use Aspose.Words for .NET 19.3. We have attached the output DOC file with this post for your kind reference. output.zip (9.1 KB)

I also used latest version of Aspose.Words for .NET 19.3 but didnt render image.
Could you please share the code for word conversion?

Also cannot install Aspose.Words 19.3 reference using Nuget. I am getting an error like
‘Aspose.Words’ already has a dependency defined for ‘SkiaSharp’… Could you please help me on that too?

@Rajeshraj1983

Your document does not contain the Discussion mail merge field. We have modified your document and tested the scenario using the following code example. Please check the attached documents.
Docs.zip (25.9 KB)

Document doc = new Document(MyDir + "MyTemplate.docx");
doc.MailMerge.FieldMergingCallback = new HandleMergeField();
doc.MailMerge.Execute(new string[] { "Discussion" }, new object[] { "" });
doc.Save(MyDir + "output.docx");

private class HandleMergeField : IFieldMergingCallback
{
    /// <summary>
    /// This is called when merge field is actually merged with data in the document.
    /// </summary>
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.DocumentFieldName.Equals("Discussion"))
        {
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            builder.MoveToMergeField(e.DocumentFieldName);
            builder.InsertHtml(File.ReadAllText(MyDir + "input.html"));
            Console.WriteLine(e.Text);
        }
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
}

Please uninstall Aspose.Words and install it again through NuGet.
Installing Aspose.Words for .NET through NuGet

Hi Tahir,
Thanks for your reply. We found the issue is related with the image path. We used relative path as image source. eg: “< img src=”/Content/imagename.jpg" />". How can solve relative path issue?
Also we dont have any physical html file. html content is stored in the database which contains image relative path

@Rajeshraj1983

You can use LoadOptions.BaseUri property to get or set the string that will be used to resolve relative URIs found in the document into absolute URIs when required.

This property is used to resolve relative URIs into absolute in the following cases:

  1. When loading an HTML document from a stream and the document contains images with relative URIs and does not have a base URI specified in the BASE HTML element.

  2. When saving a document to PDF and other formats, to retrieve images linked using relative URIs so the images can be saved into the output document.

    Aspose.Words.LoadOptions options = new Aspose.Words.LoadOptions();
    options.BaseUri = @“C:\temp”;

    Document doc = new Document(MyDir + “in.docx”, options);
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.InsertHtml(@"<img src=’/Content/image.png’ />");
    doc.Save(MyDir + “19.3.docx”);

Hi Tahir,
How can we hide/show heading and display outer containing table based on the mergefield data from database?

Requirement:
• Need to display Sample heading 1 only if megefield ‘sampledata1’ is available
• Need to display mergefield containing table only if mergefield ‘sampledata2’ is available

@Rajeshraj1983

You need to use the IF and mail merge fields to achieve your requirement. Please check the attached sample document. Hope this helps you.

Sample Template.zip (9.9 KB)

Hi Tahir,
Thanks for the quick reply.
When we used IF and mail merge field together, portion of data is truncated and style is missing

@Rajeshraj1983

Thanks for your inquiry. 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 attach the expected output Word file that shows the desired 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.

Hi Tahir,

Please find the Sample template and code we used for Mailmerge.AsposeTroubleshoot.zip (10.9 KB)

@Rajeshraj1983

We have attached the modified template document with this post. The double quotes in the result of IF field were missing in old template. Please create the template according to attached document to get the desired output.

New Sample Document.zip (12.0 KB)