Thanks for your inquiry. Could you please attach your input and expected output Word documents here for our reference? We will then provide you more information about your query along with code.
I have a template file from where I have to read the text and replace them by corresponding components.
For the reference I have attached the input as well as the output file.
Thanks for sharing the detail. In your case we suggest you please use find and replace feature of Aspose.Words. You can insert an image into document using DocumentBuilder.InsertImage and insert an embedded or linked OLE object from a file into the document using DocumentBuilder.InsertOleObject method. Please refer to the following articles.
Please use the following code example to find a text and replace it with image, OLE object or table. Hope this helps you.
Document doc = new Document(MyDir + "Inputfile.docx");
FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new FindandInsertContents();
doc.Range.Replace(new Regex("@@{Image"), "", options);
doc.Save(MyDir + "Out v16.8.0.docx");
private class FindandInsertContents : IReplacingCallback
{
private DocumentBuilder builder;
///
/// This method is called by the Aspose.Words find and replace engine for each match.
/// This method highlights the match string, even if it spans multiple runs.
///
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)e.MatchNode.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))
{
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);
}
// Write your code here to insert image, OLE and create table
// Use DocumentBuilder.InsertOleObject to insert OLE object in the document
// Use DocumentBuilder.InsertImage
builder.InsertImage(MyDir + "in.jpeg");
// remove the text
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;
}
}
Thanks for your inquiry. The older versions of Aspose.Words does not have DocumentBuilder.InsertOleObject. Please upgrade to the latest version of Aspose.Words for .NET 16.8.0 and let us know if you have any more queries.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.