Replace pages in Main document

Hello Aspose Support,

I would like to programmatically replace the last n pages of a Word document (Document A) with n pages from another document (Document B) using Aspose.Words.

My requirements are:

  1. The replaced pages from Document B should retain their original formatting, styles, and comments.
  2. The header/footer from Document A should be retained or applied to the appended pages.
  3. The overall document formatting should remain consistent after the replacement.

Could you please provide a simple sample code or best approach to achieve this using Aspose.Words?

@Vaibhavi_Lad

To replace the last n pages of a Word document (Document A) with n pages from another document (Document B) while retaining formatting, styles, and comments, follow these steps using Aspose.Words:

Cause:

You need to update Document A by replacing its last n pages with content from Document B, ensuring that the formatting and styles remain intact.

Solution:

  1. Load both documents: Load Document A and Document B into Document objects.
  2. Extract the last n pages from Document A: Use the ExtractPages method to get the last n pages.
  3. Remove the last n pages from Document A: Clear the content of Document A to prepare for the new content.
  4. Insert pages from Document B: Use the AppendDocument method to append the pages from Document B to Document A.
  5. Retain headers/footers: Ensure that the headers and footers from Document A are applied to the newly inserted pages.

Next Steps:

Here’s a sample code snippet to illustrate this process:

using Aspose.Words;

public void ReplaceLastNPages(string documentAPath, string documentBPath, int n)
{
    // Load Document A
    Document docA = new Document(documentAPath);
    
    // Load Document B
    Document docB = new Document(documentBPath);
    
    // Calculate the starting page index for extraction
    int totalPagesA = docA.PageCount;
    int startPageIndex = totalPagesA - n;

    // Extract the last n pages from Document A
    Document lastNPages = docA.ExtractPages(startPageIndex, n);
    
    // Remove the last n pages from Document A
    docA.RemoveAllChildren();
    
    // Append Document B to Document A
    docA.AppendDocument(docB, ImportFormatMode.KeepSourceFormatting);
    
    // Append the extracted pages back to Document A
    docA.AppendDocument(lastNPages, ImportFormatMode.KeepSourceFormatting);
    
    // Save the modified Document A
    docA.Save("ModifiedDocumentA.docx");
}

Closing Line:

This method effectively replaces the specified pages while maintaining the integrity of the document’s formatting. If you have any further questions or need additional assistance, feel free to ask!

Hello Aspose Team,

Thank you for the response and the sample code.

However, I would like to clarify my actual requirement and raise a follow-up:

Your sample code is appending the entire Document B and then re-appending the last N pages of Document A — this seems to result in extra pages, and it doesn’t actually replace the last N pages of Document A.

My exact requirement:

  1. Replace the last N pages of Document A with Document B (which has N pages).
  2. Preserve formatting, styles, and comments of both Document A and Document B.
  3. Apply the headers/footers from Document A (last section) to the appended Document B pages.
  4. Ensure the output is consistent with no loss of layout or unnecessary blank pages.

Key Issues in the Provided Code:

  • docA.RemoveAllChildren() removes the entire content of Document A instead of only the last N pages.
  • Appending both docB and lastNPages ends up adding more pages, not replacing.
  • Headers/footers from Document A are not being applied to the appended Document B content.

Could you please provide a corrected and minimal sample code that:

  • Removes only the last N pages from Document A (not the whole document),
  • Appends Document B in place,
  • Applies headers from Document A to the appended pages from Document B,
  • And retains styles and comments from both documents?

Thank you for your continued support.

@Vaibhavi_Lad To meat your requirements you should extract PageCount-N pages from DocumentA and then append DocumentB to the extracted pages. Something like this:

Document docA = new Document(@"C:\Temp\A.docx");
Document docB = new Document(@"C:\Temp\B.docx");

Document firstPagesA = docA.ExtractPages(0, docA.PageCount - docB.PageCount);
firstPagesA.AppendDocument(docB, ImportFormatMode.KeepSourceFormatting);

firstPagesA.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.