While merging document causing problem in last line alignment issue and data movement issue

I have multiple documents and I want to merge it, In that new document will be append from new page for that I used this code snippest.In Merge document i got last line alignment issue and some of data getting moved from expected position .

Aspose.Words.License licWord = new Aspose.Words.License();
string strLicenscePath = "D:\\DemoCodes\\ConsoleApp\\MergingWordDocument\\License\\Aspose.Wordsfor.NET.lic";
licWord.SetLicense(strLicenscePath);
Aspose.Words.Document mainDoc = new Aspose.Words.Document();
mainDoc.RemoveAllChildren();
byte[] bytesectionData;
Document result = null;
for (int i = 1; i < 20; i++)
{
    bytesectionData = File.ReadAllBytes(@"C:\\Users\\ABC\\Desktop\\vilkiskuie\\sec-" + i + ".docx");
    Aspose.Words.Document doc = new Aspose.Words.Document(new MemoryStream(bytesectionData));
    doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;

    if (result == null)
        result = doc;
    else
        result.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);


}

result.Save("C:\\Users\\ABC\\Desktop\\NewOutput.docx");

I attached Error Output here.
DataMovement1.png (121.1 KB)
Error and Expected OutPut last Line Alignment.png (90.6 KB)

My Diffrent word file
sec-1.docx (2.4 MB)
sec-2.docx (2.4 MB)
sec-3.docx (23.7 KB)
sec-4.docx (29.6 KB)
sec-5.docx (55.2 KB)
sec-6.docx (2.4 MB)
sec-7.docx (2.5 MB)
sec-8.docx (22.4 KB)
sec-9.docx (2.4 MB)
sec-10.docx (2.5 MB)
sec-11.docx (2.6 MB)
sec-12.docx (108.7 KB)
sec-13.docx (4.2 MB)
sec-14.docx (86.8 KB)
sec-15.docx (226.2 KB)
sec-16.docx (2.4 MB)
sec-17.docx (51.9 KB)
sec-18.docx (2.4 MB)
sec-19.docx (2.4 MB)

Here is my out put file
Vilkyskiu Testing 25-04_6_12_2023 12_41_41 PM.docx (1.5 MB)

I attached sample output image (error) .

1 Like

@Jayshiv1408 could you kindly provide the source and destination documents so that I can accurately reproduce the issue you are facing?

@Jayshiv1408 Could you please specify the pages on which you are observing the discrepancies? Since the final document has more than 120 pages, it would be helpful to know the specific pages where you are experiencing the issue.

@Jayshiv1408 Please note that the current result you are getting for the “Last Line Alignment” issue is expected behavior. If you perform the same operation using the MS Word application, you will obtain the same result:
sec-9-newSecitonAdded-wordOutput.docx (2.4 MB)

As a workaround for this issue, you can insert an empty paragraph at the end of the document. This can be achieved using the following code snippet:

var doc = new Document();
doc.RemoveAllChildren();
for(var i = 1; i < 20; i++) 
{
    var temp = new Document($@"C:\Temp\sections\sec-{i}.docx");
    temp.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;

    if(temp.LastSection.Body.LastChild.NodeType == NodeType.Paragraph)
    {
        var para = new Paragraph(temp);
        temp.LastSection.Body.AppendChild(para);
    }

    doc.AppendDocument(temp, ImportFormatMode.KeepSourceFormatting);
}

doc.Save(@"C:\Temp\output.docx");

By adding the empty paragraph, you can ensure the last line of text aligns properly in the document.

Please let me know if you have any further questions or concerns.

Hi, @eduardo.canal When I Merge document that time in sec-14.docx (In sec-14 data is on Page no 1) Data getting moved from one page to other page
In Output file You can See that data should be on page no 83 of 154 but its moved to page no 84 on 154.

Here is Screen shot And Document
Output.png (63.9 KB)

I already uploaded sec14.docx and here is my output
output.docx (1.1 MB)

Here on Page no 89 in output file I get data movement i upload file Snapshot its also in Sec14.docx(page no 7& 8)
Output.png (123.0 KB)

There are Other data movement is there in document so you need that then I can share more information for your help.

Thanks

@Jayshiv I am afraid, it is not always possible to preserve original document layout after merging documents. As you may know MS Word documents are flow documents and the previous content affects layout of the following content.

PS: With the latest version you can use Merger class to merge documents:

string[] documents = new string[19];
for (int i = 0; i < 19; i++)
    documents[i] = $@"C:\Temp\sec-{i + 1}.docx";

Aspose.Words.LowCode.Merger.Merge(@"C:\Temp\out.docx", documents, SaveFormat.Docx, Aspose.Words.LowCode.MergeFormatMode.KeepSourceFormatting);