PDF is one of the most ubiquitous file formats today. Whether you are submitting a resume for your next job, generating a financial report for your boss, or publishing a scientific whitepaper – PDF is the defacto format.

Because it’s so widely used, you’re likely to run across many situations as a software developer where you need to be able to open and manipulate PDF files. But what do you do when your customers prefer to submit Microsoft Word, Powerpoint, or Excel documents and you need PDF files? Thanks to Foxit, you can easily build software that converts any of these formats to PDF.

In this tutorial, we’ll look at how to convert Microsoft Office documents to PDF files using Foxit’s PDF SDK for .NET. We’ll build a .NET web application that will accept these documents as uploaded files and convert them to PDF. There’s a sample application available on GitHub in the event you want to check out the code and run the finished application. Otherwise, follow along for a complete tutorial.

 

Prerequisites

Before you start building the .NET web application, you’ll need the following:

A recent version of .NET Core such as version 3.1

– An IDE that supports .NET Core development like Visual Studio 2019 or JetBrains Rider

Foxit PDF SDK for .NET Core on Windows using a free trial

 

Creating the .NET Web Application

When building any new .NET application, Microsoft and the community recommend using .NET Core or the upcoming re-branded .NET version 5.

Create a new folder, navigate to it, and run the following command from your command prompt:

dotnet new webapp -o OfficeToPDF

This will scaffold a new .NET Core web application using Razor Pages.

Note: Alternatively, you may use your IDE to create a new Razor Page web application.

 

Referencing The Foxit PDF SDK

After you’ve downloaded and extracted Foxit PDF SDK for Windows and .NET Core, copy the “lib” folder over to your .NET Core project’s root.

Using your IDE, add a reference to “lib\\x64_vc15\\fsdk_dotnetcore.dll”.

In your IDE (such as Visual Studio 2019), right-click your project in the solution explorer and choose “Add -> Existing Item…”. Add the file found at “lib\\x64_vc15\\fsdk.dll”.

Note: If using an IDE like JetBrains Rider, you may need to manually copy that file into the root of the project.

In the solution explorer, right-click that same file and choose “properties”. Under “Copy to output directory” choose “Copy if newer”.

Your `.csproj` file should now look like this:

xml
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="fsdk_dotnet, Version=7.4.0.915, Culture=neutral, PublicKeyToken=89adf39676d92a51">
<HintPath>lib\\x64_vc15\\fsdk_dotnet.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Update="fsdk.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

You’re now ready to configure Foxit’s SDK with your serial number and key.

 

Configuration

To use Foxit PDF SDK, you’ll need the serial number and key that was given to you inside the SDK download. The folder for the SDK version 7.4 is named `foxitpdfsdk_7_4_win_dotnet`. Inside this folder, navigate to the `lib` folder. You’ll see two files that have your serial number and key:

In the `gsdk_key.txt` file, your key is all the characters after “Sign=”.

In the `gsdk_sn.txt` file, your serial number is everything after “SN=”.

Go back to your .NET application and open the `appsettings.json` file. Replace it with the following:

json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Foxit": {
"SN": "YOUR_SERIAL_NUMBER",
"Key": "YOUR_KEY"
}
}

Don’t forget to replace the serial number and key with your own. Foxit’s SDK is now configured, so now you need a user interface and logic to handle file uploads.

 

Creating A File Upload UI

For this tutorial, you’ll need to create a user interface that will allow you to upload an Office document to the .NET back-end web application.

Open up the file at `Pages/Index.cshtml` and replace it with the following:

html
@page
@model IndexModel
@{
ViewData["Title"] = "Convert Office Document To PDF!";
}
<div class="text-center">
<form enctype="multipart/form-data" method="post">
<div class="form-group">
<label asp-for="File" class="col-form-label">Upload either a Word, Powerpoint, Excel or HTML file:</label>
<input asp-for="File" type="file" class="form-control" />
<span asp-validation-for="File" class="text-danger"></span>
</div>

<div class="form-group">
<button class="btn btn-primary" type="submit">Convert To PDF!</button>
</div>
</form>
</div>

The preceding code uses the built-in .NET Razor syntax to create an HTML form with a file upload input and a `<span>` that will display any validation errors.

Next, create the code-behind file for this Razor Page. Open up the `Pages/Index.cshtml.cs` and paste the following into this file:

csharp
// ********
// You’ll need all these using statements as you add more code
// ********
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using foxit.addon.conversion;
using foxit.common;
using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;

using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;
using Convert = foxit.addon.conversion.Convert;
using Path = System.IO.Path;
namespace OfficeToPDF.Pages
{
public class IndexModel : PageModel
{
// This is the property that our uploaded file will be bound to.
public IFormFile File { get; set; }
public async Task<IActionResult> OnPost()
{
return this.Page();
}
}
}

This is a basic controller shell. Right now, it allows you to upload a file but doesn’t do anything with it. In the following section, you’ll validate the file extension and convert it to a PDF file using Foxit.

 

File Validation

Next, you need to add some basic validation that ensures the file is either a Word, Excel or PowerPoint document. At the bottom of the `IndexModel` class add the following:

