We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Losing format after doing mail merge

Hi y’all,

I’m working with the Aspose and facing these problems:

  • Style applied on the merge fields all lost after merging
    2019-04-11_09h54_00.png (71.6 KB)

  • Then, i tried with another approach by removing the MERGEFIELD keyword inside field code, the applied style can work properly but not so really. Merge fields that had been removed these keywords piled up on top of the document:
    2019-04-11_10h02_35.png (82.9 KB)

This is my code to execute the mail merge process:

public static void generateMailMergePdfFromJson(Document template, String json, String outputFilePath) throws ParserConfigurationException, SAXException, IOException, Exception {
//Remove merge field with empty value and if the row are empty=> remove paragraph
template.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
| MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS
| MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
| MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS);

    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode jsonNode = objectMapper.readTree(json);
    
    ArrayNode arrayNode = objectMapper.createArrayNode();
    arrayNode.add(jsonNode);
    
    JsonNode rootNode = objectMapper.createObjectNode();
    
    ((ObjectNode) rootNode).set(Dictionary.ROOT_TAG_NAME, arrayNode);

    //To call custom merge data with html code
    template.getMailMerge().setFieldMergingCallback(new HandleMergeFieldInsertHtml());

    // Add main merge field based of main root tag name
    template.accept(new TableTagHandler(Dictionary.ROOT_TAG_NAME));
    // Merge fields
    template.getMailMerge().executeWithRegions(new JsonMailMergeDataSet(new JsonDocument(rootNode, Dictionary.ROOT_TAG_NAME)));
    
    template.save(outputFilePath, SaveFormat.PDF);
}

Anyone has a taste for this issue, please help me.

@gialoui,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words 19.4 generated output document showing the undesired behavior
  • Your expected document showing the correct output. You can create expected document by using MS Word.
  • Please also create a simplified standalone Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.