How to Add Images to PDFs

Embedding an image in a PDF means placing the image directly within the PDF file, ensuring it's self-contained and doesn't rely on external sources. This allows the PDF to display the image seamlessly, even without an internet connection or external files.

IronPDF is capable of rendering HTML strings, files, and web URLs to PDF. By using this method, images can be embedded in HTML and then converted into a PDF document.


Embed Image in PDF Example

To embed an image in a PDF, you must first include the image in HTML using the <img> tag. Then, use the RenderHtmlAsPdf method to convert the HTML to PDF. If you have an existing PDF, you can stamp the image onto the PDF document using either an image stamper or HTML stamper tutorial.

:path=/static-assets/pdf/content-code-examples/how-to/add-images-to-pdfs-embed-image.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

string html = @"<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>";

// Render HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

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

Private renderer As New ChromePdfRenderer()

Private html As String = "<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>"

' Render HTML to PDF
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

' Export PDF
pdf.SaveAs("embedImage.pdf")
$vbLabelText   $csharpLabel

Embed with Base64 Example

To use base64 for embedding an image in HTML, you must first obtain the binary data of the image either by reading the image file or receiving it through a network request. Use the Convert.ToBase64String method in Microsoft .NET to convert the binary data to base64. Construct the image tag in HTML using "data:image/svg+xml;base64," before the base64 data. You may have noticed that the image type is being specified before the base64 data. Visit the MDN Web Docs on Image Formats for more information on image format types.

:path=/static-assets/pdf/content-code-examples/how-to/add-images-to-pdfs-base64-image.cs
using IronPdf;
using System;
using System.IO;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Import image file binary data
byte[] binaryData = File.ReadAllBytes("ironpdf-logo-text-dotnet.svg");

// Convert the binary data to base 64
string imgDataUri = Convert.ToBase64String(binaryData);

// Embed in HTML
string html = $"<img src='data:image/svg+xml;base64,{imgDataUri}'>";

// Convert HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

// Export the PDF
pdf.SaveAs("embedImageBase64.pdf");
Imports IronPdf
Imports System
Imports System.IO

Private renderer As New ChromePdfRenderer()

' Import image file binary data
Private binaryData() As Byte = File.ReadAllBytes("ironpdf-logo-text-dotnet.svg")

' Convert the binary data to base 64
Private imgDataUri As String = Convert.ToBase64String(binaryData)

' Embed in HTML
Private html As String = $"<img src='data:image/svg+xml;base64,{imgDataUri}'>"

' Convert HTML to PDF
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

' Export the PDF
pdf.SaveAs("embedImageBase64.pdf")
$vbLabelText   $csharpLabel

Ready to see what else you can do? Check out our tutorial page here: Addtional Features

Frequently Asked Questions

How can I add images to PDFs using C#?

You can add images to PDFs in C# by using IronPDF. First, download the IronPDF C# library. Then, prepare the image file and embed it in HTML using the <img> tag. Finally, use the RenderHtmlAsPdf method to convert the HTML to a PDF.

What is the advantage of using Base64 encoding for images in PDFs?

Base64 encoding allows you to embed image data directly in HTML. This means images can be displayed in PDFs without requiring separate image files or network requests, ensuring that the PDF is self-contained.

Can I add images to an existing PDF document?

Yes, you can add images to an existing PDF by stamping them onto the document. IronPDF provides tutorials for using either an image stamper or an HTML stamper to achieve this.

What is the process for converting image binary data to Base64?

To convert image binary data to Base64, use Microsoft's .NET Convert.ToBase64String method. Once converted, you can embed the Base64 string in an HTML <img> tag and render it to PDF using IronPDF.

Do embedded images in PDFs require an internet connection to display?

No, once images are embedded in PDFs, they do not require an internet connection to display. This is because the images are contained within the PDF file itself.

How can I embed an image in a PDF using HTML and IronPDF?

To embed an image in a PDF using HTML, include the image in an HTML file using the <img> tag. Then, employ IronPDF's RenderHtmlAsPdf method to convert that HTML into a PDF document.

Where can I learn more about image formats for use in Base64 embedding?

For more information on image format types for Base64 embedding, you can refer to the MDN Web Docs on Image Formats. This resource provides detailed information on different image types and their usage.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Reviewed by
Jeff Fritz
Jeffrey T. Fritz
Principal Program Manager - .NET Community Team
Jeff is also a Principal Program Manager for the .NET and Visual Studio teams. He is the executive producer of the .NET Conf virtual conference series and hosts 'Fritz and Friends' a live stream for developers that airs twice weekly where he talks tech and writes code together with viewers. Jeff writes workshops, presentations, and plans content for the largest Microsoft developer events including Microsoft Build, Microsoft Ignite, .NET Conf, and the Microsoft MVP Summit