Skip to main content

C# utility function to convert an image to a PDF file using itextsharp.

public static void ConvertImageToPdf(string srcFilename, string dstFilename)
{
    iTextSharp.text.Rectangle pageSize = null;

    using (var srcImage = new Bitmap(srcFilename))
    {
        pageSize = new iTextSharp.text.Rectangle(0, 0, srcImage.Width, srcImage.Height);
    }
    using (var ms = new MemoryStream())
    {
        var document = new iTextSharp.text.Document(pageSize, 0, 0, 0, 0);
        iTextSharp.text.pdf.PdfWriter.GetInstance(document, ms).SetFullCompression();
        document.Open();
        var image = iTextSharp.text.Image.GetInstance(srcFilename);
        document.Add(image);
        document.Close();

        File.WriteAllBytes(dstFilename, ms.ToArray());
    }
}