Appending Document Not Maintaining Formatting

I am encountering an inconsistent formatting issue when appending Word document templates using Aspose.Words in C#. The issue has been reproduced in isolation using a minimal test case (provided below), and a sample document is attached for your investigation.

When appending a .docx template into a new Word document using Document.AppendDocument(...), certain elements (such as checkboxes) cause unexpected text alignment issues.

Specifically, text near checkboxes is forced to the end of the line, as if the checkbox or its containing element is expanding to fill the line width. This behavior occurs even with a minimal, unstyled document, and without any headers, footers, or complex formatting in either the source or destination document.

The issue is not consistent across all content, but reproducible with the attached example document.

Blood Sample Collection.docx (19.0 KB)

Code to Reproduce the Issue:

public void CombineWordDocument()
{
    string projectDirectory = Directory.GetCurrentDirectory();
    string filePath = Path.Combine(projectDirectory, "TestFile", "Blood Sample Collection.docx");

    Aspose.Words.Document swDoc = new Aspose.Words.Document();
    DocumentBuilder builder = new DocumentBuilder(swDoc);

    // Setup document dimensions
    Section section = builder.CurrentSection;
    section.PageSetup.HeaderDistance = ConvertUtil.MillimeterToPoint(12);
    section.PageSetup.DifferentFirstPageHeaderFooter = true;

    // Move to start of the document
    builder.MoveToDocumentStart();

    // Load the template
    Aspose.Words.Document template = new Document(filePath);

    // Append the template document
    swDoc.AppendDocument(template, ImportFormatMode.KeepDifferentStyles);

    // Re-apply margins to ensure consistency
    foreach (Section sec in swDoc.Sections)
    {
        sec.PageSetup.FooterDistance = ConvertUtil.MillimeterToPoint(3);
        sec.PageSetup.BottomMargin = ConvertUtil.MillimeterToPoint(3);
    }

    // Save merged document for inspection
    string tempPath = Path.Combine(Path.GetTempPath(), "Merged Blood Sample Collection.docx");
    swDoc.Save(tempPath);
}

Expected Behavior:

The checkbox and surrounding text should maintain their original formatting and alignment, just as they appear in the source document.

Actual Behavior:

Text near certain checkboxes is pushed to the end of the line, as if a tab, alignment override, or layout issue is applied during or after insertion.

@OpenDevSolutions

It seems you’re experiencing formatting issues when appending a Word document using Aspose.Words, particularly with checkboxes affecting text alignment. This can occur due to how the layout is handled during the append operation.

To address this, you can try the following approaches:

  1. Check the Source Document: Ensure that the source document (the one being appended) does not have any hidden formatting or styles that could affect the layout. Elements like checkboxes can have properties that influence surrounding text.

  2. Adjust Alignment After Appending: After appending the document, iterate through the paragraphs and adjust their alignment or formatting as needed. For example:

    foreach (Section section in swDoc.Sections)
    {
        foreach (Paragraph paragraph in section.Body.Paragraphs)
        {
            // Adjust alignment or other properties as needed
            paragraph.ParagraphFormat.Alignment = ParagraphAlignment.Left; // or any desired alignment
        }
    }
    
  3. Use ImportFormatMode: You are currently using ImportFormatMode.KeepDifferentStyles, which is a good choice. However, you might want to experiment with other modes like ImportFormatMode.UseDestinationStyles to see if it resolves the issue.

  4. Inspect Checkbox Properties: If the checkboxes are causing issues, check their properties in the source document. You may need to adjust their layout or positioning.

  5. Update Aspose.Words: Ensure you are using the latest version of Aspose.Words, as updates often include bug fixes and improvements.

If the problem persists, consider sharing a minimal reproducible example on the Aspose forums for more targeted assistance.

A minimal reproducible example has been provided in the original post.

Further investigation has made me identify a Tab stop that was causing the append document to have troubles around the checkbox in the sample document.

Is this a expected feature or a bug? I have suggested that the user remove the tab stops around this checkbox to resolve this issue.

@OpenDevSolutions This is an expected behavior. Your source and target document have different sets of compatibility options. To get the same document layout, both source and target documents should have the same set of compatibility options set. If you modify the code like this the problem disappears:

Document doc = new Document();
Document src = new Document(@"C:\Temp\in.docx");

doc.RemoveAllChildren();
doc.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);

doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2019); 
doc.Save(@"C:\Temp\out.docx");

You should note documents might have different compatibility options set, for example, if they were created by different versions of MS Word. Unfortunately, there is no way to keep different compatibility options for different parts of the document. This is simply impossible due to the MS Word document structure and there are no option which can resolve this. After merging several documents all documents parts will use compatibility options set in the main target document and this might affect the appended document layout.
In addition, as you may know, MS Word documents are flow by their nature and there is no “page” concept. The consumer applications reflow the content of the document into pages on the fly and each added or removed content affects the document layout. This applies to document merging too.

Is there a easy way to detect the compatibility version of the document before attempting to merge? Is it safe enough to just OptimizeFor on a specific version? Is it preferred to optimize upwards or pick the earliest version of the combined templates?

@OpenDevSolutions If the document was create by MS Word, you can use BuiltInDocumentProperties.Version property. This property represents the version number of the application that created the document. Following are the version numbers for MS Word.

11 = Word 2003
12 = Word 2007
14 = Word 2010
15 = Word 2013
16 = Word 2016
Document doc = new Document();
int MSWVersion = doc.BuiltInDocumentProperties.Version >> 16;
Console.WriteLine(MSWVersion);

CompatibilityOptions.OptimizeFor method writes compatibility options that corresponds the specified MS Word version.

I have also noticed that when appending the document, the font in some areas changes from the original document.

For example in the original document provided “Blood Sample Collection” is “Times New Roman”. When it is merged it becomes “Aptos”.

Iv noticed that their document provided has a line at the end of it where the font is set as Aptos but I wouldn’t of expected the first line in the file to become that font.

This seems to be the result of using ImportFormatMode.KeepDifferentStyles. If I switch it to UseDestinationStyles it turns back to Times New Roman, but I believe that is due to it just being the default.

This could result in losing styles in some situations I imagine?

@OpenDevSolutions This might occur due to styles used in original and destination documents and import format mode used. Please see our documentation to learn more about differences in import format modes:
https://reference.aspose.com/words/net/aspose.words/importformatmode/

Aspose.Words mimics MS Word behavior when import content from one document to another.

Its looking like simply optimizing for 2019 is resolving the variations of fonts while still maintaining the original templates fonts.

Thanks for your help.

@OpenDevSolutions Please feel free to ask in case of any further issues. We are always glad to help you.