Articles in this section
Category / Section

How to replace placeholders in Word document and convert to PDF in ASP.NET Core?

7 mins read

Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can to replace placeholders in Word document and convert to PDF using C#. This article explains how to take user input from a UI, replace placeholders in a Word document with that input, and convert the final document to a PDF.

Steps to replace placeholders in Word document and convert to PDF:

  1. Create a new ASP.NET Core Web application (Model-View-Controller) project.
    Create ASP.NET Core application
  2. Install the Syncfusion.DocIORenderer.Net.Core NuGet package as a reference to your project from NuGet.org.
    Add DocIORenderer NuGet package reference to the project
Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.

  1. Include the following namespaces in the HomeController.cs file.
    C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;
  1. A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page Index.cshtml.
  2. Add a new code in the Index.cshtml as shown below.
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8" />
   <meta name="viewport" content="width=device-width, initial-scale=1" />
   <title>@ViewData["Title"]</title>
   <style>
       body {
           font-family: Arial, sans-serif;
           background-color: #f4f4f4;
           margin: 0;
           padding: 0;
       }

       .form-container {
           width: 400px;
           margin: 50px auto;
           padding: 20px;
           background-color: white;
           border-radius: 8px;
           box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
       }

           .form-container label {
               font-weight: bold;
               display: block;
               margin-bottom: 5px;
           }

           .form-container input[type="text"],
           .form-container input[type="email"] {
               width: 100%;
               padding: 10px;
               margin-bottom: 20px;
               border: 1px solid #ddd;
               border-radius: 4px;
           }

           .form-container input[type="submit"] {
               width: 100%;
               padding: 12px;
               background-color: #007BFF;
               color: white;
               border: none;
               border-radius: 4px;
               font-size: 16px;
               cursor: pointer;
           }

               .form-container input[type="submit"]:hover {
                   background-color: #0056b3;
               }
   </style>
</head>
<body>

   <div class="form-container">
       @using (Html.BeginForm("CreateDocument", "Home", FormMethod.Get))
       {
           <label for="firstname">First Name:</label>
           <input type="text" id="firstname" name="firstname" required />

           <label for="lastname">Last Name:</label>
           <input type="text" id="lastname" name="lastname" required />

           <label for="email">Email:</label>
           <input type="email" id="email" name="email" required />

           <label for="phone">Phone:</label>
           <input type="text" id="phone" name="phone" required />

           <label for="address">Address:</label>
           <input type="text" id="address" name="address" required />

           <input type="submit" value="Create Document" />
       }
   </div>

</body>
</html> 
  1. Add a new action method CreateDocument in HomeController.cs and include the below code snippet to to replace placeholders in Word document and convert to PDF.
    C#
//Creating a new document.
using (WordDocument document = new WordDocument(new FileStream("Data/Template.docx", FileMode.Open, FileAccess.Read), FormatType.Automatic))
{
    // Store inputs in a 1D array
    string[] userInputs = { firstname, lastname, email, phone, address };
    // Find all placeholders like "{{Name}}"
    TextSelection[] selections = document.FindAll(new Regex(@"\{(.*)\}"));
    for (int i = 0; i < selections.Count(); i++)
    {
        TextSelection selection = selections[i];
        //Replace the text with user values
        document.Replace(selection.SelectedText, userInputs[i], false, true);
    }
    //Instantiation of DocIORenderer for Word to PDF conversion
    using (DocIORenderer render = new DocIORenderer())
    {
        //Converts Word document into PDF document
        PdfDocument pdfDocument = render.ConvertToPDF(document);

        //Saves the PDF document to MemoryStream.
        MemoryStream stream = new MemoryStream();
        pdfDocument.Save(stream);
        stream.Position = 0;

        //Download PDF document in the browser.
        return File(stream, "application/pdf", "Sample.pdf");
    }
}

You can download a complete working sample to replace placeholders in a Word document and convert it to PDFfrom GitHub.

Output Word document

Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Conclusion

I hope you enjoyed learning about how to replace placeholders in a Word document and convert it to PDF in a .NET Core Word document.

You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied