Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code snippets for converting PNG to PDF using C#
Code snippets for converting JPG to PNG using Aspose.Imaging Cloud SDK For .NET
// Get ClientID from https://dashboard.aspose.cloud/
string clientSecret = "caac6e3d4a4724b2feb53f4e460eade3";
string clientID = "4ccf1790-accc-41e9-8d18-a78dbb2ed1aa";
// path of input PNG file
string imageFile = "poodle.png";
// output file format
string format = "pdf";
// name of resultant PDF document
string resultantFile = "resultant.pdf";
try
{
using (var imageStream = System.IO.File.OpenRead("/Users/nshahbaz/Desktop/" + imageFile))
{
var request = new CreateConvertedImageRequest(imageStream, format, null, null);
Stream updatedImage = imagingApi.CreateConvertedImage(request);
if (request != null && request.Equals("OK"))
{
Console.WriteLine("PNG successfully converted to PDF !");
}
// Save updated image to local storage
using (var fileStream = File.Create("/Users/nshahbaz/Desktop/" + resultantFile))
{
updatedImage.Seek(0, SeekOrigin.Begin);
updatedImage.CopyTo(fileStream);
}
}
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
// Get ClientID from https://dashboard.aspose.cloud/
string clientSecret = "caac6e3d4a4724b2feb53f4e460eade3";
string clientID = "4ccf1790-accc-41e9-8d18-a78dbb2ed1aa";
// path of input PNG file
string imageFile = "poodle.png";
// output file format
string format = "pdf";
// name of resultant PDF document
string resultantFile = "resultant.pdf";
// load the file from local drive
using (var file = System.IO.File.OpenRead("/Users/nshahbaz/Desktop/" + imageFile))
{
var uploadFileRequest = new UploadFileRequest(imageFile, file);
// Upload original document to Cloud Storage
imagingApi.UploadFile(uploadFileRequest);
}
try
{
// Create ImageRequest
var request = new ConvertImageRequest(imageFile, format, null, null);
// initiate the conversion operation
Stream updatedImage = imagingApi.ConvertImage(request);
// print success message if conversion is successful
if (request != null && request.Equals("OK"))
{
Console.WriteLine("The PNG successfully converted to PDF !");
}
// call the method to save output over system drive
saveToDisk(updatedImage, "/Users/nshahbaz/Desktop/"+resultantFile);
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
// custom method to save steam object as file instance
public static void saveToDisk(Stream responseStream, String resultantFile)
{
var fileStream = File.Create(resultantFile);
responseStream.Seek(0, SeekOrigin.Begin);
responseStream.CopyTo(fileStream);
fileStream.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment