linkedinfacebooktwittermenuarrow-up

GemBox Support Forum

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.