Free Support Forum - aspose.com

Table of contents shrinks after mail-merge

Hello,

Our application consumes a word document with mail merge fields, performs the merge and then outputs the resulting document as a PDF. However we’re experiencing unexpected behavior after the merge, where the table of contents is squashed to about 2/3 of the original width.

Example snippet

....
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
String [] mergeFields = document.getMailMerge().getFieldNames();
String [] values = new String[mergeFields.length];

for(int i =0;i<mergeFields.length;i++){
values[i] = “XXXXXXXX”;
}
document.getMailMerge().execute(mergeFields, values);
document.save(outStream,SaveFormat.PDF);

Further Details:

  • We’re using the Java library
  • If I skip the mail merge process, the TOC appears correctly.
  • merge fields are not in any headings

edit
As requested i’ve uploaded a simple maven project
the input docx file and an example PDF of the output we’re getting.

NB: There isn’t an expected output docx file, we don’t re-save it as a word document, just a PDF. So it should look the same as the input file

support_documents.zip (6.6 MB)

@MRPTristan

Thanks for your inquiry. Please call Document.UpdatePageLayout and Document.UpdateFields method before saving the document. If you still face problem, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

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.

PS: To attach these resources, please zip and upload them.

Thanks for your quick reply. I’ve edited the original post to include the documents you requested

@MRPTristan

Thanks for sharing the detail. The right indent of the paragraph that has TOC field is 113.4 pt. Please check the attached image for detail. Please set it to 0 pt to get the desired output.

Document document = new Document(MyDir + "demo_1.docx");

for (Field field : document.getRange().getFields())
{
    if(field.getType() == FieldType.FIELD_TOC)
    {
        field.getStart().getParentParagraph().getParagraphFormat().getStyle().getParagraphFormat().setRightIndent(0.0);
        field.getStart().getParentParagraph().getParagraphFormat().getStyle().getParagraphFormat().setFirstLineIndent(0.0);
        System.out.println(field.getFieldCode());
    }
}

That’s it!. So the problem actually lied with the source document.

Thank you