Aspose.Words

Specifying How a Document is Joined Together

Specifying the Source Document to Flow Continuously or Start from a New Page

Documents are appended at the section level therefore the PageSetup.SectionStart property of the Section object defines how the content of the current section is joined in relation to the previous section. If the PageSetup.SectionStart property is set to SectionStart.NewPage for the first section in the source document then the content in this section is forced to start on a new page. Conversely if the property is set to SectionStart.Continuous then the content is allowed to flow on the same page directly after the previous section’s content.

Specifying the PageSetup.SectionStart property as SectionStart.Continuous for the first section of the source document will cause the content to appear together.

Example AppendDocument_JoinContinuous

Shows how to append a document to another document so the content flows continuously.

[Java]

 

Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");

Document srcDoc =  new Document(gDataDir + "TestFile.Source.doc");

 

// Make the document appear straight after the destination documents content.

srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);

 

// Append the source document using the original styles found in the source document.

dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

dstDoc.save(gDataDir + "TestFile.JoinContinuous Out.doc");

 

 

The generated output is below. The content of the joined document flows continuously.

Specifying the PageSetup.SectionStart property as SectionStart.NewPage instead will cause the appended content to appear on a new page.

Example AppendDocument_JoinNewPage

Shows how to append a document to another document so it starts on a new page.

[Java]

 

Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");

Document srcDoc =  new Document(gDataDir + "TestFile.Source.doc");

 

// Set the appended document to start on a new page.

srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);

 

// Append the source document using the original styles found in the source document.

dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

dstDoc.save(gDataDir + "TestFile.JoinNewPage Out.doc");

 

 

Please note that sometimes even a section set to be continuous may be forced onto a different page. This can happen as a result of the sections having different page settings. For example if one section has a larger PageSetup.PageWidth or PageSetup.PageHeight setting then the two sections cannot flow on one page.

By default a new document in Microsoft Word is created with sections to start on a new page. This option can be set under Page Setup in Microsoft Word as shown below.

This option is represented by the PageSetup.SectionStart property of the Section object in Aspose.Words. This property is of no real interest in the first section of a document when it’s not being joined to another document as any section start type for the first section will not affect how the document is displayed. Due to this reason it is unlikely to be changed and therefore the first section of a document will almost always be set to start on new page by default. This property does however become important when a document is being used to append to another as it defines how the source document is appended to the destination document.

As a result of this documents that are appended without the PageSetup.SectionStart property specifically defined will almost always result in the source document appearing on a new page by default.

Appending a Document’s Content to Appear Together on the Same Page

A document can be appended so that the content will always appear together on the same page and not split across two pages.

Example AppendDocument_KeepSourceTogether

Shows how to append a document to another document while keeping the content from splitting across two pages.

[Java]

 

Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");

Document srcDoc =  new Document(gDataDir + "TestFile.Source.doc");

 

// Set the source document to appear straight after the destination document's content.

srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);

 

// Iterate through all sections in the source document.

for(Paragraph para : (Iterable<Paragraph>) srcDoc.getChildNodes(NodeType.PARAGRAPH, true))

{

    para.getParagraphFormat().setKeepWithNext(true);

}

 

dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

dstDoc.save(gDataDir + "TestDcc.KeepSourceTogether Out.doc");

 

 

The code above will set the entire content of the appended document to be kept together on the same page using the ParagraphFormat.KeepWithNext property of the Paragraph class.

The output produced is below. Since the position it is inserted at will cause the content to be split across two pages the entire content is moved to the next page instead. Please note this is different from setting the source document to appear on a new page, it is good to note that the source document is set to be appended continuously