Convert JPG, PNG, GIF, and TIFF Images to PDF in C#

We convert an Image to PDF because it gives assurance that the image will display correctly across devices without being altered. PDF images are ideal for printing and for storing images online when we intend them to be downloaded. PDFs keep the images in one document so viewers can print and save them easily.

We will use the GroupDocs.Conversion for .NET library to convert raster images to PDF. The library lets us convert the following image formats to PDF:

Convert JPG and PNG to PDF
  • JPG
  • TIFF
  • TIF
  • JPEG
  • PNG
  • GIF
  • BMP
  • ICO
  • CMX
  • DIB
  • JPC

Please check the documentation for the complete list of supported formats.

Convert Image to PDF in C#

First, we need to install GroupDocs.Conversion’s NuGet package. The Development Environment Installation and Configuration article explains in detail the steps to install the NuGet package in Visual Studio.

Conversion to PDF format could be triggered by following below steps:

  • Create a new instance of the Converter class and pass the source document path as a constructor parameter.
  • Instantiate PdfConvertOptions class.
  • Call Convert method of Converter class instance and pass filename for the converted document and the instance of PdfConvertOptions from the previous step.
using (Converter converter = new Converter("C:\\Sample.jpg"))
    PdfConvertOptions options = new PdfConvertOptions();
    converter.Convert("C:\\Converted.pdf", options);

We tried to convert the following image to PDF and got this output file.

JPEG Image

Convert to PDF in C# with advanced options

GroupDocs.Conversion provides PdfConvertOptions to give us control over conversion results when converting Image to PDF. Some of the additional options are:

  • Width – desired image width after conversion
  • Height – desired image height after conversion
  • MarginTop – desired page top margin after conversion
  • MarginBottom – desired page bottom margin after conversion
  • MarginLeft – desired page left margin after conversion
  • MarginRight – desired page right margin after conversion
  • Rotate – page rotation. Available options are: None, On90, On180, On270

The following code sample is using these additional options to convert an image to PDF.

using (Converter converter = new Converter("C:\\Data\\Sample.jpg"))
    PdfConvertOptions options = new PdfConvertOptions
        Width = 233,
        Height = 175,
        MarginTop = 20,
        MarginBottom = 20,
        MarginLeft = 20,
        MarginRight = 20,
        Rotate = Rotation.On180
    converter.Convert("C:\\Data\\AdvancedConversion.pdf", options);

and generates the following output file.

Output Document After Conversion

More resources

Please check the documentation to know about the features that GroupDocs.Conversion for .NET API offers and GitHub Examples to see these features in action.