Apryse Announces Acquisition of AI-Powered Document Toolkit Provider LEAD Technologies

How to Convert Office Documents to PDF on a Server Without Installing Office Using C#

By Apryse | 2023 Dec 18

Sanity Image
Read time

7 min

Introduction

Copied to clipboard

In the modern business landscape, sharing and archiving documents in a reliable and universally accessible format is paramount. Portable Document Format (PDF) stands out as a versatile and widely supported format that ensures consistent presentation across different platforms. Converting Microsoft Office documents such as Word, Excel, and PowerPoint files into PDFs is a common requirement.

The Apryse PDF SDK can be used with multiple languages and frameworks to convert Office Documents to PDF, with both client-side (within the browser) and server-side conversions.

In this blog post, we'll explore how to convert Office documents using the Apryse SDK C# running in .Net on a server.

The Apryse .NET library can be downloaded using Node, as can a package that contains dozens of samples. In this blog we will walk through getting started with the sample for converting a Word document to PDF, then extend it to see some of the other functionality that is available.

But Surely Creating a PDF is Easy, You Can Just Use 'Export from Word'

Copied to clipboard

If you have Office installed, then creating a PDF using that on your own machine is simple.

Theoretically a server could be set up that uses Office to perform the conversions within the Web server backend, but there are several issues with that:

  • Some versions of Office are not recommended to be used as a service component
  • Office sometimes brings up modal user dialogs, particularly when an update is needed, and since services have no UI, the dialog can never be dismissed, and the application will hang
  • Licensing of Office in this way is not the same as for a single user Desktop machine, so there is additional complexity in using this solution in a legal, licensed way.

The awesome thing about the Apryse solution is that there is no need for Office to be installed, either on the user’s machine, or on a server, for the conversion to occur.

Why Would You Want to Convert on a Server Rather Than in the Browser?

Copied to clipboard

While there are some advantages of performing the conversion in a browser, conversion on a server is likely to be faster for complex large files and is likely to result in the same conversion every time. Converting a file relies, at least in part, on locally available fonts, so a browser-based conversion performed on one machine could potentially look different from the conversion of the same file that was performed on a different machine which had different fonts available.

Apryse offers solutions for both server-side and within-browser conversions, but in this example, we will just look at the server-side option.

Sample Project for Converting a Document to PDF

Copied to clipboard

The sample project is intended to show how a single document, in a hard coded location, can be converted to PDF.

You are likely to want to be able to specify which document is to be converted, and what you want to do with the PDF once the conversion is complete. As such the code should be considered as an example of how to convert a file and see the result, rather than as a template of how to write an entire document processing solution.

How to Get and Apryse SDK Trial Key

Copied to clipboard

If you don't already have an Apryse account, go to https://dev.apryse.com and register a new account. This allows Apryse to grant you a demo license key which will be used with the Apryse SDK to enable demo functionality.

Blog image

Figure 1- The Developer Portal

Log into https://dev.apryse.com with your registered account. The .NET Core SDK is available for Windows, Linux and macOS, so select the platform that you are using.

Click on the reveal button to get your personalized Trial key.

Blog image

Figure 2 - Download Center Platform and Trial Key.

There is a great guide to getting started available from https://docs.apryse.com/try-now/ which will lead you through the steps to creating an application on Windows, macOS or Linux, but in this blog I will create a simple app, from scratch, on Windows.

Blog image

Figure 3 - the start of the guide for getting started.

I will use VSCode for development and NuGet to install the Apryse SDK.

Although there are many options when creating PDFs from Office documents, and these are illustrated in the samples on the Apryse website, in this blog I will just use a very simple method for converting an Office document to PDF.

Prerequisites

Copied to clipboard

You will need VSCode with the C# extension installed.

Setting Up Your Project

• Create a new folder with the name.

• Open that folder within VSCode.

Blog image

Figure 4 - The empty project folder in VSCode.

• Within a terminal enter dotnet new console --framework net7.0. This will create a stub project.

Blog image

Figure 5 - The project folder after creating a stub project.

• In the terminal, download the Apryse library using dotnet add package PDFTron.NET.x64. This will install the latest version using NuGet.

• Copy the following code into program.cs. This is based on the code from https://docs.apryse.com/documentation/samples/dotnetcore/cs/OfficeToPDFTest/ but uses just the simplest conversion method. The code is extremely basic and doesn’t show any of the myriad of options that are supported. It is intended solely to show how simple conversion can be.

