In-place insert of documents using Builder


#1

Hello, I searched the forums but couldn't find an example of inserting a document using document builder except this one: http://www.aspose.com/Community/Forums/39444/ShowPost.aspx

Although the post does suggest some ideas for doing this, it is unclear is this method is considered the recommended method.

I have a document I build at runtime with document builder. I need some example code to insert the contents of another document "in-place" and continue to build the original document AFTER the newly added document.

Please help...


#2

Here is the example that does the job:

Document doc1 = new Document(Application.StartupPath + @"\Doc1.doc");

DocumentBuilder builder = new DocumentBuilder(doc1);

Document doc2 = new Document(Application.StartupPath + @"\Doc2.doc");

Node currentNode = doc1.LastSection.Body.LastChild;

foreach(Section section in doc2.Sections)

{

foreach(Node node in section.Body.ChildNodes)

{

currentNode = (Node)doc1.ImportNode(node, true);

doc1.LastSection.Body.ChildNodes.Add(currentNode);

}

}

// move documentBuilder to the last node of the document

builder.MoveTo(currentNode);

// this line imitates the subsequent document building

builder.Writeln("Continue document creation...");

Don't hesitate to ask if you have further questions.


#3

Exactly what I was looking for, thanks!


#4

I need something similar. I was previously using code which imports Sections directly:

string[] filesToAdd = ...
Document mainDocument = new Document(mainFile);
foreach(string fileName in filesToAdd)
{
Document doc = new Document(fileName);
foreach(Section s in doc.Sections)
{
Section newSection = (Section) mainDocument.ImportNode(s, true);
mainDocument.Sections.Add(newSection);
}
}

My original code doesn't give me exactly what I want - there is a page throw between each imported file. The above code (importing ChildNodes) gives me a better result, but is *much* slower. Is there any way of improving its performance?

NB I think this wouold be a good candidate for a "How To" article in your Wiki/FAQ


#5

Try to set SectionStart of inserted section to continuous in your old code:

...

mainDocument.Sections.Add(newSection);

newSection.PageSetup.SectionStart = SectionStart.Continuous

...


#6

That does the trick, thanks.