How to Convert Word to PDF using C#/VB.NET Code?

How to Convert Word to PDF using C#/VB.NET Code?

PDF (Portable Document Format) is a widely used file format for sharing documents. It offers many advantages over other formats, such as Microsoft Word. For example, when you convert a Word document to PDF, it becomes more secure. PDFs can be password-protected and encrypted, preventing unauthorized access or changes to the document. Additionally, PDFs look the same on any device, regardless of the operating system, software version, or fonts installed. This ensures that the formatting, layout, and design of your document remain intact when shared with others. This article will introduce how to convert Word documents to PDF format. The following are the specific steps and methods I compiled, and provide the C# /VB.NET code as a reference.

Programming Environment

In this test, Free Spire.Doc for .NET is introduced into the program. The Spire.Doc.dll file can be referenced by:

Method 1: Download Free Spire.Doc for .NET locally, unzip it, and install it. After the installation is complete, find Spire.Doc.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.

Method 2: Install via NuGet. It can be installed by the following 2 methods:

(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.Doc", and click "Install". Wait for the program installation to complete.

(2) Copy the following content to the PM console installation.

Install-Package FreeSpire.Doc -Version 10.8.0

Convert Doc or Docx to PDF in C# and VB.NET

Spire.Doc for. NET has powerful document conversion capabilities, which can convert Word to PDF format in just three steps. The detailed steps are as follows.

Step 1: Create a Document object.

Step 2: Load a sample Word document using Document.LoadFromFile() method.

Step 3: Save the document to PDF using Doucment.SaveToFile() method.

Full Code

C#

using Spire.Doc;

namespace ToPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document document = new Document();

            //Load a sample Word document
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx");

            //Save the document to PDF
            document.SaveToFile("ToPDF.pdf", FileFormat.PDF);
        }
    }
}

VB.NET

Imports Spire.Doc

Namespace ToPDF
    Class Program
        Private Shared Sub Main(ByVal args() As String)
            'Create a Document object
            Dim document As Document = New Document

            'Load a sample Word document
            document.LoadFromFile("C:\Users\Administrator\Desktop\Test.docx")

            'Save the document to PDF
            document.SaveToFile("ToPDF.pdf", FileFormat.PDF)
        End Sub
    End Class
End Namespace

Effective Shot

Conclusion:

In addition to Word to PDF conversion, Spire.Doc for .NET also offers many other useful features. For example, you can use Spire.Doc to Convert Word to Images (JPG, PNG and SVG), Convert Word to Excel and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.Doc for .NET.