InsertDocument custom margins issue

When i use DocumentBuilder’s InsertDocument builtin method , if the inserted document has bespoke margins their format won’t be copied on the final document, and i specifically use the 2nd parameter ImportFormatMode.KeepSourceFormatting

@Remus87

When using the InsertDocument method of the DocumentBuilder class in Aspose.Words, if you are experiencing issues with custom margins not being preserved in the final document, even when specifying ImportFormatMode.KeepSourceFormatting, it may be due to how the method handles section formatting.

The InsertDocument method is designed to mimic the behavior of copying and pasting content in Microsoft Word. However, it may not always retain all formatting aspects, such as custom margins, especially if the inserted document has different section settings than the destination document.

To address this issue, you can try the following approaches:

  1. Use ImportFormatOptions: You can specify additional options using ImportFormatOptions to control how styles and formatting are handled during the insertion. For example, you can enable SmartStyleBehavior to help manage style clashes more effectively.

    Here’s a code snippet demonstrating how to use ImportFormatOptions:

    Document mainDoc = new Document("MainDocument.docx");
    Document subDoc = new Document("SubDocument.docx");
    
    DocumentBuilder builder = new DocumentBuilder(mainDoc);
    builder.MoveToDocumentEnd();
    
    ImportFormatOptions options = new ImportFormatOptions();
    options.SmartStyleBehavior = true; // Enable smart style behavior
    
    builder.InsertDocument(subDoc, ImportFormatMode.KeepSourceFormatting, options);
    mainDoc.Save("FinalDocument.docx");
    
  2. Manually Adjust Margins: After inserting the document, you may need to manually adjust the margins of the sections in the final document to match those of the inserted document. You can access the PageSetup property of the sections to set the margins accordingly.

  3. Check Section Settings: Ensure that the section settings of both the main document and the inserted document are compatible. If they differ significantly, it may lead to unexpected formatting results.

If these suggestions do not resolve the issue, consider reviewing the documentation for InsertDocument and ImportFormatOptions for more detailed information on how to manage formatting during document insertion.

For further assistance, you may also want to check related discussions in the Aspose community forums, as other users may have encountered similar issues and found solutions.

Sources:
[1]: DocumentBuilder.InsertDocument | Aspose.Words for .NET

@Remus87 This is an expected behavior. When you use InsertDocument method, section from the original document is not preserved, only content is inserted into the current section in the document. So in this case section page setup is not preserved. In your case it would be more correct to use AppendDocument method to preserve section from the source document. For example see the following code:

Document dst = new Document(@"C:\Temp\dst.docx");
Document src = new Document(@"C:\Temp\src.docx");
dst.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);
dst.Save(@"C:\Temp\out.docx");

@alexey.noskov Thank you for the explanation.
We’re using AppendDocument for other part of the reports, but there is a case scenario where we’re specifically using InsertDocument because of the order of inserting the documents (append is adding them always at the start of the doc, and Insert at the point specified, and we need that flexibility just for this use case).
So, i was just wondering if maybe adding an instance of ImportFormatOptions (the 3rd parameter of the InsertDocument) with a combination of its properties such as ForceCopyStyles or/and SmartStyleBehaviour to achieve preserving the margins of the inserted doc.
Or any other alternative solution is welcomed if exists

@Remus87 In your case you can split the target document at the specific node with section break and then insert sections from source document between the section in the target document. For example see the following code:

Document dst = new Document(@"C:\Temp\dst.docx");
DocumentBuilder builder = new DocumentBuilder(dst);

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

// Move document builder to the destination node. For demonstration purposes use bookmark.
builder.MoveToBookmark("test");
// Insert section break.
builder.InsertBreak(BreakType.SectionBreakContinuous);
// Insert section from the source document before the current section.
foreach(Section srcSect in src.Sections)
{
    Node importedSection = dst.ImportNode(srcSect, true, ImportFormatMode.KeepSourceFormatting);
    builder.CurrentSection.ParentNode.InsertBefore(importedSection, builder.CurrentSection);
}

dst.Save(@"C:\Temp\out.docx");

Thanks for the solution provided. will try it tomorrow and update here

1 Like