C#/VB.NET: Convert Word to PDF

PDF has many advantages over Word documents, for example, it has a fixed layout which ensures that the formatting and content of the document remains the same when being viewed on a variety of devices as well as operating systems. In view of this, it is more recommended to convert Word documents to PDFs when sharing and transferring them. In this article, you will learn how to convert Word to PDF and how to set conversion options in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

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

The Document.SaveToFile(string fileName, FileFormat fileFormat) method provided by Spire.Doc for .NET allows to save Word as PDF, XPS, HTML, RTF, etc. If you just want to save your Word documents as regular PDFs without additional settings, follow the steps below.

  • Create a Document object.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Save the document to PDF using Doucment.SaveToFile() method.
  • C#
  • VB.NET
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);
        }
    }
}

C#/VB.NET: Convert Word to PDF

Convert Word to Password-Protected PDF in C# and VB.NET

To convert Word to a Password-Protected PDF, you can use the Document.SaveToFile(string fileName, ToPdfParameterList paramList) method. The ToPdfParameterList parameter controls how a Word document will be converted to PDF, for example, whether to encrypt the document while conversion. The following are the detailed steps.

  • Create a Document object.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Create a ToPdfParameterList object, which is used to set conversion options.
  • Specify the open password and permission password and then set both passwords for the generated PDF using ToPdfParameterList.PdfSecurity.Encrypt() method.
  • Save the Word document to PDF with password using Doucment.SaveToFile(string fileName, ToPdfParameterList paramList) method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ToPDFWithPassword
{
    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");

            //Create a ToPdfParameterList instance
            ToPdfParameterList parameters = new ToPdfParameterList();

            //Set open password and permission password for PDF
            string openPsd = "E-iceblue";
            string permissionPsd = "abc123";
            parameters.PdfSecurity.Encrypt(openPsd, permissionPsd, Spire.Pdf.Security.PdfPermissionsFlags.Default, Spire.Pdf.Security.PdfEncryptionKeySize.Key128Bit);

            //Save the Word document to PDF with password
            document.SaveToFile("ToPDFWithPassword.pdf", parameters);
        }
    }
}

C#/VB.NET: Convert Word to PDF

Convert Word to PDF with Bookmarks in C# and VB.NET

Bookmarks can enhance the readability of a document. When generating PDF from Word, you may would like to preserve existing bookmarks of the Word document or create bookmarks from the headings. The following are the steps to convert Word to PDF with bookmarks.

  • Create a Document object.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Create a ToPdfParameterList object, which is used to set conversion options.
  • Create bookmarks in PDF from existing bookmarks in Word using ToPdfParameterList.CreateWordBookmarks property. Or you can create bookmarks in PDF from the headings in Word using ToPdfParameterList.SetCreateWordBookmarksUsingHeadings property.
  • Save the document to PDF with bookmarks using Doucment.SaveToFile(string fileName, ToPdfParameterList paramList) method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ToPDFWithBookmarks
{
    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");

            //Create a ToPdfParameterList object
            ToPdfParameterList parameters = new ToPdfParameterList();

            //Create bookmarks in PDF from existing bookmarks in Word
            parameters.CreateWordBookmarks = true;

            //Create bookmarks from Word headings
            //parameters.CreateWordBookmarksUsingHeadings= true;

            //Save the document to PDF
            document.SaveToFile("ToPDFWithBookmarks.pdf", parameters);
        }
    }
}

C#/VB.NET: Convert Word to PDF

Convert Word to PDF with Fonts Embedded in C# and VB.NET

By embedding fonts used in a Word document into the PDF document, you ensure that the PDF document looks the same on any device that does not have the appropriate fonts installed. The steps to embed fonts in PDF during conversion are as follows.

  • Create a Document object.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Create a ToPdfParameterList object, which is used to set conversion options.
  • Embed fonts in the generated PDF by setting the ToPdfParameterList.IsEmbeddedAllFonts property to true.
  • Save the document to PDF using Doucment.SaveToFile(string fileName, ToPdfParameterList paramList) method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ToPDFWithFontsEmbedded
{
    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");

            //Create a ToPdfParameterList object
            ToPdfParameterList parameters = new ToPdfParameterList();

            //Embed all the fonts used in Word in the generated PDF
            parameters.IsEmbeddedAllFonts = true;

            //Save the document to PDF
            document.SaveToFile("ToPDFWithFontsEmbedded.pdf", parameters);
        }
    }
}

C#/VB.NET: Convert Word to PDF

Set Image Quality When Converting Word to PDF in C# and VB.NET

A document containing a large number of high-quality images will often be large in size. When you convert Word to PDF, you can decide whether to compress the image quality or not. The following are the detailed steps.

  • Create a Document object.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Set the image quality using Document.JPEGQuality property.
  • Save the document to PDF using Doucment.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace SetImageQuality
{
    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");

            //Compress image to 40% of the original quality
            document.JPEGQuality = 40;

            //Preserve original image quality
            //document.JPEGQuality = 100;

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

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.