Aspose.PDF Cloud: Converting documents using CSharp SDK (part 2)
Tags: csharp, rest, tutorial, beginners
Published: 2018-06-26 20:50:03

---
title: Aspose.PDF Cloud: Converting documents using CSharp SDK (part 2)
published: true
description: This is a C# example to convert PDF file in other formats via Aspose.PDF Cloud 
tags: csharp, REST, tutorial, beginner
---

In [the previous part](https://dev.to/andruhovski/asposepdf-for-cloud-converting-documents-using-c-sdk-4a9d) we considered a process of conversion from other formats to PDF. Now, we continue exploring API of Aspose.PDF Cloud and we will see how to PDF docs can be converted to other formats: DOC/DOCX, EPUB, HTML, LaTeX, TIFF, PPTX, SVG, XLS. Obviously, that conversion process is specific for each format, therefore, we first consider the general algorithm and then tune up additional params for some formats.

Unlike the previous post, we will use an ASP.NET MVC5 application template. This will allow us to see both sync and async methods in action.

So, let assume that we need to write an ASP.NET MVC5 application that shows the content of some folder from cloud storage to the user and he can convert PDF files from that folder to appropriate format.

## Step 0. Basic preparations

* Add new ASP.NET Web Application with name `MvcConverter` and  select `No Authentication` and `MVC` template options
* Add packages for Aspose.Cloud:
    * `Install-Package Aspose.Storage-Cloud`
    * `Install-Package Aspose.PDF-Cloud`
* Update other packages
    * `Update-Package`
* Update layout in `_Layout.cshtml`

{% gist https://gist.github.com/andruhovski/fc6b7b2561bb17fdde0e7c5457c6d12d %}

## Step 1. Setup API client
We will create an additional controller class (i.g. `AsposeCloudController`) for more convenient work with the API.

    public class AsposeCloudController : Controller
    {
        private const string AppSid = "*** Put App Sid here ***";
        private const string AppKey = "*** Put App Key here ***";

        protected readonly StorageApi _storageApi;
        protected readonly PdfApi _pdfApi;

        public AsposeCloudController()
        {
            _storageApi = new StorageApi(AppKey, AppSid);
            _pdfApi = new PdfApi(AppKey, AppSid);
        }
    }

## Step 2. Modify HomeController

By default, ASP.NET MVC5 application template has `HomeController` with 3 actions: `Index`, `About`, `Contacts`. Let's change `Index` so that the list of files is displayed.

    public class HomeController : AsposeCloudController
    {
        protected string StorageName = "First Storage";
        protected string FolderName = "pdf-demo";
        public ActionResult Index()
        {
            // Check if the folder exists
            var fileExistResponse = _storageApi.GetIsExist(
                new GetIsExistRequest(FolderName, null, StorageName));
            if (fileExistResponse.Status.Equals("OK"))
            {
                if (fileExistResponse.FileExist.IsFolder == false)
                {
                    return View("ApiError", new AsposeResponse
                    {
                        Code = fileExistResponse.Code,
                        Status = "Folder not found"
                    });
                }
            }
            else
            {
                return View("ApiError", fileExistResponse);
            }
            // Get the filelist
            var filesResponse = _storageApi.GetListFiles(    
             new GetListFilesRequest(FolderName, StorageName));
            if (filesResponse.Status.Equals("OK"))
                return View("ApiError", filesResponse);
            // Get .pdf files only
            var filesInCloudFolder =
                filesResponse.Files.Where(f => f.IsFolder == false || f.Name.EndsWith(".pdf",
                                            StringComparison.OrdinalIgnoreCase)).ToList();
            return View(filesInCloudFolder);
        }
    //... other actions are omitted
    }

Also, we need to create an `Index` view. We will show the data in a tabular form. One table row will contain the name, size, modified date of one file and links for converter actions.

{% gist https://gist.github.com/andruhovski/2b05f2437129416e3eb8bb9ac0e00cb0 %}

## Step 3. Add converter action

As you can see in the previous step we will use `ConvertPDF` action with 2 parameters: `filename` and desired `format`. 

    public ActionResult ConvertPDF(string filename, string format)
    {
        if (string.IsNullOrEmpty(filename) || string.IsNullOrEmpty(format))
            return RedirectToAction("Index");
        format = format.ToLowerInvariant();           
        try
        {
            string outPath;
            switch (format)
            {
                case "tiff":
                    outPath = $"{FolderName}/{format}/{filename.Replace("pdf", "tif")}";
                    _pdfApi.PutPdfInStorageToTiff(filename, outPath, folder: FolderName);
                    break;

                case "docx":
                    outPath = $"{FolderName}/{format}/{filename.Replace("pdf", "docx")}";
                    _pdfApi.PutPdfInStorageToDoc(filename, outPath, folder: FolderName, format: "docx");
                    break;                    
                case "pdfa":
                    outPath = $"{FolderName}/{format}/{filename}";
                    _pdfApi.PutPdfInStorageToPdfA(filename, outPath, type: "PDFA1A", folder: FolderName);
                    break;
                case "html":
                    outPath = $"{FolderName}/{format}/{filename.Replace("pdf", "zip")}";
                    _pdfApi.PutPdfInStorageToHtml(filename, outPath, folder: FolderName);
                    break;
                case "svg":
                    outPath = $"{FolderName}/{format}/{filename.Replace("pdf", "zip")}";
                    _pdfApi.PutPdfInStorageToSvg(filename, outPath, folder: FolderName);
                    break;
            }
        }
        catch (ApiException ex)
        {
            ViewBag.ErrorCode = ex.ErrorCode;
            ViewBag.ErrorMessage = ex.Message;
            return View();
        }

        ViewBag.Success = true;
        return View();
    }

Despite the fact that we use the similar calls  *PutPdfInStorageTo...* to convert to different formats, I  must add some explanations. 
In the simplest case, it's enough to call *PutPdfInStorageTo...* with two parameters: input filename and output file path. Other parameters are optional and they used as necessary, eg. *folder* - used, when we list the files in subfolders (not in root).

It should also be understood that the result of the conversion is not always a one-page document and some formats are multipage in their nature, but others are not. Also, we can't store additional resources in HTML. So if we will convert to TIFF or DOC/DOCX, we no need to care about multipage and resource storing, but for HTML or SVG we need it. Obviously, for the last two formats, we need some container. Aspose.PDF Cloud uses ZIP-archive as the container. 

The last thought: some formats need to clarify their subtypes. This concerns, for example, to Microsoft Word DOC/DOCX or PDF/A-1a, PDF/A-1b.

## Final step. View results and handle error

In this demo, we have a pretty straightforward view that only display a static message with result status.

{% gist https://gist.github.com/andruhovski/908777a0a2eb7af56c6cebd861ba0c16 %}

**To sum up:** we considered a general algorithm how to use Aspose.PDF Cloud conversion API in ASP.NET MVC applications. What's next? In the next part, we will learn how to tune up the converter from PDF to other formats.