Word to PDF – Convert Word to PDF in C#

Word to PDF conversion

MS Word files including DOC, DOCX, DOCM, DOTX, ODT, OTT, etc are some of the popular file formats for information storage and data sharing. Many Universities, government, or public organizations produce their artifacts in Word format but due to such a variety of formats, the layout of content in these files is compromised when viewing on various platforms. Therefore, one viable solution is the conversion to a fixed format where the document isn’t editable. So once you save it as PDF or XPS, the document structure and fidelity are preserved on all platforms. In this article, we are going to discuss the conversion of Word to PDF format using the .NET API.

Word processing API

Aspose.Words Cloud is an amazing REST-based API providing the ease to create, edit and convert Word documents to other Supported File Formats on any platform. The Cloud architecture provides the flexibility to add Word-processing capabilities in any application including Desktop, Web, and Mobile. So, you can implement the Word document manipulation features within Windows, Linux, iOS, Android applications with minimal effort. Furthermore, we have also created language-specific programming SDKs around the REST API so that you get all these features in a consistent manner with all language standards.

Aspose.Words Cloud SDK for .NET is a wrapper around Aspose.Words Cloud offering the features to open, create, edit, merge, split, compare and convert a Word document in .NET. In this blog, we are going to discuss the Word document conversion to PDF format using the same SDK.

Installation

Please execute the following command to on command line terminal to install Aspose.Words Cloud SDK for .NET.

nuget install Aspose.Words-Cloud

In order to install the SDK from NuGet package manager, please follow the steps specified below:

  • Expand solution explorer and right-click Packages folder
  • Enter Aspose.Words-Cloud in search field
  • Now enable checkbox near Aspose.Words-Cloud in searched results and click Add Package button
  • The SDK referenced is added in project
Manage NuGet packages
Image 1:- Manage NuGet packages menu option.
NuGet package search
Image 2:- Aspose.Words Cloud NuGet package.
Aspose.Words Cloud SDK
Image 3:- Aspose.Words Cloud SDK in project reference.

SDK dependencies

Cloud subscription

Our APIs are only accessible to authorized customers and we provide a free sign-up service. No credit card or payment details are required during the sign-up process. So a free account can be created by visiting Aspose.Cloud dashboard and test our APIs up to 150 free document processing calls. You get the convenience to Sign In using your existing GitHub or Google account or, click on the Create a new Account button and provide the required information. The next step is to log in over the dashboard using your credentials and expand the Applications section from the dashboard. Scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Client Credentials
Image 4:- Client credentials on the dashboard.

How to convert Word to PDF in C#

Please follow the steps specified below to accomplish Word to PDF conversion using C# .NET code in Visual Studio. In this scenario, the input Word file is already available in Cloud storage and the resultant PDF is also stored in Cloud storage.

  • Create an instance of Configuration class which takes Client ID and Client Secret details as arguments
  • Secondly, create an instance of WordsApi class and provide configuration instance as argument
  • Thirdly, upload the file from local system to Cloud storage using UploadFileRequest(..) method
  • The next step is to create an object of SaveOptionsData which defines resultant file properties. Using this object, we define SaveFormat as Pdf, the value for ZipOutput is specified as false so that output is not in .zip archive and other proprerties
  • Now create an instance of SaveAsRequest which takes input file name and SaveOptionsData objects as arguments
  • Finally, call the SaveAs(…) method of WordApi class which takes SaveAsrequest instance as argument
word to pdf preview
Image 5:- Word to PDF conversion preview.

The sample file used in the above example can be download from test_multi_pages.doc.

Convert DOC to PDF in C#

In this scenario, we are going to convert the DOC file uploaded from local storage and convert the output to PDF format. The resultant file is also stored in Cloud storage.

  • Firstly, create an instance of Configuration class which takes Client ID and Client Secret details as arguments
  • Secondly, create an instance of WordsApi class and provide configuration instance as argument
  • Thridly, create an object of ConvertDocumentRequest class which takes local file stream, output format and resultant file name as arguments
  • Finally, call the ConvertDocument(..) method to perform the conversion operation

DOCX to PDF conversion in C#

Lets discuss the scenario to convert DOCX file available in Cloud storage and also save the resultant PDF in Cloud storage. In this scenario, we are going to use HTTP GET request method.

  • Firstly, create an instance of Configuration class which takes Client ID and Client Secret details as arguments
  • Secondly, create an instance of WordsApi class and provide configuration instance as argument
  • The next step is to upload the file from local drive to cloud storage using UploadFileRequest(…) method of WordsApi
  • Penultimate, create an object of GetDocumentWithFormatRequest class which takes DOCX file name, output format and name of resultant file as arguments
  • Finally, call the GetDocumentWithFormat(…) method to perform the conversion.

You may consider downloading the resource files used in above example from file-sample_1MB.docx and result.pdf.

docx to pdf conversion preview
Image 6:- DOCX to PDF conversion preview.

Word to PDF using cURL command

Apart from Cloud SDK in .NET application, we can also access the Cloud API through cURL commands. However, in order to use APIs via cURL commands, we need to first generate a JWT access token based on Client ID and Client Secret details.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=4ccf1790-accc-41e9-8d18-a78dbb2ed1aa&client_secret=caac6e3d4a4724b2feb53f4e460eade3" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now in order to perform the conversion, we are going to use GetDocumentWithFormat API which expects the input DOCX file to be available in Cloud storage. Please execute the following command to upload the file to cloud storage.

curl -X PUT "https://api.aspose.cloud/v4.0/words/storage/file/file-sample_1MB.docx" \
-H  "accept: application/json" \
-H  "Authorization: Bearer <JWT Token>" \
-H  "Content-Type: multipart/form-data" \
-d {"fileContent":{}}

Now to we can use the GetDocumentWithFormat API to perform DOCX to PDF conversion.

curl -X GET "https://api.aspose.cloud/v4.0/words/file-sample_1MB.docx?format=pdf&outPath=myOutput.pdf" \
-H  "accept: application/octet-stream" \
-H  "Authorization: Bearer <JWT Token>

Conclusion

We have discussed some amazing features of Word file conversion to PDF format using Aspose.Words Cloud. The conversion can be performed either using .NET code or through cURL command. Another approach is Aspose.Words Cloud API Reference which is the easiest way to try out our APIs right away in your browser!

Please note that the complete source code of SDK is available for download at GitHub and in case you encounter any issue while using the API, please feel free to contact through Free product support forum.

Related artciles