Image to PDF converter on C#

Bigyan Chapagain
2 min readSep 4, 2022

--

Image to PDF converter code using C#

Once in a while, everyone needs to convert huge chunks of image files into pdf. And doing it online is either a very cumbersome and time-consuming process or after combining images to some of the pdf files, they will say, your free trial is over!

So here is a little piece of code in .NET core that converts your multiple png files in multiple folders into multiple pdf files that have the same name as the source folder that contains the images.

Since I am comfortable with web-app development, I wrote it like a web app; when I send a GET request to my http:localhost/PDFy/pdfy using a postman or fiddler, my job will be done on the folder level. In your case, you can write just like I did, or you can do it as a console app or even a windows MAUI app. The choice is always yours.

So here is the code:

using Microsoft.AspNetCore.Mvc;

using System;

using System.IO;

using System.Threading.Tasks;

using ImageMagick;

/***

* FILE PROCESSOR:

* combine png images to pdf.

* Author: Bigyan Chapagain

* Date: September 4, 2022

* ***/

namespace Thesis.Controllers

{

[Route(“[controller]”)]

public class PDFyController : ControllerBase

{

public PDFyController(){}

[HttpGet, Route(“pdfy”)]

public async Task<IActionResult> PDFy()

{

try

{

string sourceLocation = “E:\\Slides”;

string destLocation = “E:\\AutoPDFy”;

string[] directories = Directory.GetDirectories(sourceLocation);

foreach (var dir in directories)

{

string[] dir_splits = dir.Split(‘\\’);

//The directory name that contains images will become the PDF filename.

var pdfFileName = dir_splits[dir_splits.Length — 1];

var destFullPathAndFileName = Path.Combine(destLocation + “\\” + pdfFileName+”.pdf”);

string[] files = Directory.GetFiles(dir);

//convert memory stream to file stream

MemoryStream memStream = CreatePDFFromImages(files);

Stream streamToWriteTo = System.IO.File.Open(destFullPathAndFileName, FileMode.Create);

await memStream.CopyToAsync(streamToWriteTo);

}

return Ok(new { Success = true, Message = “Images converted to PDF file Successfully.” });

}

catch (Exception ex)

{

return Ok(new { Success = false, Message = “Something went wrong.” });

}

}

private MemoryStream CreatePDFFromImages(string[] files)

{

MemoryStream memStream = new MemoryStream();

using (MagickImageCollection images = new MagickImageCollection())

{

foreach(var file in files)

{

if (file.ToLower().Contains(“.png”))

{

MagickImage img = new MagickImage(file);

img.Format = MagickFormat.Pdf;

images.Add(img);

}

}

images.Write(memStream); // Write all image to MemoryStream

memStream.Position = 0;

return memStream;

}

}

}

}

You can use this same code to convert jpg to pdf also after making a few tweaks. You can give that a shot!

The additional package that’s not available by default is only Image Magick. You can add ImageMagick from Nuget Package Manager to your code.

Enjoy.

--

--