Hi
Thanks for your inquiry. to preserve page orientation you should preserve section in the inserted document. But InsertDocument method does not preserve sections so all inserted pages has the same orientation.
To preserve section you should do the following:
Move Documentbuilder cursor to the place in the document where the document should be inserted.
Insert a section break.
Insert section from the source document before the current section of DocumentBuilder.
I’m trying to insert section breaks using this code:
for (Section srcSection : (Iterable) srcDoc.getSections())
{
db.moveTo(insertAfterNode);
db.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
…
Node newNode = importer.importNode(srcNode, true);
// Insert new node after the reference node.
dstStory.insertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
…
}
But it doesn’t work correctly. After inserting nodes from source document, the section break is moved at the end of inserted nodes. How to insert break before inserting sections from source doc ?
Hi
Thanks for your request. Your code should look like the following:
@Test
public void Test001() throws Exception
{
// Open documents.
Document dstDoc = new Document("C:\\Temp\\dst.doc");
Document srcDoc = new Document("C:\\Temp\\src.doc");
// Create Documentbuilder and move its cursor to the bookmark.
DocumentBuilder builder = new DocumentBuilder(dstDoc);
builder.moveToBookmark("test");
// Get the current section, we will insert the source document after it.
Section insertAfter = builder.getCurrentSection();
// Insert a section break.
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
// Insert the source document.
appendDocumentAfterSection(srcDoc, insertAfter);
// Save output.
dstDoc.save("C:\\Temp\\out.doc");
}
private void appendDocumentAfterSection(Document srcDoc, Section insertAfter)
{
// Create NodeImporter that will hep us to copy content from one document to another.
NodeImporter importer = new NodeImporter(srcDoc, insertAfter.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
Node insertAfterInternal = insertAfter;
// Loop through all sections in the source document and insert them after the section in the destination document.
for (Section sect: srcDoc.getSections())
{
// Import section.
Node importedSection = importer.importNode(sect, true);
// Insert it into the document.
insertAfterInternal.getParentNode().insertAfter(importedSection, insertAfterInternal);
// Reset insertAfterInternal.
insertAfterInternal = importedSection;
}
}
Hi
Thanks for your request. The main idea of this code is that we insert a section break at the place where we need to insert the source document and insert sections from the source document between newly created sections in the destination document. If you would like simply avoid inserting a page break before the source document, you can modify your code as shown below:
@Test
public void Test001() throws Exception
{
// Open documents.
Document dstDoc = new Document("C:\\Temp\\dst.doc");
Document srcDoc = new Document("C:\\Temp\\src.doc");
// Create Documentbuilder and move its cursor to the bookmark.
DocumentBuilder builder = new DocumentBuilder(dstDoc);
builder.moveToBookmark("test");
// Get the current section, we will insert the source document after it.
Section insertAfter = builder.getCurrentSection();
// Insert a section break.
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
// Insert the source document.
srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
appendDocumentAfterSection(srcDoc, insertAfter);
// Save output.
dstDoc.save("C:\\Temp\\out.doc");
}
private void appendDocumentAfterSection(Document srcDoc, Section insertAfter)
{
// Create NodeImporter that will hep us to copy content from one document to another.
NodeImporter importer = new NodeImporter(srcDoc, insertAfter.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
Node insertAfterInternal = insertAfter;
// Loop through all sections in the source document and insert them after the section in the destination document.
for (Section sect: srcDoc.getSections())
{
// Import section.
Node importedSection = importer.importNode(sect, true);
// Insert it into the document.
insertAfterInternal.getParentNode().insertAfter(importedSection, insertAfterInternal);
// Reset insertAfterInternal.
insertAfterInternal = importedSection;
}
}