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

Free Support Forum - aspose.com

Inserting document into another document - lost page orientation

Hi,
I’m trying to insert one document into another using method
public static void insertDocument(Node insertAfterNode, Document srcDoc).

The source document contains few pages with horizontal orientation. After inserting, orientation od these pages is changed.

I’ am attaching documents for testing.

My code:
Document doc = new Document(“E:\doc1.doc”);
Document docToInsert = new Document(“E:\docToInsert.doc”);
Bookmark bk = doc.getRange().getBookmarks().get(“BOOKMARK”);
insertDocument(bk.getBookmarkStart().getParentNode(), docToInsert);
doc.save(“E:\doc1_result.doc”);
doc.save(“E:\doc1_result.pdf”);


Thanks in advance for help.

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:

1. Move Documentbuilder cursor to the place in the document where the document should be inserted.

2. Insert a section break.

3. Insert section from the source document before the current section of DocumentBuilder.

I think, the information provided here could be useful for you:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/how-the-appenddocument-method-works.html

Best regards,

Hi, thanks for reply.

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 ?

Thanks in advance.

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;

}

}

Hope this helps.

Best regards,

Hi,
it’s almost perfect. However there is also unnecessary section break before the whole inserted document.
Edit:
I’ve tried to remove :
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
But it didn’t help.

Can you provide me some solution ?

Thanks in advance.

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;

}

}

Best regards,