Search code examples
c#pdfjpegimage-conversion

JPG to PDF Convertor in C#


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?


Solution

  • iTextSharp does it pretty cleanly and is open source. Also, it has a very good accompanying book by the author which I recommend if you end up doing more interesting things like managing forms. For normal usage, there are plenty resources on mailing lists and newsgroups for samples of how to do common things.

    EDIT: as alluded to in @Chirag's comment, @Darin's answer has code that definitely compiles with current versions.

    Example usage:

    public static void ImagesToPdf(string[] imagepaths, string pdfpath)
    {
        using(var doc = new iTextSharp.text.Document())
        {
            iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(pdfpath, FileMode.Create));
            doc.Open();
            foreach (var item in imagepaths)
            {
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(item);
                doc.Add(image);
            }
        }
    }