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

Free Support Forum - aspose.com

Last line with justify during mail merge

Hi,

If a document has the last line justified it spreads across the entire line. This happens during mail merge only and on all pages, except for the last.

See attachment for input docx with 2 justified lines and the saved pdf for the single and the mail merged pdf outputs.

See below code (using Java aspose-words 18.4.) to reproduce the bug:

package aspose.bug;

import com.aspose.words.Document;
import com.aspose.words.IMailMergeDataSource;
import com.aspose.words.License;
import com.aspose.words.ref.Ref;

import java.io.FileInputStream;

public class JustifyBug {

    public static void main(String... args) {
        try {
            String dir = "C:\\temp\\aspose\\";
            new License().setLicense(new FileInputStream(dir + "Aspose.Words.lic"));
            Document doc = new Document(dir + "doc-with-justify.docx");
            doc.save(dir + "doc-with-justify.pdf");
            doc.getMailMerge().execute(new IMailMergeDataSource() {
                int i=0;
                public String getTableName() throws Exception {
                    return "mock";
                }
                public boolean moveNext() throws Exception {
                    return i++ < 3;
                }
                public boolean getValue(String s, Ref<Object> ref) throws Exception {
                    return false;
                }
                public IMailMergeDataSource getChildDataSource(String s) throws Exception {
                    return null;
                }
            });
            doc.save(dir + "doc-with-justify-mail-merge.pdf");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

last-line-justify-mailmerge-bug.zip (36.7 KB)

I just tested this in MS Word 365 and it exhibits the same weird behaviour, but it still seem like a bug as it is certainly not what the user would expect.

doc-with-justify-vs-mailmerge-word365.png (46.1 KB)

@dcalde,

Thanks for your inquiry. Please note that Aspose.Words mimics the behavior of MS Word. If you perform the mail merge using MS Word, you will get the same output. Moreover, if you check the alignment of paragraph in MS Word, it is justify.

I understand, but that doesn’t make it any better and doesn’t explain why the page looks different during mail merge vs regular. It is un-expected behaviour to the user.

However assuming that this is considered feature-parity with word, rather than a bug, can you please advise if there is a programmatic way to prevent the last line from spreading across the entire page. We are dealing with user uploaded templates and I don’t want to produce any unexpected or undesirable results.

If it cannot be prevented, can it be detected programmatically so I can alert the user?
Thanks.

@dcalde,

Thanks for your inquiry. You can achieve your desired output using one of the following solution. Hope this helps you.

Solution 1:

for (Section section : doc.getSections())    
  section.getBody().getLastParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);

doc.updatePageLayout();
doc.save(MyDir + "output.docx");
doc.save(MyDir + "output.pdf");

Solution 2:

doc.getCompatibilityOptions().setDoNotExpandShiftReturn(true);
doc.save(MyDir + "output.docx");
doc.save(MyDir + "output.pdf");

Thanks.
I am going to use setDoNotExpandShiftReturn(true) as it seems to have the least potential to impact other parts of the document

@dcalde,

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.