Free Support Forum - aspose.com

Inserting a Document into a Paragraph at a specific location

We have a specific document format where tags in the word document need to be replaced with the contents of an existing document

e.g.

[&Include DOC1.doc]

That will take the contents from DOC1.doc and include at this location when we are generating the document.

I have this working correctly when the [&Include] tag is the only thing in the Paragraph, but when there is other text within the same paragraph, the document is inserted at the front of the paragraph.

e.g.

[&Include Doc1.doc] more text [&include Doc2.doc]

What I really need is to be able to insert the imported document after a Run within the paragraph, so that I can find/build a run in the paragraph matching the tag, insert the document, then remove the run for the tag.

i’m using some sample code for the insert, but it will only insert under a Paragraph or Table

    /// <summary>
    /// Inserts content of the external document after the specified node.
    /// </summary>
    /// <param name="insertAfterNode">Node in the destination document after which the content
    /// Should be inserted. This node should be a block level node (paragraph or table).</param>
    /// <param name="srcDoc">The document to insert.</param>
    private void InsertDocumentWithSectionFormatting(Node insertAfterNode, Document srcDocument)
    {
        // Make sure that the node is either a pargraph 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.");

        // Document to insert srcDoc into.
        Document dstDoc = (Document)insertAfterNode.Document;
        // To retain section formatting, split the current section into two at the marker node and then import the content from srcDoc as whole sections.
        // The section of the node which the insert marker node belongs to
        Section currentSection = (Section)insertAfterNode.GetAncestor(NodeType.Section);

        // Don't clone the content inside the section, we just want the properties of the section retained.
        Section cloneSection = (Section)currentSection.Clone(false);

        // However make sure the clone section has a body, but no empty first paragraph.
        cloneSection.EnsureMinimum();
        cloneSection.Body.FirstParagraph.Remove();

        // Insert the cloned section into the document after the original section.
        insertAfterNode.Document.InsertAfter(cloneSection, currentSection);

        // Append all nodes after the marker node to the new section. This will split the content at the section level at
        // The marker so the sections from the other document can be inserted directly.
        Node currentNode = insertAfterNode.NextSibling;
        while (currentNode != null)
        {
            Node nextNode = currentNode.NextSibling;
            cloneSection.Body.AppendChild(currentNode);
            currentNode = nextNode;
        }

        // This object will be translating styles and lists during the import.
        NodeImporter importer = new NodeImporter(srcDocument, dstDoc, ImportFormatMode.UseDestinationStyles);

        // Loop through all sections in the source document.
        foreach (Section srcSection in srcDocument.Sections)
        {
            Node newNode = importer.ImportNode(srcSection, true);

            // Append each section to the destination document. Start by inserting it after the split section.
            dstDoc.InsertAfter(newNode, currentSection);
            currentSection = (Section)newNode;
        }
    }

I’ve tried splitting the Paragraph to ensure each tag is in it’s own paragraph, but I then end up with extra linebreaks at the end of the paragraphs.

Using Aspose.Words 18.11 for .NET

Any Suggestions ?

Peter

@Etaardvark

Thanks for your inquiry. In your case, we suggest you please move the cursor to the Run node and insert the document using DocumentBuilder.InsertDocument method. Hope this helps you.

Thanks - that sorted the problem nicely
I can move to the location of the tag (even if i need to split the run), and insert the document exactly where i need to.