el

How to convert image to pdf using Image Magic in C#

Saturday, October 17, 2020

This blog will discuss how to convert any type of image file (.jpg, .gif, .tif ..) to pdf. I found a free tool “ImageMagic.NET”. You can download the DLLs from http://imagemagick.codeplex.com/ Add “ImageMagic.NET” DLL to your project. Here I’m creating a function which will create PFD from images. using ImageMagick; private Stream CreatePDFFromImage() { MemoryStream memStream = new MemoryStream(); using (MagickImageCollection images = new MagickImageCollection()) { MagickImage first = new MagickImage("C:\\image path\\file1.TIF"); first.Format = MagickFormat.Pdf; images.Add(first);   MagickImage second = new MagickImage("C:\\image path\\file2.TIF"); second.Format = MagickFormat.Pdf; images.Add(second);   MagickImage third = new MagickImage("C:\\image path\\file3.TIF");                    third.Format = MagickFormat.Pdf; images.Add(third);   // you can add any type of images   images.Write(memStream); // Write all image to MemoryStream memStream.Position = 0;   return memStream; } } Now we can call this function which will return the PDF file. MemoryStream stream = new MemoryStream(); Stream = CreatePDFFromImage(); // calling the function byte[] bytes = stream.ToArray(); // Convert MemoryStream to byte Here we can use this byte to download the PDF or display in browser.

No items found.