using pdftron;
using pdftron.SDF;
using pdftron.PDF;

namespace OfficeToPDFTestCS
{
  class Class1
{
  private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
  static Class1() { }

  static void SimpleConvert(String input_path, String output_path)
  {
    // Start with a PDFDoc (the conversion destination)
    using (PDFDoc pdfdoc = new PDFDoc())
    {
        // perform the conversion with no optional parameters
        pdftron.PDF.Convert.OfficeToPDF(pdfdoc, input_path, null);

        // save the result
        pdfdoc.Save(output_path , SDFDoc.SaveOptions.e_linearized);
        
        // And we're done!
        Console.WriteLine("Saved " + output_path);
    }
}

/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
        PDFNet.Initialize([Enter your key here]);
        try
        {
        SimpleConvert("./TestFiles/Fishermen.docx", "./TestFiles/Fishermen.pdf");
        }
        catch (pdftron.Common.PDFNetException e)
        {
        Console.WriteLine(e.Message);
        }
        catch (Exception e)
        {
        Console.WriteLine("Unrecognized Exception: " + e.Message);
        }
        
        PDFNet.Terminate();
        Console.WriteLine("Done.");
      }
    }
}
  • Now copy in the license key that you have previously downloaded.
  • Note: this sample code has a hardcoded location for the file that is to be converted, which should be located in a folder called TestFiles. The converted file will be placed into this folder.
Blog image

Figure 6 - The location where files to be converted should be placed, before the program has run.

• Run the program using dotnet run (or by using F5 within VSCode). After a few moments the files will appear and can be viewed.

Blog image

Figure 7 - The location where files to be converted should be placed, showing the newly created PDF after the program has run.

If you compare the original Word document and the newly created PDF, you can see that they look identical.

Blog image

Figure 8- the original document in Word.

Blog image

Figure 9 - The newly created PDF (shown in Chrome).

You could now use this code as the back-end to a website with the Word document being uploaded, and the converted PDF being returned.

Can the SDK do More than Convert just Word Documents to PDF?

Copied to clipboard

Absolutely.

In addition to converting from Word, the SDK can convert from Excel and PowerPoint to PDF, and even supports legacy Office formats: .doc, .xls and .ppt.

At its simplest, all that needs to be done to convert from other Office document types is to include the file extension when passing the file to the conversion method.

For example, an Excel Spreadsheet can be converted into a multiple page PDF with each page laid out in the same way as the original spreadsheet by copying the file to the TestFiles folder then using:

SimpleConvert("./TestFiles/Cashflow.xlsx", "./TestFiles/Cashflow.pdf"); 

The SDK is clever enough to know that .xlsx means that a conversion from Excel to PDF is required.

Blog image

Figure 11: An example Excel spreadsheet now converted to a PDF.

And PowerPoint presentations can be converted in a similar way into multi-page PDFs using:

SimpleConvert("./TestFiles/WW1Cryptography.pptx", "./TestFiles/WW1Cryptography.pdf");

Blog image

Figure 12: A PowerPoint presentation now converted to a PDF.

Further examples

Copied to clipboard

The method shown above is extremely simple. However, that means that it does not demonstrate all the options that are available. A more detailed code sample includes a ‘FlexibleConvert’ option which allows conversion options to be set, as well as demonstrating how the code can be used within a multithreaded environment to monitor and cancel conversions.

Conclusion

Copied to clipboard

Apryse offers a simple mechanism for converting Office documents to PDF without Office needing to be installed. This can be done with just a few lines of code that use default options. More complex options exist to allow the conversion mechanism to be tailored to your requirements.

These powerful conversion capabilities, coupled with the ease of integration provided by its C# library, make it the best choice for developers aiming to enhance their document processing workflows. Whether you're building a document management system, an online collaboration platform, or any other application involving Office documents, Apryse can help you provide a seamless and efficient conversion process.

With Apryse's capabilities at your disposal, you can enhance your application's functionality and provide users with a reliable way to convert and work with Office documents in PDF format.

In addition to converting Office documents to PDF, Apryse offers many tools for editing and handling both Office Documents and PDFs, including converting PDFs into Office documents.

When you are ready to get started, see the documentation for the SDK to get started quickly. Don’t forget, you can also reach out to us on Discord if you have any issues.

Sanity Image

Apryse

Share this post

email
linkedIn
twitter