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

Free Support Forum - aspose.com

Not able to get the number format to the destination document

Hi,

I am trying to replace the tokens in one document (let’s assume a.doc) with the contents of other document. (b.doc)
Where I had the numbering format tags for the content in the document (b.doc) that are loosed after replacing the text in the document (a.doc).

Ex: if the b.doc contains

  1. First point
  2. Second point
  3. Third point

After executing the method InsertDocumentAtReplace()
The destination doc is coming as

  1. First point
  2. Second point
  3. Third point
public void InsertDocumentAtReplace()
{
    Aspose.Words.License license = new License();
    license.SetLicense(@"C:\Aspose.Words.lic");
    Document mainDoc = new Document("D:\Test\Doc1.doc");
    DocumentBuilder db = new DocumentBuilder(mainDoc);

    mainDoc.Range.Replace(new Regex(@"\[#Blurb1\#]"), new InsertDocumentAtReplaceHandler(), false);
    mainDoc.Save("D:\\Test\\" + "InsertDocumentAtReplace Out.doc");
}

private class InsertDocumentAtReplaceHandler : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        Document insertDoc = new Document(@"D:\Test\Blurb1.docx");
        //Get MatchNode
        Run run1 = (Run)e.MatchNode;
        //Create Run
        Run run2 = (Run)run1.Clone(true);
        //Get index of match value
        int index = run1.Text.IndexOf(e.Match.Value);
        //Split text in match node
        run2.Text = run1.Text.Substring(0, index);
        run1.Text = run1.Text.Substring(index + e.Match.Value.Length);
        run1.ParentParagraph.InsertBefore(run2, run1);
        DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
        //Move to run1
        builder.MoveTo(run1);
        //Insert Paragraph break
        builder.InsertBreak(BreakType.ParagraphBreak);
        Paragraph para = (Paragraph)builder.CurrentParagraph.PreviousSibling;
        // Insert document after the paragraph, containing match text.
        InsertDocument(para, insertDoc);
        // Remove the paragraph with the match text.
        // para.Remove();
        return (ReplaceAction.Skip);
    }

    public void InsertDocument(Node insertAfterNode, Document srcDoc)
    {
        Document dstDoc = (Aspose.Words.Document)insertAfterNode.Document;
        //Content of first paragraph of srcDoc will be apended to this parafraph
        Paragraph insertAfterParagraph = insertAfterNode as Paragraph;
        //Content of last paragraph of srcDoc will be apended to this parafraph
        Paragraph insertBeforeParagraph = insertAfterNode.NextSibling as Paragraph;

        //We will be inserting into the parent of the destination paragraph.
        CompositeNode dstStory = insertAfterNode.ParentNode;
        //Remove empty paragraphs from the end of document
        while (!srcDoc.LastSection.Body.LastParagraph.HasChildNodes)
        {
            srcDoc.LastSection.Body.LastParagraph.Remove();
        }

        //Loop through all sections in the source document.
        foreach (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.
                Paragraph para = srcNode as 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 = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
                dstStory.InsertAfter(newNode, insertAfterNode);
                insertAfterNode = newNode;
            }
        }
    }
}

Any help regarding this will help me a lot.

Thanks,
Yashwanth

Hi Yashwanth,

Thanks for your inquiry. Could you please attach your input Word documents here for testing? I will investigate the issue on my side and provide you more information.

Best Regards,

Hi,

Here are the attachments please review the same.

Thanks,
Yashwanth

Hi Yashwanth,

Thanks for your inquiry. While using latest version of Aspose.Words, i have observed problem in your InsertDocument method. For more details please visit documentation here.

Please replace following code snippet:


private void NewInsertDocument(Node insertAfterNode, Document srcDoc)
{
    // Make sure that the node is either a paragraph or table.
    if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) &
    (!insertAfterNode.NodeType.Equals(NodeType.Table)))
        throw new ArgumentException("The destination node should be either a 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 (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)
        {
            // Let's skip the node if it is a last empty paragraph in a section.
            if (srcNode.NodeType.Equals(NodeType.Paragraph))
            {
                Paragraph para = (Paragraph)srcNode;
                if (para.IsEndOfSection && !para.HasChildNodes)
                    continue;
            }
            // 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;
        }
    }
}

Hope this will help.

Thanks a lot Imran. It worked !!

Thanks,
Yashwanth