How to Add Images in C#

This tutorial provides a comprehensive guide on how to add images to PDF documents in .NET applications using the Iron PDF library. It begins with installing the Iron PDF package via the NuGet Package Manager. The process involves importing the Iron PDF namespace and setting the license key. You'll learn to create a Chrome PDF renderer to convert HTML containing image tags into a PDF. The tutorial details embedding images using online URLs and base64 encoding.

For URL images, prepare an HTML string with an image tag pointing to an online source, render it to a PDF with the RenderHtmlAsPdf method, and save the file. For base64 images, convert the image's binary data into a base64 string, embed it in an HTML tag, render, and save the resulting PDF. Finally, the tutorial demonstrates running the application to verify the successful embedding of images. By following these steps, developers can easily add images to PDFs and explore more advanced features of Iron PDF for versatile document generation.

// Importing the necessary namespace from the Iron PDF library
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Set your IronPDF license key
        License.LicenseKey = "YOUR_LICENSE_KEY_HERE";

        // Creating a new instance of the HtmlToPdf class, which uses Chrome rendering
        var Renderer = new HtmlToPdf();

        // Example of embedding an image using an online URL
        string htmlWithImageUrl = @"
            <html>
                <body>
                    <h1>Image from URL</h1>
                    <img src='https://example.com/path/to/image.jpg' alt='Sample Image'/>
                </body>
            </html>";

        // Render the HTML to a PDF file and save it to the specified file path
        PdfDocument pdfFromUrlImage = Renderer.RenderHtmlAsPdf(htmlWithImageUrl);
        pdfFromUrlImage.SaveAs("UrlImageExample.pdf");

        // Example of embedding an image using base64 encoding
        string base64Image = "iVBORw0KGgoAAAANSUhEUgAAAAUA" +
                             "AAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
                             "//8/w38GIAXDIBKE0DHxgljNBAAO" +
                             "9TXL0Y4OHwAAAABJRU5ErkJggg=="; // Sample base64 string

        string htmlWithBase64Image = $@"
            <html>
                <body>
                    <h1>Image from Base64</h1>
                    <img src='data:image/png;base64,{base64Image}' alt='Sample Base64 Image'/>
                </body>
            </html>";

        // Render the HTML to a PDF file and save it to the specified file path
        PdfDocument pdfFromBase64Image = Renderer.RenderHtmlAsPdf(htmlWithBase64Image);
        pdfFromBase64Image.SaveAs("Base64ImageExample.pdf");

        // The PDFs should now contain the images as specified
    }
}
// Importing the necessary namespace from the Iron PDF library
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Set your IronPDF license key
        License.LicenseKey = "YOUR_LICENSE_KEY_HERE";

        // Creating a new instance of the HtmlToPdf class, which uses Chrome rendering
        var Renderer = new HtmlToPdf();

        // Example of embedding an image using an online URL
        string htmlWithImageUrl = @"
            <html>
                <body>
                    <h1>Image from URL</h1>
                    <img src='https://example.com/path/to/image.jpg' alt='Sample Image'/>
                </body>
            </html>";

        // Render the HTML to a PDF file and save it to the specified file path
        PdfDocument pdfFromUrlImage = Renderer.RenderHtmlAsPdf(htmlWithImageUrl);
        pdfFromUrlImage.SaveAs("UrlImageExample.pdf");

        // Example of embedding an image using base64 encoding
        string base64Image = "iVBORw0KGgoAAAANSUhEUgAAAAUA" +
                             "AAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
                             "//8/w38GIAXDIBKE0DHxgljNBAAO" +
                             "9TXL0Y4OHwAAAABJRU5ErkJggg=="; // Sample base64 string

        string htmlWithBase64Image = $@"
            <html>
                <body>
                    <h1>Image from Base64</h1>
                    <img src='data:image/png;base64,{base64Image}' alt='Sample Base64 Image'/>
                </body>
            </html>";

        // Render the HTML to a PDF file and save it to the specified file path
        PdfDocument pdfFromBase64Image = Renderer.RenderHtmlAsPdf(htmlWithBase64Image);
        pdfFromBase64Image.SaveAs("Base64ImageExample.pdf");

        // The PDFs should now contain the images as specified
    }
}
' Importing the necessary namespace from the Iron PDF library
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Set your IronPDF license key
		License.LicenseKey = "YOUR_LICENSE_KEY_HERE"

		' Creating a new instance of the HtmlToPdf class, which uses Chrome rendering
		Dim Renderer = New HtmlToPdf()

		' Example of embedding an image using an online URL
		Dim htmlWithImageUrl As String = "
            <html>
                <body>
                    <h1>Image from URL</h1>
                    <img src='https://example.com/path/to/image.jpg' alt='Sample Image'/>
                </body>
            </html>"

		' Render the HTML to a PDF file and save it to the specified file path
		Dim pdfFromUrlImage As PdfDocument = Renderer.RenderHtmlAsPdf(htmlWithImageUrl)
		pdfFromUrlImage.SaveAs("UrlImageExample.pdf")

		' Example of embedding an image using base64 encoding
		Dim base64Image As String = "iVBORw0KGgoAAAANSUhEUgAAAAUA" & "AAAFCAYAAACNbyblAAAAHElEQVQI12P4" & "//8/w38GIAXDIBKE0DHxgljNBAAO" & "9TXL0Y4OHwAAAABJRU5ErkJggg==" ' Sample base64 string

		Dim htmlWithBase64Image As String = $"
            <html>
                <body>
                    <h1>Image from Base64</h1>
                    <img src='data:image/png;base64,{base64Image}' alt='Sample Base64 Image'/>
                </body>
            </html>"

		' Render the HTML to a PDF file and save it to the specified file path
		Dim pdfFromBase64Image As PdfDocument = Renderer.RenderHtmlAsPdf(htmlWithBase64Image)
		pdfFromBase64Image.SaveAs("Base64ImageExample.pdf")

		' The PDFs should now contain the images as specified
	End Sub
End Class
$vbLabelText   $csharpLabel

Further Reading: How to Add Images to PDFs

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.
< PREVIOUS
How to Add and Remove Attachments in PDF C#
NEXT >
How to Add, Copy, and Delete Pages in PDFs Using C#