Word to PDF

How to Convert Word to PDF Online

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 viewed on various platforms. Therefore, one viable solution is the conversion to a fixed format that preserves the document structure and fidelity on any platform. In this article, we are going to discuss the conversion of Word to PDF format using the .NET API.

Word to PDF Conversion 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. As per the article’s scope, we are going to use Aspose.Words Cloud SDK for .NET for Word to PDF conversion.

Installation

Please execute the following command on the command line terminal to install the SDK.

nuget install Aspose.Words-Cloud

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

  • Expand solution explorer and right-click the Packages folder
  • Enter Aspose.Words-Cloud in the search field
  • Now enable the checkbox near Aspose.Words-Cloud in searched results and click Add Package button
  • The SDK referenced is added in the project

We also need to create a free account by visiting Aspose.Cloud dashboard where we can sign in using our existing GitHub or Google account or, can Create a new Account. Simply scroll down towards the Client Credentials section to see Client ID and Client Secret details.

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 the Configuration class that takes Client ID and Client Secret details as arguments
  • Secondly, create an instance of WordsApi class and provide a configuration instance as an argument
  • Thirdly, upload the file from the 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 the .zip archive and other properties
  • Now create an instance of SaveAsRequest which takes the input file name and SaveOptionsData objects as arguments
  • Finally, call the SaveAs(…) method of WordApi class which takes the SaveAsrequest instance as an argument
word to pdf preview

Image 5:- Word to PDF conversion preview.

The sample file used in the above example can be downloaded 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 that takes Client ID and Client Secret details as arguments
  • Secondly, create an instance of WordsApi class and provide a configuration instance as an argument
  • Thirdly, create an object of ConvertDocumentRequest class that takes the local file stream, output format, and resultant file name as arguments
  • Finally, call the ConvertDocument(..) method to perform the conversion operation

DOCX to PDF in C#

Let’s discuss the scenario to convert a 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 that takes Client ID and Client Secret details as arguments
  • Secondly, create an instance of WordsApi class and provide a configuration instance as an argument
  • The next step is to upload the file from the local drive to cloud storage using the UploadFileRequest(…) method of WordsApi
  • Penultimate, create an object of GetDocumentWithFormatRequest class which takes the DOCX file name, output format, and name of the resultant file as arguments
  • Finally, call the GetDocumentWithFormat(…) method to perform the conversion.

You may consider downloading the resource files used in the 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

In this section, we are going to access the Cloud API through cURL commands. So the first step is to 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 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":{}}

Finally, 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 converting Word to PDF format using Aspose.Words Cloud. The conversion can be performed either using the .NET code or through the cURL command. Furthermore, you may also explore the API through Swagger UI in your browser.

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