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
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.
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
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)
{
}
}
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.