csharp
private static string[] ValidExtensions = new[] { "docx","pptx", "xlsx" };
private bool EnsureExtensionIsConvertible(string fileExtension) =>
ValidExtensions.Contains(fileExtension);

The `EnsureExtensionIsConvertible` will let you test whether a given file extension is able to be converted to a PDF document.

Next, replace the entire `OnPost` method that was already in your controller shell with the following:

csharp
public async Task<IActionResult> OnPost()
{
// Ensure that the uploaded file type is valid.
var fileExtension = Path.GetExtension(File.FileName).Remove(0, 1).ToLower();
if (!EnsureExtensionIsConvertible(fileExtension))
{
this.ModelState.AddModelError(
nameof(this.File),
$"File extension {fileExtension} is not convertible to PDF.");
return this.Page();
}
return this.Page();
}

Now, when you try to upload an invalid document type, you’ll get an error on the UI.

 

PDF Conversion

Now it’s time to add the code that converts Microsoft Office documents to PDF documents.

First, replace the `OnPost` method again with the following final implementation:

csharp
public async Task<IActionResult> OnPost([FromServices] IConfiguration config)
{
// Ensure that the uploaded file type is valid.
var fileExtension = Path.GetExtension(File.FileName).Remove(0, 1).ToLower();
if (!EnsureExtensionIsConvertible(fileExtension))
{
this.ModelState.AddModelError(
nameof(this.File),
$"File extension {fileExtension} is not convertible to PDF.");
return this.Page();
}

// Save the uploaded file.
var filePath = $"{Directory.GetCurrentDirectory()}/doc.{fileExtension}";
using (var stream = System.IO.File.Create(filePath))
{
await this.File.CopyToAsync(stream);
}
// Return the converted file to the web browser.
return ConvertToPDFFileResult(config, fileExtension, filePath);
}

You’ll see the same file validation logic from before as well as the code that saves the uploaded file and converts it to a PDF. If the file is valid then you’ll save it to the file system. Next, a method, `ConvertToPDFFileResult`, is used to return the converted PDF file as an HTTP message.

Create the `ConvertToPDFFileResult` method like this:

csharp
private static IActionResult ConvertToPDFFileResult(IConfiguration config, string fileExtension, string filePath)
{
var saveToPath = $"{Directory.GetCurrentDirectory()}/converted.pdf";
Library.Initialize(
config.GetValue<string>("Foxit:SN"),
config.GetValue<string>("Foxit:Key"));

if (fileExtension == "docx")
{
using var settings = new Word2PDFSettingData();
Convert.FromWord(filePath, string.Empty, saveToPath, settings);
}
else if (fileExtension == "pptx")
{
using var settings = new PowerPoint2PDFSettingData();
Convert.FromPowerPoint(filePath, string.Empty, saveToPath, settings);
}
else if (fileExtension == "xlsx")
{
using var settings = new Excel2PDFSettingData();
Convert.FromExcel(filePath, string.Empty, saveToPath, settings);
}
return new PhysicalFileResult(saveToPath, "application/pdf");
}

The Foxit PDF SDK does all the “heavy lifting”, so this method is pretty straightforward.

When converting any of the Office documents to a PDF, the Foxit SDK will save the converted file to the location that was specified. All we need to do is read that file using the `PhysicalFileResult` class. This will return the file to the requesting internet browser as an appropriate HTTP response.

 

Running the Application

To run the application, navigate to the root of your .NET application’s project. Then, execute `dotnet run` in your terminal. You’ll see the upload form in your browser.

Next, create and save a Microsoft Word document and upload it using your upload form.

Press “Convert To PDF!” and you’ll be taken to the converted PDF file in your browser.

That’s it!

 

Bonus: HTML To PDF Conversion

If you need to convert HTML documents to PDF, then Foxit also has you covered. The developer guide has a section about how to convert HTML documents to PDF documents.

If you follow the instructions and install the required files, you can add the following code inside the `ConvertToPDFFileResult` method in your application:

csharp
// ******
// HTML conversion requires the "htmltopdf" library for Foxit Support:
// https://developers.foxitsoftware.com/kb/article/developer-guide-foxit-pdf-sdk-net/#html-to-pdf-conversion
// ******
else if (fileExtension.ToLower() == "html")
{
using var settings = new HTML2PDFSettingData();
settings.page_height = 640;
settings.page_width = 900;
settings.page_mode = HTML2PDFSettingData.HTML2PDFPageMode.e_PageModeSinglePage;
Convert.FromHTML(filePath, “engine_path”, “cookies_path”, settings, saveToPath, 20);
}

Also, don’t forget to add the `”html”` file extension to the list of valid extensions. This will now let you convert uploaded HTML files or web pages to PDF documents that you can send to your users.

 

Now, you’ve seen how easy Foxit’s PDF SDK is to use when converting Microsoft Office and HTML documents to PDF. The core logic for PDF conversion was only about 20 lines of code. Foxit’s PDF SDK can help you build advanced application features that your customers need without investing a ton of time into custom code. Have a look at the developer guide to see what else is possible with Foxit’s PDF SDK for .NET.