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

Free Support Forum - aspose.com

Inserting Word .Doc into another document

Hi,

I need to insert several word documents into a single document at specific positions. I have used the following code, and everything works well apart from the Continuous Section Breaks seem to be behaving like NewPage Section Breaks as they force a new page to start where each document is inserted. Any advice would be greatly appreciated:

public void InsertDocument(Node node, Document doc)

{

Document dstDoc = node.Document;

Section insertedSection;

int index;

if(node.GetAncestor(typeof(Cell))!=null || node.GetAncestor(typeof(Shape))!=null)

{

// Insertion point is tracked to Cell or Shape level:

// - insert appended document on node by node basis.

index = node.ParentNode.ChildNodes.IndexOf(node);

foreach(Section section in doc.Sections)

{

insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);

foreach(Node insertedNode in insertedSection.Body.ChildNodes)

{

// Only Paragraph or Table nodes can be inserted into Cell or Shape

if(node is Paragraph || node is Table)

{

node.ParentNode.ChildNodes.Insert(index++, insertedNode);

}

}

}

}

else

{

// Insertion point is tracked to Section.Body level:

// - insert appended document on section by section basis.

Section dstSection;

Body body = (Body)node.GetAncestor(typeof(Body));

if(body.LastChild!=node)

{

DocumentBuilder builder = new DocumentBuilder(dstDoc);

builder.MoveTo(node);

builder.InsertBreak(BreakType.SectionBreakContinuous);

dstSection = builder.CurrentParagraph.ParentSection;

}

else

{

dstSection = (Section)node.GetAncestor(typeof(Section));

}

index = dstDoc.Sections.IndexOf(dstSection);

int index0 = index;

foreach(Section section in doc.Sections)

{

insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);

dstDoc.Sections.Insert(index++, insertedSection);

}

dstDoc.Sections[index0].PageSetup.SectionStart = SectionStart.Continuous;

}

}

Please try using this updated version of InsertDocument:

///

/// Inserts content of the external document after the specified node.

///

/// Node in the destination document where the external document content should be inserted.

/// Document to insert.

public void InsertDocument(Node node, Document doc)

{

Document dstDoc = node.Document;

Section insertedSection;

int index = node.ParentNode.ChildNodes.IndexOf(node);

foreach (Section section in doc.Sections)

{

insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);

foreach (Node insertedNode in insertedSection.Body.ChildNodes)

{

// Only Paragraph or Table nodes can be inserted into Cell or Shape

if (insertedNode is Paragraph || insertedNode is Table)

{

// Do not insert node if it is a last empty paragarph in the section.

if (insertedNode is Paragraph && insertedNode == section.Body.LastChild && insertedNode.ToTxt().Equals(string.Empty))

break;

node.ParentNode.ParentNode.ChildNodes.Insert(++index, insertedNode.Clone(true));

}

}

}

}

This seems to work, but when used in the context of a ReplaceEvaluator an 'Object reference is not set to an instance of an object' is thrown. This is the code I am using:

private void InsertDocs()

{

try

{

// Process all tags inserting relevant doc from current or root folder

// Look for

doc.Range.Replace(new System.Text.RegularExpressions.Regex(tagStart + "ReadDoc\\..*?" + tagEnd, System.Text.RegularExpressions.RegexOptions.IgnoreCase), new ReplaceEvaluator(InsertDocEvaluator), false);

}

catch(Exception ex)

{

// Error here after replacing a few docs: 'Object reference is not set to an instance of an object'

MessageBox.Show("InsertDocs: " + ex.Message);

}

}

private ReplaceAction InsertDocEvaluator(object sender, ReplaceEvaluatorArgs e)

{

try

{

// Extract the document name from the tag

string docName = e.Match.Value;

docName = docName.Substring(docName.IndexOf(".")+1);

docName = docName.TrimEnd(tagEnd.ToCharArray());

// Insert doc

Document insertDoc = new Document(docName);

InsertDocument(e.MatchNode, insertDoc);

e.Replacement = "";

return(ReplaceAction.Replace);

}

catch(Exception ex)

{

MessageBox.Show("InsertDocEvaluator: " + ex.Message);

}

return(ReplaceAction.Skip);

}

The issue is complicated. Please give some more time for research. I will try to find some workaround.

Best regards,

This problem is logged to our defect base as issue #1258. We will try to fix it in our next release in a few weeks.

Best regards,

Fixed in Aspose.Words for .NET 4.0.2. Please dowload and check your application with this new version.

Best regards,