Change the mergeFields value to an html text

I’m using Aspose Word for Java v11 with ms word 2010 and I want to change the mergeFields value to an html text, my Source code is like this:

builder.moveToMergeField(“test”);

builder.insertHtml(" Hello word");

The problem is I want the mergefields still on the same format (font, size,color,style…), so can you help me please ?

This message was posted using Email2Forum by simon.bell.

Hi Yasser,

Thanks for your inquiry. Please check my reply at following forum link.

https://forum.aspose.com/t/49139

Hi,

Thanks for your inquiry. I think, you can use the following code snippet to be able to merge the formatting that is specified inside Html string with the formatting of MergeField:

Document doc = new Document(@"C:\test\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToMergeField("mf", false, false);
InsertHtmlWithBuilderFormatting(builder, " Hello word");
// Just to remove the mergefield
builder.MoveToMergeField("mf");
doc.Save(@"C:\test\out.docx");

Please find attached the InsertHtmlWithBuilderFormatting routine with this post.

I hope, this helps.

Best Regards,

Hi Awais,

Thanks for your replay, but i’m programming with Java, can you attach the java code of “insertHtmlWithBuilderFormatting”.

Best regards,

Hi Yasser,

Thanks for your request. I have attached the Java equivalent of InsertHtmlWithBuilderFormatting here with this post. Please try using the following code snippet:

Document doc = new Document("C:\\Temp\\mf.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToMergeField("mf", false, false);
DocumentBuilderHelper helper = new DocumentBuilderHelper(builder);
helper.insertHtmlWithBuilderFormatting("formatted html string");
// Just to remove the mergefield
builder.moveToMergeField("mf");
doc.save("C:\\Temp\\outJava.docx");

I hope, this helps.

Best Regards,

Great work!

Thank you Awais.

Hi Yasser,

Thanks for your feedback. Please let us know any time you have any further queries. We’re always glad to help you.

Best regards,

can you share the DocumentBuilderHelper here?

@zwei I am afraid attachments were lost after forum migration. But if you need simply use DocumentBuilder’s formatting upon inverting HTML, you can use the following overload:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/inserthtml/#inserthtml_2

Thanks for your reply, actually we have tried

public void InsertHtml(string html, bool useBuilderFormatting)

But our program still doesn’t work as expected. We use following code to insert HTML into MergeField

final Document doc = new Document(template);
final DocumentBuilder builder = new DocumentBuilder(doc);
...
builder.moveToMergeField(fieldKey, false, false);
builder.insertHtml(HTMLContent,true);
builder.moveToMergeField(fieldKey);
...
doc.save(targetDocx);

Here it is the template Docx file:
MatthiasMergeField.docx (38,7 KB)

Here it is the Key:HTMLContent, the key is "VERTRAGSTEXT ", as you can see, the HTML Content is very long:
keyAndHTMLContent.docx (14,8 KB)

Here it is the expected result:
MergeFieldSollErgebnis.pdf (154,1 KB)

However our actual result looks so:
MergeFieldIstErgebnis.pdf (91,3 KB)

The problem is, in the Word Template file, there are defined formats:

However, such defined formats get lost…

@zwei Thank you for additional information. I am afraid, there is no way to produce the expected output by inserting HTML into the document. first of all structure of your HTML is the following:

<ol>
    <li style="text-align: center;">first level</li>
    <ol>
        <li>second level</li>
        <li>second level</li>
    </ol>
</ol>
<ol>
    <li style="text-align: center;">first level</li>
    <ol>
        <li>second level</li>
        <li>second level</li>
    </ol>
</ol>

As you can see the first levels are defined as a separate lists, so in the output numbering is restarted for each first level item.

It would be easier to create the required list using Aspose.Words API than by inserting HTML:
https://docs.aspose.com/words/net/working-with-lists/

Thanks for your reply!

1 Like

Actually I am seeking something like this. Even if the first levels are defined as a separate lists, the numbering style still should be applied.

https://forum.aspose.com/t/apply-custom-paragraph-numbering-style-to-generate-numbered-list-items-in-word-document-using-java/229772/4

@zwei You can achieve this by postprocessing the nodes inserted by DocumentBuilder. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Use node changing callback to collect the inserted paragraphs.
InsertedParagraphsCollector callback = new InsertedParagraphsCollector();
doc.setNodeChangingCallback(callback);

builder.moveToMergeField("$VERTRAGSTEXT");
// Add the current document builder's paragraph.
callback.getInsertedParagraphs().add(builder.getCurrentParagraph());
builder.insertHtml(readStream(new FileInputStream("C:\\Temp\\in.html")));

for (Paragraph p : callback.getInsertedParagraphs())
{
    if (p.isListItem())
    {
        if (p.getListFormat().getListLevelNumber() == 0)
        {
            p.getListFormat().removeNumbers();
            p.getParagraphFormat().clearFormatting();
            p.getParagraphFormat().setStyleName("Paragraph");
        }
        if (p.getListFormat().getListLevelNumber() == 1)
        {
            p.getListFormat().removeNumbers();
            p.getParagraphFormat().clearFormatting();
            p.getParagraphFormat().setStyleName("Absatz");
        }
    }
}

doc.save("C:\\Temp\\out.pdf");
private static class InsertedParagraphsCollector implements INodeChangingCallback
{
    public ArrayList<Paragraph> getInsertedParagraphs()
    {
        return mInsertedParagraphs;
    }
        
    @Override
    public void nodeInserting(NodeChangingArgs args) throws Exception {
    }
    
    @Override
    public void nodeInserted(NodeChangingArgs args) throws Exception {
        if(args.getNode().getNodeType() == NodeType.PARAGRAPH)
        {
            Paragraph para = (Paragraph)args.getNode();
            mInsertedParagraphs.add(para);
        }
    }
    
    @Override
    public void nodeRemoving(NodeChangingArgs args) throws Exception {
    }
    
    @Override
    public void nodeRemoved(NodeChangingArgs args) throws Exception {
    }
        
    private ArrayList<Paragraph> mInsertedParagraphs = new ArrayList<Paragraph>();
}
1 Like

Thank you very much, that is exactly what I wanted.

1 Like