When working with Word documents in .NET Standard 2.0 applications, there will be times when it is necessary to store data in non-Word formats. Programmatically saving a file through C#/VB .NET API for PDF, is the ideal way to accomplish this for many reasons.

Why convert a Word Document to PDF?

  • You want the format of your documents to be consistent. A PDF will keep all original formatting, regardless of the operating system or computer used to open the file.
  • You need long-term data preservation. Working with PDF formats will allow you to share, collaborate, and ensure the security of the content within your documents, long-term.
  • You may have designed Word documents from other data sources and need to create documents such as:
    • Invoice slips
    • Newsletters
    • Journals
    • Project Plans
    • Articles

GrapeCity Documents for Word (GcWord) is a Word API offering a complete C#/VB .NET solution to program and work with Word documents, with zero dependencies on Microsoft Office.

With the GcWord API, developers create powerful document conversions for archival and delivery systems in .NET Standard 2.0 targeted applications, all with the ability to programmatically create these conversions on a variety of operating systems including Windows, Mac, and Linux.

GcWord's feature-rich object model is based on Microsoft Office API, Word JavaScript API, and OpenXML SDK. The architecture is simple and easy to use so creating DOCX files through C#/VB .NET code, loading DOCX files, and accessing the object model is simple and quick. You can add, remove, and modify objects (and their properties like formatting), save a file to a DOCX file, or export it to PDF. You can do all this in any .NET Standard 2.0 targeted application.

In this demo, we'll cover:

  • How to use the C# .NET API to load the Word document (.docx) file in GcWord
  • Use the PdfOutputSettings method of the C# .NET API to export the Word document to PDF

NOTE: Please be sure to review and/or create the sample code from the previous article: install GcWord in a .NET Core console application and create a Word document in code. Important dependencies and references will be required from this project in order to successfully compile and run the steps below.

Step 1: Install GcWord in .NET Core Console application

Learn how to install GcWord in a .NET Core console application and create a Word document in code, using GcWord.

After completing this example application, you should have a Word document that looks like the below:
Convert Word Documents to PDF in .NET Applications

Step 2: Build on existing Example - Add Nuget package

In order to export a Word document to PDF, you need to download and install GrapeCity.Documents.Word.Layout Nuget Package.

Follow these steps:

Visual Studio for Windows

  1. Right-click the project in Solution Explorer and choose Manage NuGet Packages
  2. In Package source in the top right, select nuget.org
  3. Click Browse tab in top left and enter "GrapeCity.Documents" as the search string. You should see several GrapeCity.Documents packages listed
  4. Select GrapeCity.Documents.Word.Layout, and click Install. Accept the license agreement

Visual Studio for MAC

  1. In the Package source on the top left, choose nuget.org
  2. Click Browse tab on the top right and search for "Grapecity.Documents"
  3. On the left panel, choose GrapeCity.Documents.Word.Layout
  4. On the right panel, click Install
  5. Choose 'I Accept' in the next screen

Visual Studio Code for Linux

  1. From Extensions, install the Nuget Package Manager and activate it
  2. In Visual Studio Code, press Ctrl+P to open the file command box, type > in it, find "Nuget Package Manager: Add Package" in the list that opens, and click it
  3. In the search box that opens, type "GrapeCity" and press Enter. This should bring up the list of available GrapeCity packages, GrapeCity.Documents.Word.Layout among them
  4. Select it and then the build number. The package will be added to the application

Step 3: Add Namespace

Make sure you have the following namespaces included in Program.cs file (if you are building on the previous application, some of these should already be in your code):


using GrapeCity.Documents.Word;
using System.Drawing;
using GrapeCity.Documents.Word.Layout;
using System.IO.Compression;

Step 4: Load a Word document (.docx) file in GcWord

Add the following lines of code in the main function of Program.cs. This code will create a GcWord document object. Then load the Word document into the object.

var wordDoc = new GcWordDocument();

var path = Path.Combine(@"ImportanceOfWetlands.docx");

wordDoc.Load(path);

Step 5: Export the Word document to PDF with various PdfOutputSettings

Instead of saving to Word, you can directly save the Word document to PDF with the following PdfOutputSettings:

  • CompressionLevel: This specifies values that indicate whether a compression operation emphasizes speed or compression size. It takes the CompressionLevel Enum to set different values
  • PdfAConformanceLevel: It gets or sets the PDF/A conformance levels with the help of PdfAConformanceLevel Enum.
  • DocumentInfo: The DocumentInfo object contains various information about the document like author, title, creation date, etc.
  • ImageOptions: It gets and sets the ImageOptions object that contains options controlling how images are processed in the current document
  • Linearized: It indicates whether the generated PDF should be saved as linearized
  • Metadata: It gets or sets the metadata associated with the document
  • PdfVersion: It gets or sets the PDF Version of the generated document
  • By default, the version is determined automatically based on which features are used in this document. Setting this property to a non-null string in the format "1.X" (where X is a digit from 0 to 9) overrides the automatic value with the specified one
  • BackColor: It gets or sets the background color. The default is white

Use the C# .NET and GcWordLayout class code snippet below to save the document to PDF:

using (var layout = new GcWordLayout(wordDoc))

   {

     PdfOutputSettings pdfOutputSettings = new PdfOutputSettings();

     pdfOutputSettings.CompressionLevel = CompressionLevel.Fastest;

     pdfOutputSettings.ConformanceLevel = GrapeCity.Documents.Pdf.PdfAConformanceLevel.PdfA1a;

     pdfOutputSettings.BackColor = Color.LightGray;

     pdfOutputSettings.DocumentInfo = new GrapeCity.Documents.Pdf.DocumentInfo()

     {   Author = "John",

         Title = "Deforestation",

        CreationDate = new GrapeCity.Documents.Pdf.PdfDateTime(DateTime.Now)

     };

    layout.SaveAsPdf("SimpleText.pdf", null, pdfOutputSettings);

   }

Run the application. Your PDF should look like this:

Convert Word Documents to PDF in .NET Applications

Thanks for following along! If you have any questions about the new features, please leave them in the comments below.

Happy Coding!

Try a GcPdf free trial for 30 days

Download the latest version of GrapeCity Documents for PDF

Download Now!