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

Free Support Forum - aspose.com

Inserting Image

Dear Sir/Madame,


I would like to insert an image into a word document, preferably using DocumentBuilder.InsertImage(). I am inside of an IReplacingCallback.Replacing() callback function. I have used

Paragraph para = (Paragraph)e.MatchNode.ParentNode;

to get the current node. However, I cannot determine how to create a DocumentBuilder with its cursor located on, or preferably just after (since I will be replacing the paragraph with the image) the para.

I have a few related questions:
1. In general, if I have a Node/CompositeNode, how can I get the Document (not DocumentBase)?
2. In general, if I have a Node/CompositeNode, how can I get a DocumentBuilder positioned on that Node/CompositeNode?

In my specific case, if I cannot get a DocumentBuilder positioned at the right location, then, how can I still insert the image. My surrounding code is roughly:

Paragraph para = (Paragraph)e.MatchNode.ParentNode;
//check para is Paragraph or Table - code not shown here
CompositeNode dstStory = para.ParentNode;
dstStory.InsertImageAfter(“x.png”); // How do I do the equivalent of this line???
para.Remove();

Thank you.

I managed to get the DocumentBuilder and set the cursor using


DocumentBuilder db = new DocumentBuilder(((Document)para.Document));
db.MoveTo(para);

I can then insert the image with

db.InsertImage(srcImage);

However, if I then remove the paragraph, it also removes the image I just inserted.

I thought I could do

DocumentBuilder db = new DocumentBuilder(((Document)para.Document));
db.MoveTo(para.ParentNode);

However, I get a System.InvalidOperationException
The node must be a paragraph or an inline node.

I have some placeholder text in my document (the text matched by the find). I want to replace that text with the image. So, I want to delete that text, then put the image where it was. What would be the most appropriate way to do that?


I did it this way.


DocumentBuilder db = new DocumentBuilder(((Document)para.Document));
db.MoveTo(para);
Node newPara = db.InsertParagraph();
db.MoveTo(newPara);
db.InsertImage(srcImg);

Everything works now, but I just made guesses on everything I did here. Please just confirm for me that what I did makes sense, or please let me know a better way.

Thanks
Hi Joshua,

Thanks for your inquiry. Please use following FindandInsertImage class to find a text and replace it with image. Hope this helps you.

If you still face problem, please share your input document and text which you want to replace with image here for reference. We will then provide you more information about your query along with code.

private class FindandInsertImage : IReplacingCallback

{

private String path;

public FindandInsertImage(String imagepath)

{

path = imagepath;

}

///

/// This method is called by the Aspose.Words find and replace engine for each match.

///

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

// This is a Run node that contains either the beginning or the complete match.

Node currentNode = e.MatchNode;

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

// This array is used to store all nodes of the match for further removing.

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

{

runs.Add(currentNode);

remainingLength = remainingLength - currentNode.GetText().Length;

// Select the next Run node.

// Have to loop because there could be other nodes such as BookmarkStart etc.

do

{

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

runs.Add(currentNode);

}

// Create Document Builder and insert document

DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);

builder.MoveTo((Run)runs[runs.Count - 1]);

builder.InsertImage(path);

// Now remove all runs in the sequence.

foreach (Run run in runs)

run.Remove();

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

return ReplaceAction.Skip;

}

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;

}

}