We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Insert Document adds page break before

Searched all forums to insert one document to another at mergefield with keeping both documents its own formatting. Found solution below. But it has the problem: it adds page break before inserted document. Don’t want to delete all page breaks because original document could have some. How to avoid adding unnecessary page breaks while inserting document into another?

// Construct a document builder that will work with the destination document.

DocumentBuilder builder = new DocumentBuilder(destinationDocument);

// Find the insertion point. DocumentBuilder.MoveToMergeField will automatically remove the merge field from the document.

if (!builder.MoveToMergeField(mergFieldName))

throw new ApplicationException("Insertion point not found.");

Node sectionBeforeInsertion = builder.CurrentSection;

//// Check whether merge field was placed at the section end.

//// Break the section if only this is required.

if (!(builder.IsAtEndOfParagraph && builder.CurrentParagraph.IsEndOfSection))




// Traverse all the sections in the subdocument and insert them after the place we found.

// sectionBeforeInsertion will advance as we add sections.

foreach (Section sectionToAdd in sourceDocument.Sections)


Node newNode = destinationDocument.ImportNode(sectionToAdd, true, ImportFormatMode.KeepSourceFormatting);

sectionBeforeInsertion = destinationDocument.InsertAfter(newNode, sectionBeforeInsertion);


Hi Victor,

Thanks for your inquiry.

Please create a simple application (for example a Console Application Project) that helps us reproduce the same problem on our end and attach it here for testing. Unfortunately, it is difficult to say what the problem is without the Document(s) and simplified application. We need your Document(s) and Simple Project to reproduce the problem. As soon as you get these pieces of information to us we’ll start our investigation into your issue. Thanks for your cooperation.

Best regards,

Thank you for reply.

I included simple console application.

It adds pagebreak before first subreport, but not second and so on.

Hi Victor,

Thanks for the additional information. In your case, you need to specify whether you want the first section to start from a new page or on the same page. Please add the following line of code in your Main method’s body:


InsertBySectionsKeepingFormatting("Subreport:1", docMainTemlate, docTemlate2);

Stream stream = new MemoryStream();

string outputPath = path + "Output.pdf";

docMainTemlate.Sections[1].PageSetup.SectionStart = SectionStart.Continuous;




I hope, this helps.

Best regards,

Thank you very much. It works.

Futhermore if subreports will be inserted as unknown section indexer (could be multiply page breaks before) this code will remove unexpected page breaks and keep existing:

foreach(Section section in docMainTemlate.Sections)


section.PageSetup.SectionStart = SectionStart.Continuous;


This code also fixes footer displayed at wrong place(after another subreport).

Hi Victor,

Thanks for your feedback and it’s great you were able to find what you were looking for. Please let us know any time you have any further queries.

Best regards,