I have to convert jpg and png format images into pdf runtime and then upload using c# by IFormFile?
Please suggest, Should I go with ITextSharp or any other option?
I have to convert jpg and png format images into pdf runtime and then upload using c# by IFormFile?
Please suggest, Should I go with ITextSharp or any other option?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Aug 7, 2021, 9:22 AM EDT
Viorel S81 reputationPosted Aug 8, 2021, 9:10 PM EDT
You can use Spire.PDF (available on NuGet) to do the file format conversion.
Here is the code snippet for your reference:
//Create a pdf document with a section and page added. PdfDocument doc = new PdfDocument(); PdfSection section = doc.Sections.Add(); PdfPageBase page = doc.Pages.Add(); //Load a png image file PdfImage image = PdfImage.FromFile(@"D:\images\img.png"); //Set image location and size in PDF float widthFitRate = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width; float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height; float fitRate = Math.Max(widthFitRate, heightFitRate); float fitWidth = image.PhysicalDimension.Width / fitRate; float fitHeight = image.PhysicalDimension.Height / fitRate; //Draw image on page page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight); //Save the file doc.SaveToFile("image to pdf.pdf"); doc.Close();Yogesh KhurpeRank #7,540 · 239 reputationPosted Aug 9, 2021, 12:15 PM EDT
Marvin ReidRank #1,963 · 746 reputationPosted Mar 9, 2023, 9:53 AM EST
You can convert both your jpg and png format images to PDF files using the DocumentConverter. Here is some sample code that shows how you can handle this:
using (DocumentConverter documentConverter = new DocumentConverter()) { var input = @"C:\Input\input.jpg"); // supports both jpg and png var output = @"C:\Output\output.pdf"); var jobData = DocumentConverterJobs.CreateJobData(input, output, DocumentFormat.Pdf); jobData.JobName = "save to pdf"; var documentWriter = new DocumentWriter(); documentConverter.SetDocumentWriterInstance(documentWriter); var renderingEngine = new AnnWinFormsRenderingEngine(); documentConverter.SetAnnRenderingEngineInstance(renderingEngine); var job = documentConverter.Jobs.CreateJob(jobData); documentConverter.Jobs.RunJob(job); }https://www.leadtools.com/help/sdk/tutorials/dotnet-convert-files-with-the-document-converter.html