DEV Community

Andriy Andruhovski for Aspose.PDF

Posted on • Edited on

3

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.

using Aspose.Pdf.Cloud.Sdk.Api;
using Aspose.Storage.Cloud.Sdk.Api;
namespace BatchConverting
{
class Program
{
private const string AppSid = "************************************"; //demo value, please change it
private const string AppKey = "************************************"; //demo value, please change it
private const string StorageName = "First Storage";
private const string FolderHTML = "Html-Demo";
private const string FolderPDF = "Pdf-Demo";
private static readonly PdfApi PdfApi = new PdfApi(AppKey, AppSid);
private static readonly StorageApi StorageApi = new StorageApi(AppKey, AppSid);
private static void Main()
{
BatchConversionHtml2PdfEx01();
}
private static void BatchConversionHtml2PdfEx01()
{
//TODO: Place for example code
}
}
}

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.

private static void BatchConversionHtml2PdfEx01()
{
var filesResponse = StorageApi.GetListFiles(new GetListFilesRequest(FolderHTML, StorageName));
foreach (var item in filesResponse.Files.Where(f=>f.IsFolder==false && f.Name.EndsWith(".html")))
{
//...............
}
}
view raw Program.cs hosted with ❤ by GitHub

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:

private static void BatchConversionHtml2PdfEx01()
{
var filesResponse = StorageApi.GetListFiles(new GetListFilesRequest(FolderHTML, StorageName));
foreach (var item in filesResponse.Files.Where(f=>f.IsFolder==false && f.Name.EndsWith(".html")))
{
try
{
var documentResponse = PdfApi.PutCreateDocument(
name: $"{Path.GetFileNameWithoutExtension(item.Name)}.pdf",
templateFile: item.Path,
templateType: "html",
folder: FolderPDF,
storage: StorageName);
Console.WriteLine($"{documentResponse.Code} {documentResponse.Status}");
}
catch (Aspose.Pdf.Cloud.Sdk.Client.ApiException ex)
{
dynamic errContent = JObject.Parse(ex.ErrorContent);
Console.WriteLine(ex.ErrorCode);
Console.WriteLine(errContent.Message);
}
}
}
view raw Program.cs hosted with ❤ by GitHub

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

private static void BatchConversionJpeg2PdfEx01()
{
var filesResponse = StorageApi.GetListFiles(new GetListFilesRequest(FolderHTML, StorageName));
foreach (var item in filesResponse.Files.Where(f => f.IsFolder == false && f.Name.EndsWith(".jpg")))
{
try
{
var documentResponse = PdfApi.PutCreateDocument(
name: $"{Path.GetFileNameWithoutExtension(item.Name)}.pdf",
templateFile: item.Path,
templateType: "jpg",
folder: FolderPDF,
storage: StorageName);
Console.WriteLine($"{documentResponse.Code} {documentResponse.Status}");
}
catch (Aspose.Pdf.Cloud.Sdk.Client.ApiException ex)
{
dynamic errContent = JObject.Parse(ex.ErrorContent);
Console.WriteLine(ex.ErrorCode);
Console.WriteLine(errContent.Message);
}
}
}
view raw Program.cs hosted with ❤ by GitHub

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.

private static void BatchConversionJpg2PdfEx02()
{
var filesResponse = StorageApi.GetListFiles(new GetListFilesRequest(FolderHTML, StorageName));
var images = new ImagesListRequest(
filesResponse.Files.Where(f => f.IsFolder == false && f.Name.EndsWith(".jpg"))
.Select(fn => fn.Path).ToList());
try
{
var documentResponse = PdfApi.PutCreateDocumentFromImages(
name: "demo.pdf",
images: images,
folder: FolderPDF,
storage: StorageName);
Console.WriteLine($"{documentResponse.Code} {documentResponse.Status}");
}
catch (Aspose.Pdf.Cloud.Sdk.Client.ApiException ex)
{
Console.WriteLine(ex.ErrorCode);
Console.WriteLine(ex.Message);
}
}
view raw Program.cs hosted with ❤ by GitHub

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