Inserting html in DocumentBuilder does confusing stuff

Hello
I have an Aspose.Words tool that should render html from the data to field in a PDF file.
This works.
But if I enclose the mergefield in an If-clause with the same field in the if as I want to render, it only renders the second word (as far as I can tell this only happens in I check the same field I want to render).
I included the java code the docx file and an output pfdtest.zip (397.3 KB)
.
Best,
Peter Binnig

@p.binnig The problem occur because you use DocumentBuilder.moveToMergeField in your IFieldMergingCallback implementation. You should use DocumentBuilder.moveToField instead. Please see the following code:

class ShapeSetFieldMergingCallback implements IFieldMergingCallback {
    @Override
    public void fieldMerging(FieldMergingArgs args) throws Exception {
        String code = args.getField().getFieldCode();
        if (code.contains("\\b html")) {
            com.aspose.words.DocumentBuilder builder = new com.aspose.words.DocumentBuilder(args.getDocument());
            builder.moveToField(args.getField(), true);
            Object html = args.getFieldValue();
            if (html != null) builder.insertHtml( (String) html);
            args.setFieldValue("");
        }
    }

    @Override
    public void imageFieldMerging(ImageFieldMergingArgs imageFieldMergingArgs) throws Exception {
        // Implementation is not required.
    }
}
1 Like