I am trying to insert insertme.doc into ProductSOSTemp1.doc at bookmark ‘ScopeOfSupply’. I am not getting any errors but the document is not being inserted.
Here is my code:
private void button2_Click(object sender, EventArgs e)
{
InsertDocument(@"c:\testing\ProductSOSTemp1.doc",@"c:\testing\insertme.doc","ScopeOfSupply",@"c:\testing\newdoc.doc",@"c:\program files\cooper power systems\eagle admin install\aspose.total.lic");
}
public void InsertDocument(String afterNode, String doc, String bmark, String NewDoc, String sLicense)
{
Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense(sLicense);
Node insertAfterNode;
Aspose.Words.Document dstDoc = new Aspose.Words.Document(afterNode);
LoadFormat format = Aspose.Words.Document.DetectFileFormat(doc);
Aspose.Words.Document srcDoc;
if (format == LoadFormat.Unknown)
{
// If format is unknown, maybe this is HTML (issue #3955)
try
{
srcDoc = new Aspose.Words.Document(doc, LoadFormat.Html, string.Empty);
}
catch
{
throw new ArgumentException("Input file format is unknown");
}
}
else
{
srcDoc = new Aspose.Words.Document(doc);
}
try
{
insertAfterNode = dstDoc.Range.Bookmarks[bmark].BookmarkStart.ParentNode;
}
catch
{
throw new ArgumentException("Invalid arguments - AsposeComWrapper");
}
// We need to make sure that the specified node is either pargraph or table.
if (!((insertAfterNode.NodeType == NodeType.Paragraph) ||
(insertAfterNode.NodeType == NodeType.Table)))
throw new ArgumentException("The destination node should be either paragraph or table.");
// We will be inserting into the parent of the destination paragraph.
CompositeNode dstStory = insertAfterNode.ParentNode;
// This object will be translating styles and lists during the import.
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document,
ImportFormatMode.UseDestinationStyles);
// Loop through all sections in the source document.
foreach(Aspose.Words.Section srcSection in srcDoc.Sections)
{
// Loop through all block level nodes (paragraphs and tables) in the body of the section.
foreach(Node srcNode in srcSection.Body)
{
// Do not insert node if it is a last empty paragarph in the section.
Aspose.Words.Paragraph para = srcNode as Aspose.Words.Paragraph;
if ((para != null) && para.IsEndOfSection && !para.HasChildNodes)
break;
// This creates a clone of the node, suitable for insertion into the destination document.
Node newNode = importer.ImportNode(srcNode, true);
// Insert new node after the reference node.
dstStory.InsertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
dstDoc.Save(NewDoc);
}