Add some "data-" attributes (or some metadata) to span html elements

Hello,

I would like to know if there possibility to add some “data-” attributes (or some metadata) to span HTML elements that are being created in the resulting HTML element during field merging?

For example:

DocumentModel doc = DocumentModel.Load (GetTexplateFileName(cvData.TemplateID)); 
doc.Sections.ToList().ForEach(section => section.PageSetup.PageColor.SetSolid(Color.White)); 
doc.MailMerge.FieldMerging += (sender, e) => 
{
    if (e.Value is not null && e.Inline is not null) 
    {
        var origInline = e.Inline; 
        // explicitely set text color, it appears GemBox does not know about the intended color of a merge field? 
        (origInline as Run).CharacterFormat.FontColor = Color.Black; 

        //My question would be is it possible here to somehow add html attributes to inline document. In line 
        // above, we are setting font color for merge field element, so what would be generated in result is
        // <span style=\"position:absolute; white-space:pre; font:20pt 'Arial'; color:1000000; font-weight:bold; left:Opt\">SomeValue<span>
        // So question would be is it possible to add "data-*" (e.g."data-test" attribute with value "test".) attribute, similar to how we set color 
        // so that resulting element is 
        // <span style=\"position:absolute; white-space:pre; font:20pt 'Arial'; color:#000000; font-weight:bold; left:Opt\" data-test="test">SomeValue<span> 
    }
};

Thanks

Hi John,

First regarding this code:

// explicitely set text color, it appears GemBox does not know about the intended color of a merge field?
(origInline as Run).CharacterFormat.FontColor = Color.Black; 

Can you tell us what issue are you trying to resolve with that?
This should not be necessary, note that the resulting Run element should have the same FontColor that your MERGEFIELD has.

Now regarding the question, unfortunately, the Run element doesn’t have metadata (currently, only drawings and tables have metadata).

Nevertheless, perhaps you could insert some hidden text to achieve your requirement?
For example, something like this:

doc.MailMerge.FieldMerging += (sender, e) =>
{
    if (e.IsValueFound && e.Value is not null)
    {
        (e.Inline as Run).CharacterFormat.FontColor = Color.Black;
        e.Inlines.Add(new Run(e.Document, "test") { CharacterFormat = { Hidden = true } });
    }
};

I hope this helps.

Regards,
Mario

Hi,

Thank you for your quick response. In regards to FontColor question, that was some leftover code, and it works as expected, we do not have problem with that any more.
Regards second part and adding hidden text, that does not solve our problem as then in generated html there is no that hidden element, so appropriate information is not there. I would have expected that I get some hidden element in generated html as well or something similar.
Anyway, I guess that is probably out of scope of whole library.

The hidden text should be in the output HTML as well, it will just have the “display: none” style on it.

If you’re not reproducing this, please send us a small Visual Studio project that shows what you have so that we can investigate it.

Hi, in regards of project. I may send you this code parts, I guess this would be enough for testing.

//This would be some service method
string htmlString;
HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions()
{
    EmbedImages = true,
};

DocumentModel dc = await cvRepository.GenerateCvDocument(data); //data is entity containing values for merge fields and template docx

using MemoryStream outputMemoryStream = new();
dc.Save(outputMemoryStream, htmlSaveOptions);
outputMemoryStream.Position = 0;

using StreamReader reader = new(outputMemoryStream);
htmlString = await reader.ReadToEndAsync();

return await Task.FromResult(htmlString);

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public async Task<DocumentModel> GenerateCvDocument(MergeData data)
{
    DocumentModel dc = DocumentModel.Load(GetTemplateFileName(data.TemplateID));

    dc.MailMerge.FieldMerging += (sender, e) =>
    {
        if (e.Value is not null && e.Inline is not null)
            e.Inlines.Add(new Run(e.Document, "test") { CharacterFormat = { Hidden = true } });
    };

    dc.MailMerge.Execute(data.Content);

    return await Task.FromResult(dc);
}

In generated html, there is no test, and there is no any element with display:none. If I remove {Hidden:true}, then, “test” is concatenated to merge value from data.Content.

Thank you and best regards,

Hi,

I’ve used a similar code and was unable to reproduce your issue.
Perhaps the problem is that you’re using some older version of GemBox.Document, please try again with the latest version:
https://www.gemboxsoftware.com/document/downloads/bugfixes.html
Or the latest NuGet package:
https://www.nuget.org/packages/GemBox.Document/.

Again, if the problem remains please send us the complete project so that we can investigate your problem.

Regards,
Mario