Free Support Forum - aspose.com

Insert document at bookmark not working

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);

}

Hello

Thanks for your inquiry. Actually “insertme.DOC” it is not a .DOC it is HTML. If you try open/save this HTML using MS Word and then insert into the destination document the problem disappears. Please let me know in case of any issues. I will be glad to help you.

Best regards,

Furthermore, this HTML is not valid. There is an extra table without rows and cells..

<html>

<head>

<meta http-equiv="Content-Language" content="en-us">

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<meta name="GENERATOR" content="Microsoft FrontPage 4.0">

<meta name="ProgId" content="FrontPage.Editor.Document">

<title></title>

</head>

<body>

<table border="1" width="100%" cellpadding="0" cellspacing="0">

<table border="0" width="100%" cellpadding="0" cellspacing="0">

<tr>

<td width="70%">

<font face="Arial">14400V 150KVAR 2BU-22in./22in. 125BIL 60HZ</font>

</td>

</tr>

</table>

</table>

</body>

</html>

Removing the extra table fixed it… Thank You!!!