Skip to content

Instantly share code, notes, and snippets.

@aspose-cloud
Last active August 26, 2021 21:07
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/f74e919a2e76136189458c8c3f40d7ed to your computer and use it in GitHub Desktop.
Save aspose-cloud/f74e919a2e76136189458c8c3f40d7ed to your computer and use it in GitHub Desktop.
Code snippets for converting JPG to PDF using C#
// get client credentials from https://dashboard.aspose.cloud/
string clientSecret = "caac6e3d4a4724b2feb53f4e460eade3";
string clientID = "4ccf1790-accc-41e9-8d18-a78dbb2ed1aa";
// Create an instance of PdfApi
PdfApi pdfApi = new PdfApi(clientSecret, clientID);
// name of intput JPG file
string imageFile = "word-to-jpg.jpeg";
// name of resultant PDF to be generated on Cloud storage
string resultantFile = "converted.pdf";
// read the image file from local storage
using (var file = System.IO.File.OpenRead("/Users/nshahbaz/Desktop/" + imageFile))
{
// Upload original document to Cloud Storage
pdfApi.UploadFile(imageFile,file);
}
// create an ImageTemplateRequest instance defining properties for output PDF
// We have set margin details for PDF and have enabled the OCR properties
ImageTemplatesRequest imageTemplatesRequest = new ImageTemplatesRequest(IsOCR: true,
OCRLangs: "eng",
ImagesList: new List<ImageTemplate>()
{
new ImageTemplate(ImagePath: $"word-to-jpg.jpeg", ImageSrcType: ImageSrcType.Common, LeftMargin : 10, RightMargin : 10)
});
try
{
// Invoke SDK to convert JPG to PDF and save output in Cloud storage
var apiResponse = pdfApi.PutImageInStorageToPdf(resultantFile, imageTemplatesRequest);
if (apiResponse != null && apiResponse.Status.Equals("OK"))
{
Console.WriteLine("JPG successfully converted to PDF !");
Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace);
}
Code snippets for converting JPG to PDF using Aspose.PDF Cloud SDK for .NET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment