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 so we can combine PNG to PDF. In this article, we are going to discuss the steps on how we can convert a PNG to PDF format using .NET REST API.
- Image Conversion API
- Convert PNG to PDF
- PNG to PDF Online using cURL Commands
Image Conversion API
Aspose.Imaging Cloud is our dedicated REST API developed to facilitate our customers looking for image processing and manipulation capabilities in the Cloud. You can perform operations such as image resizing, cropping, rotating, scaling, flipping, searching, export to other Supported File Formats. Now as per this article, we are going to use Aspose.Imaging Cloud SDK for .NET which is a wrapper around Aspose.Imaging Cloud.
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
Install in Visual Studio
Another approach is the installation directly within Visual Studio:
- Open the Solution Explorer.
- Expand the project and Right-click the Packages folder within your solution.
- Select Manage NuGet Packages… option
- Click on the Browse tab and search for “Aspose.Imaging-Cloud“.
- Click the checkbox besides Aspose.Imaging-Cloud package, select the appropriate version in the right tab and click Add Package button.
The next step is to create 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.
Convert PNG to PDF
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.
Load PNG 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 an ImagingApi object while passing Client ID and Client Secret details 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 the image is already available in Cloud storage)
- Then create an instance of ConvertImageRequest class which takes the 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 a local drive by using File.Create an object (the method is shown below).
Load PNG from Local Drive
In the 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 an ImagingApi object which takes Client ID and Client Secret details as arguments
- Secondly, read the PNG image from the local drive using File.OpenRead object
- Thirdly, create ConvertedImageRequest 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 Online using cURL Commands
Our APIs can also be accessed via cURL commands, so we are going to convert PNG to PDF using cURL Commands. So first we need to generate a 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 a 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
In this article, we have explored the capabilities of Aspose.Imaging Cloud to convert PNG 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 issues or have any queries while using the API, please feel free to contact us through the Free product support forum.
We also recommend visiting the following links to learn more about