Free Support Forum - aspose.com

Page break after first AppendChild() method

Hello!
I use this code:

private void AppendDocument(Document dstDoc, Document srcDoc)
{
foreach (Section srcSection in srcDoc)
{
Node dstSection = dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
dstDoc.AppendChild(dstSection);
}
}

public Document Execute()
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Writeln("Some text.");

string DocPath = "...";
Document doc1 = new Document(System.IO.Path.Combine(DocPath, "Template.doc"));

string[] fieldNames = {...};
object[] fieldValues = {...};
doc1.MailMerge.Execute(fieldNames, fieldValues);

doc1.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

AppendDocument(doc, doc1);
builder.MoveToDocumentEnd();

builder.Writeln("Some text.");
AppendDocument(doc, doc1);

return doc;
}

As a result I receive such document on structure:

Some text.

Some text.
Contents of the document doc1.

Why there is the page break after the first "Some text."? Though after second "Some text." is not present...

Thanks!

Hi, Dmitry

Thanks for your interest in Aspose products. It seems that this occurs because section created by Aspose.Words and section created by MSWord have different PageSetup. That’s why MSWord splits these sections. You can solve this problem using two different ways.

First way is appending content of source section. See the following code.

private void AppendDocument_103816(Document dstDoc, Document srcDoc)

{

foreach (Section srcSection in srcDoc)

{

Node dstSection = dstDoc.ImportNode(srcSection, true, ImportFormatMode.UseDestinationStyles);

dstDoc.LastSection.AppendContent((Section)dstSection);

}

}

Second way is clearing formatting of source section. See the following code.

private void AppendDocument_103816(Document dstDoc, Document srcDoc)

{

foreach (Section srcSection in srcDoc)

{

Node dstSection = dstDoc.ImportNode(srcSection, true, ImportFormatMode.UseDestinationStyles);

(dstSection as Section).PageSetup.ClearFormatting();

(dstSection as Section).PageSetup.SectionStart = SectionStart.Continuous;

dstDoc.AppendChild(dstSection);

}

}

I hope that this will help you. Please let me know if you would like to know something else.

Best regards,

Thank you very much!