DEV Community

Andriy Andruhovski for Aspose.PDF

Posted on • Edited on

Aspose.PDF Cloud: Converting documents using C# SDK

In the previous post, we taught upload files to Aspose.Cloud. Now we will consider basic steps to convert documents with Aspose.Cloud API v1.0.
For the first example, we will use converting HTML to PDF. Let assume for easy that source files will be located in "Html-Demo" folder on the default storage and we will store resultant files on "Pdf-Demo" folder.

As in the previous post, we create a console application, but now we will add two packages:

  • to manage storage (Install-Package Aspose.Storage-Cloud)
  • to manipulate documents (Install-Package Aspose.PDF-Cloud)

UPD: This post is valid for Aspose.PDF-Cloud v.18.8

The next step is to declare the necessary fields and constants in our class.

So, we are ready to convert our files from HTML to PDF. To get a list of files we call a GetListFiles method and filter the results.

All that's left to do is call a conversion API method PutCreateDocument with appropriate parameters. It should be noted that this method has parameters for several cases of converting, but we set values required only for this one:

  • name - destination filename (only name, not a path);
  • templateFile - the source file path;
  • templateType - the content type of source file, can be xml, html, bmp, jpg, png, tiff, emf, cgm, tex;
  • folder - destination folder;
  • storage - storage name.

In this case, we deal with an ApiException. This class is similar to the generic exception, but it has an additional field ErrorContent. This field usually holds JSON-string with the error message.

The following snippet holds the full code of our function:

As you can see above, we can convert to PDF from different file types. Let's try to change HTML to JPEG:

In this case, we get the PDF document too, but with raster image per file.
To combine a series of images into one PDF we need to call PutCreateDocumentFromImages method. The following snippet shows how we can combine JPEG files into one PDF. To reduce the explanation, named arguments were used.

So, in this post, we were considered how to create PDF from different formats. In the next article, we will consider the reverse process - converting from PDF to another file formats.

Top comments (2)

Collapse
 
rosberg_guedes profile image
Rosber Guedes

That's amazing. Thanks for sharing!

Collapse
 
iditectweb profile image
iditectweb

one more .NET pdf library, support converting html to pdf using c# code