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

Free Support Forum - aspose.com

InsertDocument as bookmark not working with one file but works with the other

Hi,
Below is my code for insertdocument, which has been working great until now.
I am trying to insert two.doc into one.doc and it is not working.
I also attached two_WORKS.doc because this is a similar file that works.
Also I am using Aspose.Words 5.2.1
Thanks!
Scott

private void button2_Click(object sender, EventArgs e)
{
    InsertDocument(@"c:\testdata\one.doc", @"c:\testdata\two.doc", "ScopeOfSupply", @"c:\testdata\three.doc");
}
public void InsertDocument(String afterNode, String doc, String bmark, String NewDoc)
{
    Aspose.Words.License license = new Aspose.Words.License();
    license.SetLicense("c:\\users\\Aspose.Total.lic");
    Node insertAfterNode;
    Document dstDoc = new Document(afterNode);
    LoadFormat format = Document.DetectFileFormat(doc);
    Document srcDoc;
    if (format == LoadFormat.Unknown)
    {
        // If format is unknown, maybe this is HTML (issue #3955)
        try
        {
            srcDoc = new Document(doc, LoadFormat.Html, string.Empty);
        }
        catch
        {
            throw new ArgumentException("Input file format is unknown");
        }
    }
    else
    {
        srcDoc = new 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.KeepSourceFormatting);
    // 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);
}

Hi
Thanks for your inquiry. Two.DOC is HTML document and it contains invalid content. Here is what your document contains.

<table border="0" width="100%" cellpadding="0" cellspacing="0">
    <td width="10%">
        <font face="Arial"></font>
    </td>
    <td width="30%" valign="top">
        <font face="Arial">Reclosers</font>
    </td>
    <td width="60%">
        <font face="Arial">NOVA Triple Single Recloser (Qty: 1)
</table>
You can see that table is invalid. There is no tags and the last is ended improperly. Here is how this should look.
<table border="0" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td width="10%">
            <font face="Arial"></font>
        </td>
        <td width="30%" valign="top">
            <font face="Arial">Reclosers</font>
        </td>
        <td width="60%">
            <font face="Arial">NOVA Triple Single Recloser (Qty: 1)</font>
        </td>
    </tr>
</table>

I highlighted my changes.
Best regards.