If you are working with Word documents in code in .NET Standard 2.0 targeted applications, there will be times when you do not want to store your data in a Word format. A PDF (Portable Document Format) is an ideal format for a variety of reasons.
Why convert a Word document to PDF?
- You do not have Microsoft Office installed on your system, but still want to print or distribute your Word documents.
- 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
- Project Plans
GrapeCity Documents for Word (GcWord) is a Word API that offers a complete solution to program and work with Word documents, with zero dependancies on Microsoft Office.
With GcWord, developers can create powerful document conversions for archival and delivery systems in .NET Standard 2.0 targeted applications. You can convert Word documents to PDF on all three operating systems -- Windows, Mac, and Linux.
In this demo, we'll cover:
- How to load the Word document (.docx) file in GcWord
- Export the Word document to PDF
Step 1: Exporting Word documents to PDF
Learn how to install GcWord in a .NET Core console application and create a Word document in code, using GcWord.
At the end of this blog, your Word document would look like this:
Step 2: Add namespace
In order to export a Word document to PDF, you need to download and install GrapeCity.Documents.Layout Nuget Package.
Follow these steps:
Visual Studio for Windows
- Right-click the project in Solution Explorer and choose Manage NuGet Packages.
- In Package source in the top right, select nuget.org.
- Click Browse tab in top left and enter "GrapeCity.Documents" as the search string. You should see several GrapeCity.Documents packages listed.
- Select GrapeCity.Documents.Layout, and click Install. Accept the license agreement.
Visual Studio for MAC
- In the Package source on the top left, choose nuget.org.
- Click Browse tab on the top right and search for "Grapecity.Documents."
- On the left panel, choose GrapeCity.Documents.Layout.
- On the right panel, click Install.
- Choose 'I Accept' in the next screen.
Visual Studio Code for Linux
- From Extensions, install the Nuget Package Manager and activate it.
- 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.
- In the search box that opens, type "GrapeCity" and press Enter. This should bring up the list of available GrapeCity packages, GrapeCity.Documents.Layout among them.
- Select it and then the build number. The package will be added to the application.
Step 3: Load a Word document (.docx) file in GcWord
Add following lines of code in main function of Program.cs. This code will create a GcWord document object. Then load the Word document into it.
var doc = new GcWordDocument(); var path = Path.Combine(@"ImportanceOfWetlands.docx"); doc.Load(path);
Step 4: Export the Word document to PDF
Instead of saving to Word, you can directly save the Word document to PDF using following code:
Run the application. Your PDF would look like this:
Watch the video tutorial
It's that simple! We hope you like this feature. Do you have a question or comment? Please leave it in the thread below.
Export your Word document to a PDf in .NET Apps Download GrapeCity Documents for Word
Export your Word document to a PDf in .NET Apps
Download GrapeCity Documents for WordDownload Now!