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

Free Support Forum - aspose.com

Inserting a document inside another document using merge field

hi support,

The use case that i am trying to achieve is,

I will have a Word Template document with sequence of Merge fields, and based on these merge fields i would like to insert documents (which are generated dynamically) in each merge field.

I have gone through the developer resources (https://docs.aspose.com/display/wordsjava/How+to++Insert+a+Document+into+another+Document) and found that

mainDoc.getMailMerge().execute(new String[] { "Document_1" }, new String[] { dataDir + "InsertDocument2.doc" });

In this statement, the second parameter in execute method is String array object, this will not help me adding a document object which is generated dynamically.

Please help me in achieving this use case.

Thanks,

@rgadipalli,

You can meet this requirement by using the following code:

Document doc = new Document("D:\\Temp\\in.docx");

doc.getMailMerge().setFieldMergingCallback(new HandleMergeField());
doc.getMailMerge().execute(new String[] { "mf" }, new Object[] { "D:\\Temp\\in.docx" });

doc.save("D:\\Temp\\Sample\\awjava-18.4.docx");
/////////////////////////////////////////
private static class HandleMergeField implements IFieldMergingCallback {
    public void fieldMerging(FieldMergingArgs args) throws Exception {
        if (args.getFieldName().equals("mf")) {
            DocumentBuilder builder = new DocumentBuilder(args.getDocument());
            builder.moveToMergeField(args.getFieldName());

            builder.insertDocument(new Document(args.getFieldValue().toString()), ImportFormatMode.USE_DESTINATION_STYLES);
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) {

    }
}

hi Awias,

Yes, the new Object[] {“D:\Temp\in.docx”} will work if we have server location, but in our case we have the Document Object generated from DocumentBuilder.

There will be template with all merge fields and each merge field should be replaced by the document which is created from DocumentBuilder.

Please advise how can we achieve this.

Thanks,

@rgadipalli,

In execute method, instead of path array I.e. new Object[] { “D:\Temp\in.docx” }, do you want to pass array of document builder objects i.e. new Object[] { builder1, builder 2, builder 3 … }? Please share complete details of your usecsse.

yes Awais,

I am trying below case

Case 1:
Document mainDoc = new Document(dataDir + “InsertDocument1.docx”); – Main document with list of merge fields
Document subDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(subDoc);

mainDoc.getMailMerge().execute(new String[] { “Document_1” }, new Object[] { subDoc });

Case 2:
Document mainDoc = new Document(dataDir + “InsertDocument1.docx”); – Main document with list of merge fields
Document subDoc = new Document(dataDir + “InsertDocument2.docx”); – Sub document with list of merge fields
subDoc.getMailMerge().setFieldMergingCallback(new InsertDocumentAtMailMergeHandler());
subDoc.getMailMerge().execute(new String[] { “MF1”, “MF2” }, new String[] { “Merge Field1”, “Merge Field2” });

mainDoc.getMailMerge().execute(new String[] { “Document_1” }, new Object[] { subDoc});

In these cases, i am getting below exception.

Exception in thread “main” java.lang.ClassCastException: com.aspose.words.DocumentBuilder cannot be cast to java.lang.String.

Use case, is there will be one master template with list of merge fields, and in each merge field i would like to insert a Document which is generated on demand using document builder or from document template with more merge fields.

Please let me know if this helps.

Thanks,

@rgadipalli,

We are checking this scenario and will get back to you soon.

@rgadipalli,

Please try using the following code. Hope, this helps.

Document doc = new Document("D:\\Temp\\in.docx");
Document subDoc = new Document("D:\\Temp\\input.docx");

doc.getMailMerge().setFieldMergingCallback(new HandleMergeField());
doc.getMailMerge().execute(new String[] { "mf" }, new Object[] { subDoc });

doc.save("D:\\Temp\\awjava-18.4.docx");
////////////////////////////////////////////////////////////////
private static class HandleMergeField implements IFieldMergingCallback {
    public void fieldMerging(FieldMergingArgs args) throws Exception {
        if (args.getFieldName().equals("mf")) {
            DocumentBuilder builder = new DocumentBuilder(args.getDocument());
            builder.moveToMergeField(args.getFieldName());

            builder.insertDocument((Document) args.getFieldValue(), ImportFormatMode.USE_DESTINATION_STYLES);
        }
    }

    public void imageFieldMerging(ImageFieldMergingArgs args) {

    }
}