PDF to Word – Convert PDF to Word in C#

PDF to Word

In everyday routine, a large number of documents are produced in Portable Document Format (PDF). One of its salient features is that it prevents document formatting on all platforms including desktop or mobile. However, we cannot directly modify the PDF files and for that purpose, we need to use some PDF manipulation software which include installation and licensing costs. One of the viable solutions is the conversion of PDF files to Word formats. In this article, we are going to discuss the steps and details on how to convert PDF to Word using .NET REST API.

PDF processing REST API

Aspose.PDF Cloud is an amazing REST-based API offering the capabilities to create, manipulate and render PDF
files to other formats including DOC, DOCX, XLSX, PPTX, HTML, etc formats. Owing to its REST architecture, it can be used on any platform including desktop, web, mobile, and any operating system such as Windows, macOS, Linux, etc. So you get similar performance and experience on all the platforms. No additional software download and installation is required.

Now in order to further facilitate our users, we have created specific programming SDKs which are wrapped around the Cloud API. So in case you need the PDF processing capabilities within the .NET application, Aspose.PDF Cloud SDK for .NET is the solution. The first step is to install the SDK which is available for download at NuGet and GitHub. Hence, execute the following command in the NuGet package manager to install the latest release of SDK.

Install-Package Aspose.Pdf-Cloud

Referencing within VisualStudio

We can also add the SDK directly inside the Visual Studio project. Therefore, expand the project tree in Solution Explorer, right-click the Packages folder, and select Manage NuGet Packages… option from the context menu.

Manage NuGet packages
Image 1:- Manage NuGet packages.

Now search Aspose.PDF Cloud in the search field, enable the checkbox beside the package name and click Add Package button.

Aspose.PDF Cloud
Image 2:- Aspose.PDF Cloud SDK in NuGet packages.

Notice that Aspose.Pdf-Cloud.dll appears under the Packages folder.

Aspose.Pdf-cloud.dll
Image 3:- Aspose.Pdf-cloud.dll under solution explorer.

Configurations

In order to access the API, you need to create a free account by visiting Aspose.Cloud dashboard and test our APIs up to 150 free document processing calls. You may Sign In using your existing GitHub or Google account or, click on the Create a new Account button and provide the required information. Then login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

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

Once you are satisfied with the API, you may consider opting for a license purchase which is quite flexible. You are only charged for the requests you have made to the server and it can be as low as $0.005 / API Call. You may visit the pricing page for further details.

PDF to DOCX in C#

Please follow the steps specified below to perform the conversion of PDF files to Word document (DOC) format in the C# .NET application. After the conversion, the output is stored in steam instance which can be later saved on local system.

  • Firstly, define Client ID and Client Secret details using string objects
  • Secondly, initialize the PdfApi object while passing Client ID and Client Secret details as arguments
  • Thirdly, read the file content and upload to Cloud storage using UploadFile(..) method of PdfApi
  • Now we need to call the GetPdfInStorageToDoc(…) method which takes PDF file reference from cloud storage and other option parameters such as format, maxDistanceBetweenTextLines, conversion mode etc
  • Since the output is in steam, so we can use File.Create to save it over system drive
PDF to docx conversion
Image 1:- PDF to Docx conversion preview.

Please visit the following links to download HtmlExample1.pdf and Converted.docx.

Convert PDF to DOCX – Output in Cloud storage

In this section, we are going to discuss the process of loading PDF document available in Cloud storage, perform its conversion to DOCX format and save the output in Cloud storage. Please follow the instructions specified below to accomplish this task:

  • First create an instance of PdfApi by passing Client ID and Client secret details as arguments
  • Load the PDF file from local system and upload to cloud storage using UploadFile(..) method of PdfApi (this is optional step and can be used if you do not have any PDF already in Cloud storage)
  • Create. a string variable defining resultant file name
  • Finally, call the PutPdfInStorageToDoc(..) which takes input PDF name, output file name and other optional parameters

PDF to Word conversion with cURL command

The cURL commands are an amazing mechanism for accessing the REST APIs. All you need to know is API end points, the required parameters and you can accomplish your tasks right within the command line terminal. In following section, we are going to perform the PDF file conversion to DOCX format using cURL command.

Since our APIs are only accessible to authorized persons, so the first step is to generate a JSON Web Token (JWT) based on ClientID and ClientSecret details retrieved from Aspose.Cloud dashboard. Please execute the following command in the terminal to generate the JWT token.

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"

Once we have the JWT token, we can execute the following command to perform conversion of PDF file already available in cloud storage and also save the output file in cloud storage.

curl -X PUT "https://api.aspose.cloud/v3.0/pdf/HtmlExample1.pdf/convert/doc?outPath=converted.docx&addReturnToLineEnd=true&format=DocX&mode=Textbox" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

Conclusion

This article has explained some amazing capabilities of Aspose.PDF Cloud for the conversion of PDF files to Word formats. With couple of code lines, you can accomplish this requirement. We have also discussed how we can access the same Cloud API through cURL commands over the terminal. Apart from conversion, it also provides other exciting features and their details can be found over Aspose.PDF Cloud Features.

Since our Cloud SDKS are developed under MIT License, so their complete code snippet can be downloaded from GitHub. In case you encounter any issue while using the API or you have any related query, please feel free to contact via Free Support forum.

Related articles

We also recommend visiting following links to learn more about