When working with Word documents in .NET Standard 2.0 applications, there are times when it is necessary to store data in non-Word formats. One of the most popular formats is PDF because of its ability to preserve formatting and the security of the document itself. Programmatically saving a file through GcWord C#/VB .NET API for PDF is the ideal way to accomplish this.
You may have designed Word documents from other data sources and need to create documents such as:
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 various 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 quick and straightforward.
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.
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 to compile and run the steps below successfully.
Learn how to use GcWord.
After completing this example application, you should have a Word document that looks like the below:
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
Visual Studio for MAC
Visual Studio Code for Linux
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):
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.
Instead of saving to Word, you can directly save the Word document to PDF with the following PdfOutputSettings:
Use the C# .NET and GcWordLayout class code snippet below to save the document to PDF:
Run the application. The new PDF should look like this:
Thanks for following along! If you have any questions about the new features or how to use any GrapeCity Documents APIs, please contact us directly or leave comments on this blog.