Merge field does not retain indentation after parsing

Hi,

I have added merge field in the following way :

Here you can see that the merge field is indented. But after I execute MailMerge with Aspose.Words then I get something like following:

Here you can see that if the value of merge field is more than 1 line then the second line starts from beginning of page instead of the set indentation.

@amaurya

Summary
When a MERGEFIELD is replaced with text that contains line‑break characters, Aspose.Words creates a new paragraph for each line. The newly created paragraphs use the default paragraph formatting, so any left‑indent, first‑line indent, or hanging indent that was applied to the original field’s paragraph is not automatically applied to the second and subsequent lines.

Why it happens

  • The mail‑merge engine treats each line‑break (\r, \n or \r\n) as the end of a paragraph.
  • The paragraphs that are inserted for the additional lines inherit the document’s default paragraph style rather than the style of the paragraph that contained the MERGEFIELD.
  • Consequently only the first line keeps the indentation that was set in the template.

What to do next

  1. Keep the original paragraph formatting
  • In the template, make sure the paragraph that contains the MERGEFIELD has the required indentation (left/first‑line/hanging) applied via the Paragraph dialog or a custom style.
  • After the merge, you can re‑apply the same style to the inserted paragraphs if needed, e.g., by iterating over the runs/paragraphs that were added and setting their ParagraphFormat to match the template paragraph.
  1. Use the MERGEFORMAT switch
  • Edit the field code in the template to include the \* MERGEFORMAT switch:
{ MERGEFIELD MyField \* MERGEFORMAT }
  • This switch helps preserve character‑level formatting; it does not affect paragraph‑level indentation but is a good practice for overall formatting consistency.
  1. Post‑merge cleanup (if required)
  • After executing mail merge, walk through the document and locate paragraphs that were inserted for the field value. Apply the same indentation settings as the original template paragraph, for example:
Paragraph templatePara = (Paragraph)doc.getRange().getFields().get(0).getStart().getParentNode();
for (Paragraph p : insertedParagraphs) {
p.getParagraphFormat().setLeftIndent(templatePara.getParagraphFormat().getLeftIndent());
p.getParagraphFormat().setFirstLineIndent(templatePara.getParagraphFormat().getFirstLineIndent());
}
  • The exact code will depend on the language (Java, .NET, etc.) you are using.
  1. Validate with a minimal document
  • Create a small DOCX that contains a single MERGEFIELD with the desired indentation.
  • Run the mail‑merge and verify that all lines keep the indentation. If they do not, apply step 3 to copy the formatting programmatically.
  1. If the issue persists
  • Check the version of Aspose.Words you are using; newer releases may contain options or bug‑fixes related to preserving paragraph formatting during mail merge.
  • Consider reaching out to Aspose support with a minimal reproducible example and the exact library version you are using.

These steps should help you retain the intended indentation for multi‑line merged values.

Note: The answer is based on general behavior of Aspose.Words mail merge and does not rely on any undocumented API that was not present in the provided evidence.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.

@amaurya Could you please attach your sample input and problematic output document along with simple code that will allow us to reproduce the problem? We will check the issue and provide you more information. Unfortunately, it is impossible to analyze the problem by screenshots.