JPG to PDF – Convert JPG to PDF in C#

Convert JPG to PDF

The JPG or JPEG images are very popular because as use a complex lossy compression algorithm enabling the users to create smaller graphics. The majority of devices including Desktop, mobile, and other handheld devices support JPG images. However, if we need to share a large set of images, either we compress them as a single archive, upload them to a file sharing service or, convert them to PDF format and share a single Portable Document Format (PDF) file. In this article, we are going to discuss the conversion of JPG files to PDF format using C# .NET.

Image processing API

Aspose.Imaging Cloud is one of our popular APIs offering the capabilities to manipulate images on the cloud. Some of the exciting features include image resizing, cropping, rotation, and conversion to other supported file formats. It is developed as per REST API architecture, therefore it is platform-independent and can easily integrate with other cloud services. Also, in order to further facilitate our customers, we have created programming language-specific SDKs which take care of the low-level details of making requests and handling responses and enable you to focus on writing code specific to your particular project.

Aspose.Imaging Cloud SDK for .NET is a programming SDK developed as a wrapper around Aspose.Imaging Cloud. This SDK is capable of loading JPG images and render them in PDF format within the .NET applications.

PDF processing API

Similarly, Aspose.PDF Cloud is one of our leading REST API enabling the users to create, digitally sign, split, merge, & convert PDF files to HTML, XLS, XLSX, PPTX, DOC, XML, etc. It also enables you to transform EPUB, PS, SVG, XPS, JPEG, and other popular formats to PDF.

Albeit, both the APIs provide the capability to transform JPG raster images to PDF format, but we are going to focus on the conversion using Aspose.PDF Cloud SDK for .NET.

Installation

The first step in getting started with SDK usage is installation and it’s available for download at NuGet and| GitHub. Please execute the following command in the terminal to install the SDK via NuGet:

nuget install Aspose.Pdf-Cloud

Or execute the following command in NuGet Package Manager:

PM> Install-Package Aspose.Pdf-Cloud

Another approach is the installation directly within Visual Studio:

  1. Open the Solution Explorer.
  2. Expand the project and Right-click Packages folder within your solution.
  3. Select Manage NuGet Packages… option
  4. Click on the Browse tab and search for “Aspose.Pdf-Cloud“.
  5. Click on the Aspose.Pdf-Cloud package, select the appropriate version in the right-tab and click Add Package button.
Aspose.PDF Cloud NuGet package
Image 1:- Aspose.PDF Cloud as NuGet package.

Cloud subscription

At Aspose, privacy and data integrity are of utmost importance and in order to use our APIs, you need to first create a free subscription account by visiting Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information. Now 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 2:- Client Credentials at Aspose.Cloud dashboard.

JPG to PDF in C#

Please follow the instructions below to convert the JPG file to PDF format using C# .NET code.

  • First of all, 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 it to Cloud storage using the UploadFile(..) method of PdfApi
  • Next, create an object of ImageTemplatesRequest class where we define if OCR is enabled on image file, the language for OCR, margin details for image in PDF and page margin details.
  • Finally, call the PutImageInStorageToPdf(..) method of PdfApi class which take ouput PDF name and ImageTemplatesRequest object as arguments.
JPG to PDF conversion preview
Image 3:- JPG to PDF conversion preview.

Convert multiple images to PDF

Image 4:- Convert multiple images to PDF.

In case we have a requirement to convert a bulk of images to PDF format, all we need to do is crate more instances of ImageTemplate class within ImageTemplatesRequest object. Please see the code snippet below which shows how easily we can convert three images available in Cloud storage to single PDF document.

Similarly, you can specify different margin details for each image being placed inside the PDF document.

ImageTemplatesRequest imageTemplatesRequest = new ImageTemplatesRequest(IsOCR: true,
                OCRLangs: "eng",
                ImagesList: new List<ImageTemplate>()
                {
                    new ImageTemplate(ImagePath: $"jpg_to_pdf.jpg", ImageSrcType: ImageSrcType.Common, LeftMargin : 10, RightMargin : 10),
                    new ImageTemplate(ImagePath: $"word-to-jpg.png", ImageSrcType: ImageSrcType.Common, LeftMargin : 0, RightMargin : 0),
                    new ImageTemplate(ImagePath: $"word-to-pdf.jpg", ImageSrcType: ImageSrcType.Common, LeftMargin : 5, RightMargin : 5)
                });
3 images to PDF
Image 5:- Preview of 3 images converted to PDF.

Convert JPG to PDF using the cURL command

As Aspose.PDF Cloud is developed as per REST architecture, so it can also be accessed via cURL commands. However, before accessing the APIs, we need 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 the JWT token has been generated, please execute the following command to convert JPG image to PDF format and save the output in Cloud storage.

curl -X PUT "https://api.aspose.cloud/v3.0/pdf/converted.pdf/create/images" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{  \"IsOCR\": true,  \"ImagesList\": [    {      \"ImagePath\": \"word-to-jpg.jpeg\",      \"ImageSrcType\": \"Common\",      \"LeftMargin\": 0,      \"RightMargin\": 0,      \"TopMargin\": 0,      \"BottomMargin\": 0,      \"PageWidth\": 800,      \"PageHeight\": 800,      \"MarginInfo\": {        \"Left\": 10,        \"Right\": 10,        \"Top\": 10,        \"Bottom\": 10      }    }  ]}"

Conclusion

In this article, we have discussed the details pertinent to JPG raster images conversion to PDF format using Aspose.PDF Cloud. The API is so robust that with same code snippet and cURL commands, you may also convert other raster images such as PNG, BMP, GIF etc to PDF. Furthermore, please note that Aspose.PDF Cloud SDK for .NET is distributed under an MIT license and its complete source code is available for download over GitHub.

Should you have any related query, please feel free to contact via our Free product support forums.

Related Articles

You may consider visiting following links to learn more about