C# - How to Convert Word, Excel, and PowerPoint to PDF

Alexander Stock
5 min readMar 4, 2022

--

With the increasing popularity of PDF files in today’s time, it has become a common requirement for programmers to convert MS Office files like Word, Excel, and PowerPoint to PDF while maintaining high quality.

However, there is no built-in library available in .NET that facilitates file format conversion. Therefore, programmers need to rely on third-party libraries like Spire.Office for .NET to accomplish this task.

In this article, I will not only demonstrate the process of converting different MS Office file formats to PDF using Spire.Office but also provide guidance on configuring the conversion settings. This includes options such as embedding fonts in the generated PDF and encrypting the PDF with a password.

Add References

Method #1: Download Spire.Office (hotfix version is recommended) and unzip the package somewhere on your disk to find the “BIN” folder. Spire.Office has the DLLs compiled for multiple versions of .NET Framework as well as for .NET Core and other platforms. Choose the DLLs from the folder that you exactly need and add them all as dependencies in your project.

Method #2: Create a .NET application in you Visual Studio, and install Spire.Office directly through NuGet. NuGet package manager will automatically install the correct version that fits in with your application.

Convert Word (Doc/Docx) to PDF in C#

Spire.Office is a combination of multiple file format libraries, including Spire.Doc, Spire.XLS, Spire.Presentation, and Spire.PDF. As suggested by their names, they are used for processing Word, Excel, PowerPoint and PDF documents, respectively.

The Spire.Doc namespace provides the Document class to represents a Word document model and the ToPdfParameterList class to control how Word documents will be converted to PDF. The following are the detailed steps:

  • Create a Document object, and load a Word file using LoadFromFile method.
  • Create a ToPdfParameterList object, and use the properties like IsEbeddedAllFonts and DisableLink to control whether to embed fonts in generated PDF and whether to disable hyperlink.
  • Use ToPdfParameterList.PdfSecurity.Encrypt method to set open password, permission password, and users’ permission over the generated PDF document.
  • Set the image quality after conversion using Document.JPEGQuality property.
  • Save the Word document to PDF using Document.SaveToFile method.
using Spire.Doc;

namespace ConvertWordToPdf
{
class Program
{
static void Main(string[] args)
{
// Create a Document instance
Document doc = new Document();

// Load a sample Word document, the file extension can be .doc or .docx
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

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

// Embed all fonts in the PDF document
toPdfParameterList.IsEmbeddedAllFonts = true;

// Remove the hyperlinks but keep the character formats
toPdfParameterList.DisableLink = true;

// Set open password, permission password and user's permission over the PDF document
toPdfParameterList.PdfSecurity.Encrypt("open password", "permission password", PdfPermissionsFlags.None, PdfEncryptionKeySize.Key128Bit);

// Set the output image quality as 100% of the original image (the default is 80%)
doc.JPEGQuality = 100;

// Save the Word document as PDF
doc.SaveToFile("ToPDF.pdf", toPdfParameterList);
}
}
}
Figure 1. Conert Word to PDF

Convert Excel (Xls/Xlsx) to PDF in C#

The Spire.Xls namespace offers the Workbook class to represent an Excel document model and the ConverterSetting class to set the conversion conditions. The following are the steps:

  • Create a Workbook object and load an Excel file using LoadFromFile method.
  • Loop through the worksheets in the workbook, and auto fit the column widths of each sheet using Worksheet.AllocatedRange.AutoFitColumns method. This step ensures that the content within a small cell can fully display.
  • Get ConverterSetting object, and set the worksheet content to fit to the page size through SheetFitToPage property.
  • Use ConverterSetting.PdfSecurity.Encrypt method to set open password, permission password, and users’ permission over the generated PDF document.
  • Save the Excel file to PDF using Workbook.SaveToFile method.
using Spire.Xls;

namespace ConvertExcelToPdf
{
class Program
{
static void Main(string[] args)
{
// Create a Workbook instance
Workbook workbook = new Workbook();

// Load a sample Excel file, the file extension can .xls or .xlsx
workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

// Loop through the worksheets
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
// Autofit column width so that the content inside a cell can display completely
workbook.Worksheets[i].AllocatedRange.AutoFitColumns();
}

// Get convert setting
ConverterSetting converterSetting = workbook.ConverterSetting;

// Set worksheets to fit to page when converting
converterSetting.SheetFitToPage = true;

// Set open password, permission password and user's permission over the PDF document
converterSetting.PdfSecurity.Encrypt("open password", "permission password", PdfPermissionsFlags.None, PdfEncryptionKeySize.Key128Bit);

// Save Excel to PDF
workbook.SaveToFile("ExcelToPdf.pdf", FileFormat.PDF);
}
}
}
Figure 2. Convert Excel to PDF

Convert PowerPoint (Ppt/Pptx) to PDF in C#

The Spire.Presentation namespace provides the Presentation class to represents a PowerPoint document model and the SaveToPdfOption class to control the conversion settings. The steps are as follows:

  • Create a Presentation object and load a PowerPoint document using LoadFromFile method.
  • Get the SaveToPdfOption object.
  • Set open password, permission password, and users’ permission over the generated PDF document using SaveToPdfOption.PdfSecurity.Encrypt method.
  • Save the PowerPoint document to PDF using Presentation.SaveToFile method.
using Spire.Presentation;

namespace ConvertPowerPointToPdf
{
class Program
{
static void Main(string[] args)
{
// Create a Presentation object
Presentation presentation = new Presentation();

// Load a sample PowerPoint file, the file extension can be .ppt or .pptx
presentation.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pptx");

// Get SaveToPdfOption object
SaveToPdfOption saveToPdfOption = presentation.SaveToPdfOption;

// Set open password, permission password and user's permission over the PDF document
saveToPdfOption.PdfSecurity.Encrypt("open password", "permission password", PdfPermissionsFlags.None, PdfEncryptionKeySize.Key128Bit);

// Save the presentation to PDF
presentation.SaveToFile("ToPdf.pdf", FileFormat.PDF);
}
}
}
Figure 3. Convert PowerPoint to PDF

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.