Unable to change font of a specific string in a word/pdf document

Using Aspose.Net library for word and pdf

Issue 1:
I have an Word template which contains 4-5 paragraphs with font style “Times New Roman”. in this document a word “Hello” and I need to replace this Hello word with a new word “Hi” with new font style “Lucida Handwriting” and font size is 10 .
Note: I don’t want to change font style of whole document or paragraph. I just want to changes font style of the word “Hi”.

Please find the below code snippte:

Stream s = new MemoryStream(File, 0, File.Length, true, true);
Aspose.Words.Document conv = new Aspose.Words.Document(s);                        
                    
     conv.Range.Replace("Hello", "Hi", false, false); 
     conv.Save(convPdf, Aspose.Words.SaveFormat.doc);  

Issue 2:

I am facing the same issue in the pdf.
I have also tried the Run class/method but did not worked.

I am a paid user of Aspose and please reply ASAP.

Thanks.

@amanjainmccalla

Thank you for contacting support.

For Aspose.PDF for .NET API, you may specify ReplacementFont property in TextEditOptions class. The specified font will be used (if accessible) in the case where specified TextState.Font can not display segment text.

segment.TextEditOptions = new TextEditOptions(TextEditOptions.NoCharacterAction.UseCustomReplacementFont)
{
    ReplacementFont = FontRepository.FindFont("Lucida Handwriting")
};

About Aspose.Words API, we will be sharing our findings with you soon.

@amanjainmccalla

Please read the following article.
Find and Replace

In your case, you need to implement IReplacingCallback interface as shown below. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");

FindReplaceOptions options = new FindReplaceOptions();
options.ReplacingCallback = new ReplaceEvaluator();
doc.Range.Replace("Hello", "", options);

doc.Save(MyDir + "19.8.docx");

private class ReplaceEvaluator : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);
        builder.MoveTo(e.MatchNode);
        builder.Font.Name = "Arial";
        builder.Font.Size = 10.0;
        builder.Write("Hi");
        builder.Font.ClearFormatting();
        return ReplaceAction.Replace;
    }
}

Thanks Tahir .

I have tried to implement the solution shared by you but it not worked due to version differences.
I am using Aspose version 15.6.0.0, and
FindReplaceOptions class not available in this version because this version did not have namespace Aspose.Words.Replacing.
it will be good if you provide some help in Aspose vesion 15.6.0.0.

the same promblem I am facing with Aspose.pdf. Aspose pdf version is 10.5.0.0
Please help on that also.

@amanjainmccalla

Please note that we do not provide support for older released versions of Aspose products. Also, please note that we do not provide any fixes or patches for old versions of Aspose APIs either. All fixes and new features are always added into new versions of our APIs. So, please upgrade to the latest version of Aspose.PDF for .NET 19.8 and Aspose.Words for .NET 19.8 to get the desired output.

we are a paid user of Aspose, and we are using aspose since long time. we are using aspose dlls in many applications, and it’s not possible for us to update all aspose dlls in each application, so please do something needfull for this version 15.6.0.0. if you want I can share our paid a/c details.
We need quick help. Can we talk over phone regarding this? you can call me now on below mentioned number.
My mob: 9643878850

@amanjainmccalla

Please note that we only provide support via forums.

As per company policy, we do not provide support for older releases of Aspose APIs.