This quick tutorial guides you on how to convert JPG to PDF format with C# REST API. It shares the details to set the development environment, a list of steps to write the application, and a sample code for changing a JPEG to a PDF with C# Low Code API using a .NET-based cloud SDK. You will learn to convert a single or list of images to PDF where each image becomes a page in the newly created PDF.
Prerequisite
- Create an account API credentials export JPG to PDF
- Download Aspose.PDF Cloud SDK for Dotnet to convert a JPEG file to PDF
- Setup C# project with the above SDK for changing a JPEG to a PDF
Steps to Convert a JPG to a PDF with C# REST Interface
- Configure the PdfApi class object using the App key and Sid for converting JPG images to PDF
- Upload the image(s) to the Cloud storage with unique names
- Create the ImageTemplate objects for each image
- Create the list of ImageTemplate objects
- Create the ImageTemplatesRequest object using the images list and IsOCR flag
- Convert all the images to a PDF using the PutImageInStorageToPdf() method
- Download the PDF with an image on each page
These steps summarize how to convert Image to PDF with C# RESTful Service. Commence the process by uploading the single or multiple images to the Cloud storage, creating the ImageTemplate class objects for all the images, and creating a list of these objects. Subsequently, create an ImageTemplatesRequest request using the above ImageTemplate list and call the PutImageInStorageToPdf() method to convert the image to a PDF.
Code for JPG to PDF Document Converter with C# .NET-based API
using System; | |
using System.IO; | |
using Aspose.Pdf.Cloud.Sdk.Api; | |
using Aspose.Pdf.Cloud.Sdk.Model; | |
using System.Collections.Generic; | |
namespace Aspose.PDF.Cloud.Examples.Kb | |
{ | |
public class PdfTasks | |
{ | |
public static void ConvertJpgToPdf() | |
{ | |
// Create the PdfApi class object | |
PdfApi pdfApi = new PdfApi("App Key", "App Sid"); | |
try | |
{ | |
// Upload the images | |
pdfApi.UploadFile("input1.jpg", new MemoryStream(File.ReadAllBytes("input1.jpg"))); | |
pdfApi.UploadFile("input2.jpg", new MemoryStream(File.ReadAllBytes("input2.jpg"))); | |
// Create the ImageTemplate class objects | |
ImageTemplate imageTemplate1 = new ImageTemplate(ImagePath: "input1.jpg", ImageSrcType: ImageSrcType.Common); | |
ImageTemplate imageTemplate2 = new ImageTemplate(ImagePath: "input2.jpg", ImageSrcType: ImageSrcType.Common); | |
// Create the list of images | |
List<ImageTemplate> images = new List<ImageTemplate>() { imageTemplate1, imageTemplate2 }; | |
ImageTemplatesRequest request = new ImageTemplatesRequest(IsOCR:false, ImagesList:images); | |
// Convert images to PDF | |
var apiResponse = pdfApi.PutImageInStorageToPdf("output.pdf", request); | |
if (apiResponse != null && apiResponse.Status.Equals("OK")) | |
{ | |
// Download the output pdf file | |
Stream storageRes = pdfApi.DownloadFile("output.pdf"); | |
FileStream fileStream = new FileStream("output.pdf", FileMode.Create, FileAccess.Write); | |
storageRes.CopyTo(fileStream); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
} | |
} | |
} | |
} |
This code demonstrates the process of developing a picture to PDF converter with C# Low Code API. To convert a single image to a PDF, you may follow the same process except upload the particular image to the cloud storage and create the required lists with a single item only. If you set the IsOCR flag to true, you may set the OCRLangs property to “string”.
This article has taught us the process of developing a picture to PDF file converter with C# RESTful Service. If you want to insert the image on an existing page along with other contents, follow the article on Insert Image into PDF with C# REST API.