I found many support that used the similar logic to replaec text with image. But when I tried to use the same code, I got the following error
Argument 2: cannot convert from ‘ReplaceWithImageEvaluator’ to ‘string’
Argument 3: cannot convert from ‘bool’ to ‘Aspose.Words.Replacing.FindReplaceOptions’
would it be any new coding method for the function call Replace?
@queeniechan,
To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:
- Your simplified input Word document
- The image file that you want to insert
- Aspose.Words for .NET 20.8 generated output DOCX file showing the undesired behavior (if any)
- Your expected DOCX file showing the desired output. You can create this document manually by using MS Word.
- Please also share the source code that you are currently getting problems with
As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you more information.
How about providing ANY code method that could replace a text with an image? As far as I can tell, the example code provided is not supported and asking for more information when you obviously don’t have a solution is unacceptable.
To clarify, this post does not work as the method signature implemented doesn’t exist at all:
https://forum.aspose.com/t/replace-text-in-word-docx-document-with-jpeg-gif-png-images-using-c-net-code/49345
@Sunhillow You ca easily replace text with an image using IReplacingCallback. Please see the following code example:
Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new ReplaceEvaluatorFindAndReplaceWithImage();
doc.Range.Replace("[ImagePlaceholder]", @"C:\Temp\img.png", options);
doc.Save(@"C:\Temp\out.docx");
private class ReplaceEvaluatorFindAndReplaceWithImage : IReplacingCallback
{
/// <summary>
/// This method is called by the Aspose.Words find and replace engine for each match.
/// </summary>
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
Document doc = (Document)e.MatchNode.Document;
// 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 deleting.
List<Run> runs = new List<Run>();
// 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((Run)currentNode);
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((Run)currentNode);
}
// Insert an image at place of matched text.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveTo(runs[runs.Count - 1]);
// Here we suppose that replacement is a path to an image.
// You can use another approach to get an image.
builder.InsertImage(e.Replacement);
// Delete matched runs
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);
run.ParentNode.InsertAfter(afterRun, run);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring((0), (0) + (position));
return afterRun;
}
}
The code replaces "[ImagePlaceholder]"
text with an image, which path is passes as a replacement.