Insert Word Document in DOCX File during Mail Merge & Retain Page Setup, Orientation (Java)

Hi Aspose Team!

I am trying to implement a document merging functionality and everything works fine till now, but at now I am trying to solve my requirement around page orientation of the imported document.

So, I have a document with mergefields and some of them points to another word documents that should be inserted at their location, this can be achieved easily thanks to your API, I implemented a custom IFieldMergingCallback, and I am calling there the DocumentBuilder’s insertDocument function that places the document there. This is working fine.
Unfortunately the original page orientation of the inserted document has not been kept and it seems there is no direct parameter that I could specify either to the DocumentBuilder object or directly to the insertDocument call (the ImportFormatNode does not influence this behaviour).
I tried to set the orientation through the pagesetup object (side note: despite the fact that my inserted document has landscape orientation, the aspose api reads landscape orientation out from its first section)

I also tried to replace the insertDocument with this code, without success.
I also tried to apply the same as you suggested in this topic.

Have you any suggestion how should I do this?
Thank you in advance,
Best Regards,
Jozsef

@m4gic,

The following code of latest 21.5 version of Aspose.Words for Java should insert a document at merge field’s position in another Word document:

Document doc = new Document("C:\\Temp\\input.docx");
Document subDoc = new Document("C:\\Temp\\mixed orientation.docx");

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

doc.save("C:\\Temp\\awjava-21.5.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) {

    }
}

In case the problem still remains, then please ZIP and attach the following resources here for testing:

  • Your simplified source Word documents
  • Aspose.Words for Java 21.5 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document manually by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your issues and provide you more information.

Dear @awais.hafeez,

Unfortunately I am using already the suggested version and the FieldMergingCallback as you wrote.
I attached a simplified maven project with a single junit test that runs my code on the simplified documents to show you my problem.

aspose-mailmerge-doc-orientation.zip (164.4 KB)

Thank you very much for your time and effort in advance.

Best Regards,
Jozsef

@m4gic,

We have managed to reproduce this issue on our end by using the following simple code:

Document doc = new Document("C:\\Temp\\aspose-mailmerge-doc-orientation\\template.docx");
Document subDoc = new Document("C:\\Temp\\aspose-mailmerge-doc-orientation\\attachment1.docx");
Document subDoc2 = new Document("C:\\Temp\\aspose-mailmerge-doc-orientation\\attachment2.docx");

doc.getMailMerge().setFieldMergingCallback(new HandleMergeField());
doc.getMailMerge().execute(new String[]{"Attachment[0]#", "Attachment[1]#"}, new Object[]{subDoc, subDoc2});

doc.save("C:\\Temp\\awjava-21.5.docx");

private static class HandleMergeField implements IFieldMergingCallback {
    public void fieldMerging(FieldMergingArgs args) throws Exception {
        if (args.getFieldName().equals("Attachment[0]#") || args.getFieldName().equals("Attachment[1]#")) {
            DocumentBuilder builder = new DocumentBuilder(args.getDocument());
            builder.moveToMergeField(args.getFieldName());

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

    public void imageFieldMerging(ImageFieldMergingArgs args) {

    }
}

But, can you please also ZIP and attach your expected Word DOCX document showing the desired output here for our reference? You can create this document manually by using MS Word.

P.S. attachment1.docx and attachment2.docx both have Portrait orientation and custom Paper Sizes

Hello,

First of all, thank you for your answer.
Albeit your suggestion did not work for me, it led me to a solution, that I would like to share, hopefuly it will help to someone in the same shoes. My IFieldMergingCallback differs a bit:

    private class HandleMergeField implements IFieldMergingCallback
    {

        private final int importFormatMode;

        public HandleMergeField(int importFormatMode)
        {
            this.importFormatMode = importFormatMode;
        }

        @Override
        public void fieldMerging(FieldMergingArgs args) throws Exception
        {
            if (args.getFieldName().equals("Attachment[0]#") || args.getFieldName().equals("Attachment[1]#"))
            {
                DocumentBuilder builder = new DocumentBuilder(args.getDocument());
                builder.moveToMergeField(args.getFieldName());
                Document document = (Document) args.getFieldValue();
                // if there is a difference, then store it as we will modify the template
                boolean hasDiff = hasDifferentOrientationOrWidth(builder.getCurrentSection(), document.getFirstSection());
                // copy page setup (width & orientation)
                copyPageSetup(builder, document);
                // insert document
                builder.insertDocument(document, importFormatMode);
                // if there was a diff, insert a section break too
                if (hasDiff)
                {
                    builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
                }
            }
        }

        private void copyPageSetup(DocumentBuilder builder, Document documentToMerge)
        {
            PageSetup documentToMergePageSetup = documentToMerge.getFirstSection().getPageSetup();
            PageSetup templateCurrentSectionPageSetup = builder.getCurrentSection().getPageSetup();
            templateCurrentSectionPageSetup.setOrientation(documentToMergePageSetup.getOrientation());
            templateCurrentSectionPageSetup.setPageHeight(documentToMergePageSetup.getPageHeight());
            templateCurrentSectionPageSetup.setPageWidth(documentToMergePageSetup.getPageWidth());
        }

        private boolean hasDifferentOrientationOrWidth(Section templateSection, Section documentToMergeSection)
        {
            if (templateSection.getPageSetup().getOrientation() != documentToMergeSection.getPageSetup().getOrientation())
            {
                return true;
            }
            if (templateSection.getPageSetup().getPageWidth() != documentToMergeSection.getPageSetup().getPageWidth())
            {
                return true;
            }
            return false;
        }

        @Override
        public void imageFieldMerging(ImageFieldMergingArgs args)
        {

        }
    }

Best Regards,
m4gic

@m4gic,

It is great that you were able to find what you were looking for and thanks for sharing your solution which may also help other developers who are looking to solve similar problem.