ADAPT HTML POLICIE FROM PLACEHOLDER IN TEMPLATE 1

Hello,

I m trying to send html format in my placeholder and then generate a pdf and it s working as expected .
Now i want that what i send in html will be formated with the right size and format policy of the appropriate placeholder in my template .

I need your help please!
Don’t hesitate if you node more clarifications !

Thanks in advance.

Yes that’s the part of code that we implement when we insert html tags :slight_smile:

public void fieldMerging(FieldMergingArgs args) throws Exception
{
    if (documentBuilder == null)
    {
        documentBuilder = new DocumentBuilder(args.getDocument());
    }
    // ------ HTML Handling --------
    // All merge fields that expect HTML data should be marked with some prefix, e.g. 'html'
    if (args.getDocumentFieldName().startsWith(HTML_PREFIX) && args.getFieldValue() != null)
    {

        // Insert the text for this merge field as HTML data, using DocumentBuilder
        DocumentBuilder builder = new DocumentBuilder(args.getDocument());
        builder.moveToMergeField(args.getDocumentFieldName());
        builder.insertHtml((String)args.getFieldValue());
    }

Today we generate a right pdf when we push html in fieldValue like this :

{
    "fieldName": "HTML_MAIN_TEXT_BODY",
    "fieldValue": "<div style='color: black; font-family: Times New Roman; font-size: 12px'><u>Special Condition(s)</u><br/> The Euler Hermes grade that you have requested for the above mentioned company is 06, USD 40 000. It may also be consulted via our online portal.<br/>Please note that if at any time during the duration of this EZ Cover decision, we issue you a <b>Permitted limit</b> on the Insured Buyer, the <b>Permitted limit</b> takes precedence over this EZ Cover decision.</div>",
    "typeCode": "HTML"
}

But now my objectif is not to set manually the policy in the style of balise html , but i want to set it in the code when i insertHtml (means that want to insert html with the font size that i have in the placeholder (MAIN_TEXT_BODY) mentionned in my document ) and send it to Aspose .

Thanks!

I have this code as fillPlaceholder :

public Document fillPlaceholders(TemplateDataSourceDto dataSource, Document template) {
 LOGGER.info(“Fill placeholders”);
 // register listener for warning events generated by aspose document processor
 template.setWarningCallback(new HandleDocumentWarnings());
 // remove placeholders name when the field is unused
 template.getMailMerge().setCleanupOptions(REMOVE_UNUSED_REGIONS | REMOVE_UNUSED_FIELDS | REMOVE_EMPTY_PARAGRAPHS);
// merge specific fields like booleans or html
 // here to insert
 template.getMailMerge().setFieldMergingCallback(new HandleMergeField());

If i add this : template.setAutomaticallyUpdateStyles(true); to the line 6 it will fix the issue ?

Is there someone who can suggest something ? wthe need is quite urgent please!

@MDAKNOU You can specify HtmlInsertOptions.USE_BUILDER_FORMATTING option. In this case font and paragraph formatting specified in DocumentBuilder will be used as base formatting for text inserted from HTML.

builder.insertHtml((String)args.getFieldValue(), HtmlInsertOptions.USE_BUILDER_FORMATTING);

Hello @alexey.noskov

Yes i use this option of use builder , it functionne but for examle when i put in my html a part with balise b code and if the format policy placeholder in my document is not defined as bold format , i get “code” not in bold format .

@MDAKNOU Could you please create a sample template, provide sample input data and attach current and expected output documents? We will check the issue and provide you more information.

Hello @alexey.noskov ,

yes it s working now for me ! Thanks for your reply!

1 Like