Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this comprehensive tutorial, we explore the process of converting images to PDFs using Iron PDF in a C# console application. Beginning with the setup, we define paths for single and multiple images. The tutorial showcases four different methods, each demonstrating unique features of Iron PDF.
The first method, 'convert image to PDF with header,' adds a custom HTML header with a blue title and divider line to a PDF. This is ideal for reports or presentations requiring contextual information. The second method, 'converting image to PDF centered,' positions an image in the center of a PDF, creating visually appealing documents suitable for portfolios or showcasing photos.
In the third method, 'converting multiple images to PDF,' we demonstrate combining several images into one document. This is perfect for creating photo albums or compiling multiple diagrams into a cohesive document. The final method, 'SIMPLE image to PDF,' provides a straightforward conversion without additional formatting, suitable for quick PDF conversions.
After executing the code, the tutorial reviews the output PDFs, highlighting the effectiveness of each method. The tutorial concludes by inviting viewers to subscribe for more tutorials and try Iron PDF by signing up for a trial. This tutorial is perfect for developers looking to enhance their document generation capabilities using Iron PDF.
Below is the sample implementation code for these methods:
// This example script demonstrates image to PDF conversions using IronPDF library in C#
using IronPdf;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// Path to the image file
string imagePath = "path/to/your/image.jpg";
// Paths to multiple images
List<string> imagePaths = new List<string>
{
"path/to/first/image.jpg",
"path/to/second/image.jpg",
// Add more image paths as needed
};
// Method 1: Convert image to PDF with a custom header
ConvertImageToPdfWithHeader(imagePath);
// Method 2: Convert image to centered PDF
ConvertImageToPdfCentered(imagePath);
// Method 3: Convert multiple images into one PDF
ConvertMultipleImagesToPdf(imagePaths);
// Method 4: Simple image to PDF conversion
SimpleImageToPdf(imagePath);
}
// This method adds a custom header to the PDF before converting the image
static void ConvertImageToPdfWithHeader(string imagePath)
{
var pdfDocument = new HtmlToPdf().RenderHtmlAsPdf("<h1 style='color: blue;'>Custom Header</h1><hr/>");
var image = new PdfDocument(imagePath);
pdfDocument.AppendPdf(image);
pdfDocument.SaveAs("image_with_header.pdf");
}
// This method centers the image in the PDF
static void ConvertImageToPdfCentered(string imagePath)
{
var pdfDocument = new PdfDocument();
var image = new PdfDocument(imagePath);
// Centering image in PDF
pdfDocument.Merge(image);
pdfDocument.SaveAs("centered_image.pdf");
}
// This method combines multiple images into a single PDF document
static void ConvertMultipleImagesToPdf(List<string> imagePaths)
{
var pdfDocument = new PdfDocument();
foreach (var path in imagePaths)
{
var image = new PdfDocument(path);
pdfDocument.Merge(image);
}
pdfDocument.SaveAs("multiple_images.pdf");
}
// This method performs a simple conversion of an image to PDF without extra formatting
static void SimpleImageToPdf(string imagePath)
{
var pdfDocument = new PdfDocument(imagePath);
pdfDocument.SaveAs("simple_image.pdf");
}
}
// This example script demonstrates image to PDF conversions using IronPDF library in C#
using IronPdf;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
// Path to the image file
string imagePath = "path/to/your/image.jpg";
// Paths to multiple images
List<string> imagePaths = new List<string>
{
"path/to/first/image.jpg",
"path/to/second/image.jpg",
// Add more image paths as needed
};
// Method 1: Convert image to PDF with a custom header
ConvertImageToPdfWithHeader(imagePath);
// Method 2: Convert image to centered PDF
ConvertImageToPdfCentered(imagePath);
// Method 3: Convert multiple images into one PDF
ConvertMultipleImagesToPdf(imagePaths);
// Method 4: Simple image to PDF conversion
SimpleImageToPdf(imagePath);
}
// This method adds a custom header to the PDF before converting the image
static void ConvertImageToPdfWithHeader(string imagePath)
{
var pdfDocument = new HtmlToPdf().RenderHtmlAsPdf("<h1 style='color: blue;'>Custom Header</h1><hr/>");
var image = new PdfDocument(imagePath);
pdfDocument.AppendPdf(image);
pdfDocument.SaveAs("image_with_header.pdf");
}
// This method centers the image in the PDF
static void ConvertImageToPdfCentered(string imagePath)
{
var pdfDocument = new PdfDocument();
var image = new PdfDocument(imagePath);
// Centering image in PDF
pdfDocument.Merge(image);
pdfDocument.SaveAs("centered_image.pdf");
}
// This method combines multiple images into a single PDF document
static void ConvertMultipleImagesToPdf(List<string> imagePaths)
{
var pdfDocument = new PdfDocument();
foreach (var path in imagePaths)
{
var image = new PdfDocument(path);
pdfDocument.Merge(image);
}
pdfDocument.SaveAs("multiple_images.pdf");
}
// This method performs a simple conversion of an image to PDF without extra formatting
static void SimpleImageToPdf(string imagePath)
{
var pdfDocument = new PdfDocument(imagePath);
pdfDocument.SaveAs("simple_image.pdf");
}
}
' This example script demonstrates image to PDF conversions using IronPDF library in C#
Imports IronPdf
Imports System.Collections.Generic
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Path to the image file
Dim imagePath As String = "path/to/your/image.jpg"
' Paths to multiple images
Dim imagePaths As New List(Of String) From {"path/to/first/image.jpg", "path/to/second/image.jpg"}
' Method 1: Convert image to PDF with a custom header
ConvertImageToPdfWithHeader(imagePath)
' Method 2: Convert image to centered PDF
ConvertImageToPdfCentered(imagePath)
' Method 3: Convert multiple images into one PDF
ConvertMultipleImagesToPdf(imagePaths)
' Method 4: Simple image to PDF conversion
SimpleImageToPdf(imagePath)
End Sub
' This method adds a custom header to the PDF before converting the image
Private Shared Sub ConvertImageToPdfWithHeader(ByVal imagePath As String)
Dim pdfDocument = (New HtmlToPdf()).RenderHtmlAsPdf("<h1 style='color: blue;'>Custom Header</h1><hr/>")
Dim image = New PdfDocument(imagePath)
pdfDocument.AppendPdf(image)
pdfDocument.SaveAs("image_with_header.pdf")
End Sub
' This method centers the image in the PDF
Private Shared Sub ConvertImageToPdfCentered(ByVal imagePath As String)
Dim pdfDocument As New PdfDocument()
Dim image = New PdfDocument(imagePath)
' Centering image in PDF
pdfDocument.Merge(image)
pdfDocument.SaveAs("centered_image.pdf")
End Sub
' This method combines multiple images into a single PDF document
Private Shared Sub ConvertMultipleImagesToPdf(ByVal imagePaths As List(Of String))
Dim pdfDocument As New PdfDocument()
For Each path In imagePaths
Dim image = New PdfDocument(path)
pdfDocument.Merge(image)
Next path
pdfDocument.SaveAs("multiple_images.pdf")
End Sub
' This method performs a simple conversion of an image to PDF without extra formatting
Private Shared Sub SimpleImageToPdf(ByVal imagePath As String)
Dim pdfDocument As New PdfDocument(imagePath)
pdfDocument.SaveAs("simple_image.pdf")
End Sub
End Class
Further Reading: How to Convert Images to a PDF