SoFunction
Updated on 2025-03-08

Summary of the method of implementing Word to PDF in C#

In .NET, you can use libraries to convert Word to PDF. This is a sample code, but be aware that this requires Microsoft Office to be installed on your system.

Before you start, you need to add a reference to the following steps:

  • Right-click on your project and select "Add Reference".
  • Select the "COM" tab in the pop-up window.
  • Locate and select "Microsoft Word Object Library" in the list.
using System;
using ;
public class WordToPdfConverter
{
    public void ConvertWordToPdf(string wordFilePath, string pdfFilePath)
    {
        // Create a Word application instance        Application wordApp = new Application();
        // Create a Word document object and open a Word file        Document wordDoc = (wordFilePath);
        try
        {
            // Save Word documents as PDF files            wordDoc.SaveAs2(pdfFilePath, );
        }
        catch (Exception ex)
        {
            ("Error converting file: " + );
        }
        finally
        {
            // Close Word document            ();
            // Exit Word app            ();
        }
    }
}

This class can be used like this:

WordToPdfConverter converter = new WordToPdfConverter();
(@"C:\path\to\", @"C:\path\to\");

This code will open the specified Word file, save it as a PDF file, and then close the Word document and application.

Note: This method requires the installation of Microsoft Word on the machine where the code is running.

In addition, the WPS COM component can be used to implement Word to PDF conversion. The code example is as follows:

First, you need to add a reference to KWPS and in the project.

using System;
using ;
using ;
using ;
using ;
using KWPSLib;
namespace WPSConvertToPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a WPS Application object             wpsApp = new ();
            // Open WPS document             wpsDoc = (@"D:\", , );
            try
            {
                // Save WPS document as PDF file                (@"D:\", );
                ("The conversion was successful!");
            }
            catch (Exception ex)
            {
                ("Error converting file: " + );
            }
            finally
            {
                // Close WPS document                (, , );
                // Exit WPS application                (, , );
            }
        }
    }
}

This approach also requires installing WPS Office on the machine running the code, and adding references to KWPS and to the project.

To add a reference to KWPS and you need to do the following steps in your project:

  • Right-click the project name and select Add->Reference.
  • In the dialog box that pops up, select the "COM" tab.
  • Find and select "Kingsoft Office" or "WPS Office" in the list (depending on which version of your system is installed).
  • Make sure the checkboxes for "Kingsoft Office" or "WPS Office" are selected and click "OK".
  • This will add a reference to KWPS in your project. You should now be able to use KWPS and in your code.

Note that using this method requires installing WPS Office on the machine where the code is running, and you need to make sure that your project has added a reference to KWPS.

If "Kingsoft Office" or "WPS Office" is not found in the list, it may be that you do not have WPS Office installed, or your WPS Office version does not support COM components. In this case, you may need to update or reinstall WPS Office, or consider using other libraries or methods to implement Word to PDF conversion.

This is the article about the method of implementing Word to PDF in C#. For more related C# Word to PDF content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!