Find and Replace Word Using Aspose Word

The Find and Replace function in Microsoft Word is extremely useful for identifying and replacing specific text in documents. It becomes more useful and time-saving while dealing with lengthier papers. We should accomplish it programmatically if we need to find and replace text in thousands of Word documents.

Here, I am taking the simple example to replace the name from the textbox value in the word file.
Step 1: Create the Temporary word file

Step 2: Store that word file in the project solution

Step 3: Install the Aspose Word from Nuget Package

Step 4: Take one textbox and button in your view and make an ajax call on the button as shown below

<input type="text" id="txtname" placeholder="Enter You Name" class="form-control" />
<button type="button" id="btnSave" class="btn btn-info">Submit</button>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
        $(document).on("click", "#btnSave", function () {
            debugger;
            var name = $("#txtname").val();
            $.ajax({
                url: "/Home/ReplaceWord?Name=" + name,
                type: "GET",
                dataType: "json",
                success: function (result) {
                   location.href = "/Home/DownloadReplaceWordFile?filename=" + result;
                },
                error: function (err) {
                    console.log(err)
                }
            });
        });
     
</script>

Step 5: Define the path in the appsettings.Json file for the document and read the value from appsetting.Json

In appsetting.json define the path of your document as shown below

"File": {
"Path": "XYZ" //Define your path here
}

Copy and paste the below code to read the value from appsetting.json

public class HomeController : Controller
   {
       public static IConfigurationRoot configuration;
       public HomeController(IConfiguration configuration)
       {
           _configuration= configuration
       }
 var path = configuration.GetSection("File").GetSection("Path").Value + "NewDocument.docx";//read value from appsetting.json  
   }

Step 6: Copy and paste the below code in your controller to replace the word and download the file

public IActionResult ReplaceWord(string Name)
 {
  
   var path = configuration.GetSection("File").GetSection("Path").Value + "NewDocument.docx";
   Document doc = new Document(path);
   doc.Range.Replace("#name#", Name, new FindReplaceOptions(FindReplaceDirection.Forward));
   string copyPath = configuration.GetSection("File").GetSection("Path").Value + Name+".pdf";
   doc.Save(copyPath);
   return Json(Name+".pdf");
 }

//To download word file in pdf formate
public FileResult DownloadReplaceWordFile(string filename)
 {
    var path = configuration.GetSection("File").GetSection("Path").Value + filename;
    byte[] bytes = System.IO.File.ReadAllBytes(path);
    return File(bytes, "application/pdf", filename);
 }

OUTPUT

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories