Skip to content

Instantly share code, notes, and snippets.

@aspose-cloud
Last active September 3, 2021 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-cloud/37a8d9e90a46570c63f61bc8fbb6c71a to your computer and use it in GitHub Desktop.
Save aspose-cloud/37a8d9e90a46570c63f61bc8fbb6c71a to your computer and use it in GitHub Desktop.
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