How to Convert Images to a PDF

by Chaknith Bin

Converting images to PDF is a useful process that combines multiple image files (such as JPG, PNG, or TIFF) into a single PDF document. This is often done to create digital portfolios, presentations, or reports, making it easier to share and store a collection of images in a more organized and universally readable format.

IronPDF allows you to convert a single or multiple images into a PDF with unique image placements and behaviors. These behaviors include fitting to the page, centering on the page, and cropping the page. Additionally, you can add text and HTML headers and footers using IronPDF, apply watermarks with IronPDF, set custom page sizes, and include background and foreground overlays.

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Convert Image to PDF Example

Use the ImageToPdf static method within the ImageToPdfConverter class to convert an image to a PDF document. This method requires only the file path to the image, and it will convert it to a PDF document with default image placement and behavior. Supported image formats include .bmp, .jpeg, .jpg, .gif, .png, .svg, .tif, .tiff, .webp, .apng, .avif, .cur, .dib, .ico, .jfif, .jif, .jpe, .pjp, and .pjpeg.

Sample Image

Image Sample

Code

:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-one-image.cs
using IronPdf;

string imagePath = "meetOurTeam.jpg";

// Convert an image to a PDF
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath);

// Export the PDF
pdf.SaveAs("imageToPdf.pdf");
Imports IronPdf

Private imagePath As String = "meetOurTeam.jpg"

' Convert an image to a PDF
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath)

' Export the PDF
pdf.SaveAs("imageToPdf.pdf")
VB   C#

Output PDF


Convert Images to PDF Example

To convert multiple images into a PDF document, you should provide an IEnumerable object that contains file paths instead of a single file path, as shown in our previous example. This will once again generate a PDF document with default image placement and behavior.

:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-multiple-images.cs
using IronPdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

// Retrieve all JPG and JPEG image paths in the 'images' folder.
IEnumerable<String> imagePaths = Directory.EnumerateFiles("images").Where(f => f.EndsWith(".jpg") || f.EndsWith(".jpeg"));

// Convert images to a PDF
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePaths);

// Export the PDF
pdf.SaveAs("imagesToPdf.pdf");
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq

' Retrieve all JPG and JPEG image paths in the 'images' folder.
Private imagePaths As IEnumerable(Of String) = Directory.EnumerateFiles("images").Where(Function(f) f.EndsWith(".jpg") OrElse f.EndsWith(".jpeg"))

' Convert images to a PDF
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePaths)

' Export the PDF
pdf.SaveAs("imagesToPdf.pdf")
VB   C#

Output PDF


Image Placements and Behaviors

For ease of use, we offer a range of helpful image placement and behavior options. For instance, you can center the image on the page or fit it to the page size while maintaining its aspect ratio. All available image placements and behaviors are as follows:

  • TopLeftCornerOfPage: Image is placed at the top-left corner of the page.
  • TopRightCornerOfPage: Image is placed at the top-right corner of the page.
  • CenteredOnPage: Image is centered on the page.
  • FitToPageAndMaintainAspectRatio: Image fits the page while keeping its original aspect ratio.
  • BottomLeftCornerOfPage: Image is placed at the bottom-left corner of the page.
  • BottomRightCornerOfPage: Image is placed at the bottom-right corner of the page.
  • FitToPage: Image fits the page.
  • CropPage: Page is adjusted to fit the image.
:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-one-image-image-behavior.cs
using IronPdf;
using IronPdf.Imaging;

string imagePath = "meetOurTeam.jpg";

// Convert an image to a PDF with image behavior of centered on page
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath, ImageBehavior.CenteredOnPage);

// Export the PDF
pdf.SaveAs("imageToPdf.pdf");
Imports IronPdf
Imports IronPdf.Imaging

Private imagePath As String = "meetOurTeam.jpg"

' Convert an image to a PDF with image behavior of centered on page
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath, ImageBehavior.CenteredOnPage)

' Export the PDF
pdf.SaveAs("imageToPdf.pdf")
VB   C#

Image Behaviors Comparison

Place the image at the top-left of the page
Place the image at the top-right of the page
Place the image at the center of the page
Fit the image to the page while maintaining the aspect ratio
Place the image at the bottom-left of the page
Place the image at the bottom-right of the page
Stretch the image to fit the page
Crop the page to fit the image

Apply Rendering Options

The key to converting various types of images into a PDF document under the hood of the ImageToPdf static method is to import the image as an HTML <img> tag and then convert the HTML to PDF. This is also the reason we can pass the ChromePdfRenderOptions object as a third parameter of the ImageToPdf method to customize the rendering process directly.

:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-one-image-rendering-options.cs
using IronPdf;

string imagePath = "meetOurTeam.jpg";

ChromePdfRenderOptions options = new ChromePdfRenderOptions()
{
    HtmlHeader = new HtmlHeaderFooter()
    {
        HtmlFragment = "<h1 style='color: #2a95d5;'>Content Header</h1>",
        DrawDividerLine = true,
    },
};

// Convert an image to a PDF with custom header
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath, options: options);

// Export the PDF
pdf.SaveAs("imageToPdfWithHeader.pdf");
Imports IronPdf

Private imagePath As String = "meetOurTeam.jpg"

Private options As New ChromePdfRenderOptions() With {
	.HtmlHeader = New HtmlHeaderFooter() With {
		.HtmlFragment = "<h1 style='color: #2a95d5;'>Content Header</h1>",
		.DrawDividerLine = True
	}
}

' Convert an image to a PDF with custom header
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath, options:= options)

' Export the PDF
pdf.SaveAs("imageToPdfWithHeader.pdf")
VB   C#

Output PDF

If you'd like to convert or rasterize a PDF document into images, please refer to our guide on how to rasterize PDFs to images.

Chaknith related to Output PDF

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.