Free Support Forum -

How to insert Embed files while reading a dynamic HTML file

Hi Team ,

I am using Apose total for creating Excel , Word and PDF files for many years.
In my task, i am reading a static HTML file , replace placeholders with dynamic content in it , create a DocumentBuilder and insert html in to it and document is ready.
Its all works fine , In bottom of attached document, you can see that there may any numbers of Responses undersection/subsection and these responses may have any embeded files.
Here Section A’s responses have some embeded files (decided by database) , i write code to for inserting files but i am not able place them just below their recpective responses.

So guide me to find a way to insert respective embeded object on just below of respective dynamic responses. (4.3 MB)

Thanks in advance


Thanks for your inquiry. In your case, you need to move the cursor to the desired location after inserting the HTML and insert the OLE object using DocumentBuilder.InsertOleObject method. You can find the text e.g. “Embeded PDF File” from the document and insert the OLE. We suggest you please read the following article about finding the text.
Find and Replace

Thanks Tahir for quick response.
As per your suggestion a OLE object be insert at a particular paragraph position like below.
builder.MoveToParagraph(14, 0);
builder.Writeln(“OLE object inserted here.”);

Since in my case index of paragraphs(where OLE object need to be inserted) are not fixed so it will be better
1.While creating dynamic HTML i insert some keywords “Myattachment1” , “Myattachment2” into it.
2.insert HTML into document .
3.Find the paragraph index for text “Myattachment1” in the document (lets it be xx)
4.Then move the cursor at that paragraph and insert OLE object there.
builder.MoveToParagraph(xx, 0);
builder.Writeln(“OLE object inserted here.”);

I am struggling in above step#3 to find index of paragraph containing “Myattachment1” , please suggest with a sample piece of code .

Many Thanks…


Thanks for your inquiry. Please insert the HTML into your document and then find the text e.g. “Myattachment1” and insert the OLE. Following code example shows how to find a text and insert the OLE. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
FindAndInsertOLE insertole = new FindAndInsertOLE("path of ole");
doc.Range.Replace("Myattachment1", "", new FindReplaceOptions() { ReplacingCallback = insertole }); 
doc.Save(MyDir + "18.9.docx");

public class FindAndInsertOLE : IReplacingCallback
    string ole;
    DocumentBuilder builder;
    public FindAndInsertOLE(string olepath)
        this.ole = olepath;
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
        // This is a Run node that contains either the beginning or the complete match.
        Node currentNode = e.MatchNode;

        if (builder == null)
            builder = new DocumentBuilder((Document)currentNode.Document);

        // The first (and may be the only) run can contain text before the match, 
        // in this case it is necessary to split the run.
        if (e.MatchOffset > 0)
            currentNode = SplitRun((Run)currentNode, e.MatchOffset);

        ArrayList runs = new ArrayList();

        // Find all runs that contain parts of the match string.
        int remainingLength = e.Match.Value.Length;
        while (
            (remainingLength > 0) &&
            (currentNode != null) &&
            (currentNode.GetText().Length <= remainingLength))
            remainingLength = remainingLength - currentNode.GetText().Length;

            // Select the next Run node. 
            // Have to loop because there could be other nodes such as BookmarkStart etc.
                currentNode = currentNode.NextSibling;
            while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));

        // Split the last run that contains the match if there is any text left.
        if ((currentNode != null) && (remainingLength > 0))
            SplitRun((Run)currentNode, remainingLength);

        Run run = (Run)runs[0];
        builder.InsertOleObject(ole, true, true, null);

        // Signal to the replace engine to do nothing because we have already done all what we wanted.
        return ReplaceAction.Skip;

    /// <summary>
    /// Splits text of the specified run into two runs.
    /// Inserts the new run just after the specified run.
    /// </summary>
    private static Run SplitRun(Run run, int position)
        Run afterRun = (Run)run.Clone(true);
        afterRun.Text = run.Text.Substring(position);
        run.Text = run.Text.Substring(0, position);
        run.ParentNode.InsertAfter(afterRun, run);
        return afterRun;

Thanks Tahir ,

I am using Aspose words , what is equivalent namespace from class FindReplaceOptions in that
since namespace Aspose.Words.Replacing not exists in Aspose words



Thanks for your inquiry. You can use old Aspose.Words.Range.Replace method.

Please note that we do not provide support for older released versions of Aspose.Words. Moreover, we do not provide any fixes or patches for old versions of Aspose products either. All fixes and new features are always added into new versions of our products.

We always encourage our customers to use the latest version of Aspose.Words as it contains newly introduced features, enhancements and fixes to the issues that were reported earlier.

Thanks Tahir ,

It helps me .