PNG to PDF – Convert PNG to PDF in C#

Convert PNG to PDF

The Portable Network Graphics (PNG) is quite popular due to lossless compression which means that you do not lose detail and quality after image compression. Therefore, it is suitable for different types of digital images including photographs and graphics. Owing to the fact that it can handle detailed and high-contrast images, it is widely used. However, we may have a requirement to share a large set of PNG images and their conversion to a PDF format can be one of the viable solutions. In this article, we are going to discuss the steps on how we can convert a PNG image to PDF format using .NET REST API.

Image processing REST API

Aspose.Imaging Cloud is our dedicated REST API developed to facilitate our customers looking for image processing and manipulations capabilities in the Cloud. No additional software download or installation is required. So all the demanding operations related to image resizing, cropping, rotating, scaling, flipping, searching, export to other Supported File Formats can be done in the cloud.

In order to further facilitate our users, we have created specific programming SDKs. Therefore, in order to implement the image processing capabilities in the .NET application, please try using Aspose.Imaging Cloud SDK for .NET. It is a wrapper around Aspose.Imaging Cloud and enables you to implement diverse imaging capabilities in your cloud-based solution.

Installation

The .NET Cloud SDK is available for download over NuGet and GitHub. Please execute the following command in the terminal to install the SDK via NuGet:

nuget install Aspose.Imaging-Cloud

In case you need to install from NuGet Package Manager, please execute the following command

PM> Install-Package Aspose.Imaging-Cloud

Visual Studio approach

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.Imaging-Cloud“.
  5. Click the checkbox besides Aspose.Imaging-Cloud package, select the appropriate version in the right-tab and click Add Package button.
Aspose.Imaging-Cloud
Image 1:- Aspose.Imaging-Cloud as NuGet package.

Free account subscription

In order to use our APIs, you need to first create a free subscription account. No credit card or other payment details are required. All you need to provide is your name, email ID, and required password. Furthermore, please note that under this free account, you can perform up to 150 free document processing requests and once you are satisfied with our services, you may opt for a license subscription for as low as $0.005$ / API call. For further details, please visit the Pricing page.

Without any further ado, let’s 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.

PNG to PDF in C#

In order to convert a PNG image to PDF format, we may use either of the following two APIs to accomplish this requirement:

The GET API expects you to first upload an image to Cloud Storage and then perform the conversion. Whereas the second API requires you to directly pass the image in the request body and the final output is stored on the Cloud Storage.

Loading Image from Cloud storage

The following steps define how we can first upload an image to Cloud storage and then will perform the conversion. If we skip the image upload routine, then we can also convert any existing image in Cloud storage to PDF format with the same code snippet.

  • First we need to create ImagingApi object while passing Client ID and Client Secret detials as arguments
  • Secondly, load PNG image from local drive and pass as argument to UploadFileRequest object
  • Thirdly, call the UploadFile(…) method of ImagingApi to upload the file to Cloud storage (you may skip this step if image is already avaiable in Cloud storage)
  • Then create an instance of ConvertImageRequest class which takes image name and resultant output format as arguments. Folder and Storage names are optional.
  • Now call the ConvertImage(…) method of ImagingApi class to initiate the conversion operation
  • Finally, as the response contains a streamed image, so we can save it to local drive by using File.Create object (method is shown below).

Loading image from local drive

In following section, we are going to use CreateConvertedImage API to convert an image directly passed in the request body.

  • The first step is to create ImagingApi object which takes Client ID and Client Secret detials as arguments
  • Seconldy, read the PNG image from local drive using File.OpenRead object
  • Thirdly, create CreateConvertedImageRequest object which takes ImageStream and output format as arguments
  • Then call the CreateConvertedImage(…) method of ImagingApi to perform the conversion operation
  • Finally, save the resultant PDF using File.Create object
PNG to PDF preview
Image 3:- PNG to PDF conversion preview.

You may consider downloading the input poodle.png and converted resultant.pdf for testing purposes.

Convert PNG to PDF using cURL command

The cURL commands are also an exciting feature to access the REST APIs via command line terminal. Since our APIs are based on REST architecture, so they can also be accessed via cURL commands. Also, as mentioned above, the APIs are only accessible to authorized persons, therefore, we need to first generate JWT access token based on Client ID and Client Secret details.

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 access token has been generated, please execute the following command to convert a PNG image available in Cloud storage to PDF format. The output PDF is then stored on local drive.

curl -X GET "https://api.aspose.cloud/v3.0/imaging/poodle.png/convert?format=pdf" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-o /Users/nshahbaz/Desktop/myResultant.pdf

Conclusion

In this article, we have explored the capabilities of Aspose.Imaging Cloud related to the conversion of PNG image to PDF format. We have also observed that conversion can be performed using .NET Cloud SDK as well as the cURL command. We have also provided the Swagger UI so you can test the REST API directly within the browser.

Also please be noted that the Cloud SDK is distributed under an MIT license and its complete source code is available for download over GitHub. In case you encounter any issue or have any query while using the API, please feel free to contact through the Free product support forum.

Related articles

We also recommend visiting the following links to learn more about