JPG to PDF Convertor in C#

Coppermill picture Coppermill · Oct 29, 2009 · Viewed 65.8k times · Source

I would like to convert from an image (like jpg or png) to PDF.

I've checked out ImageMagickNET, but it is far too complex for my needs.

What other .NET solutions or code are there for converting an image to a PDF?

Answer

Darin Dimitrov picture Darin Dimitrov · Oct 29, 2009

Easy with iTextSharp:

class Program
{
    static void Main(string[] args)
    {
        Document document = new Document();
        using (var stream = new FileStream("test.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
        {
            PdfWriter.GetInstance(document, stream);
            document.Open();
            using (var imageStream = new FileStream("test.jpg", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                var image = Image.GetInstance(imageStream);
                document.Add(image);
            }
            document.Close();
        }
    }
}