How to Add Images to PDFs in C# with IronPDF
IronPDF enables embedding images directly into PDFs by converting HTML containing image tags to PDF documents, creating self-contained files that display images without external dependencies or internet connections.
Quickstart: Embed Images into PDFs with EaseGet started embedding images in PDF files using IronPDF in .NET C#. Convert your image to a Base64 string and embed it in an HTML <img> tag to generate a self-contained PDF that requires no external files. This method ensures your images display without internet access.
-
1Install IronPDF with NuGet Package Manager
-
2Copy and run this code snippet.
new IronPdf.ChromePdfRenderer() .RenderHtmlAsPdf("<img src='data:image/png;base64," + Convert.ToBase64String(File.ReadAllBytes("logo.png")) + "'>") .SaveAs("image-embedded.pdf");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
Minimal Workflow (5 steps)
- Download the IronPDF C# library
- Prepare the image file to be embedded
- Use the
imgtag to embed images in HTML - Render the HTML to PDF using the
RenderHtmlAsPdfmethod - Embed the image using Base64 encoding
How Do I Embed Images in PDFs?
To embed an image in a PDF, 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, stamp the image onto the PDF document using either an image stamper or HTML stamper tutorial. This approach works with IronPDF's Chrome rendering engine.
Why Does the RenderHtmlAsPdf Method Work Best?
The RenderHtmlAsPdf method levers IronPDF's Chrome rendering engine to convert HTML content, including images, into PDF format while preserving layout and styling. This method supports responsive CSS layouts and ensures images render as they appear in a web browser. For complex layouts, see our guide on pixel-perfect HTML formatting.
What Image Formats Are Supported?
IronPDF supports all major image formats including PNG, JPG, SVG, GIF, and BMP when embedding through HTML conversion. The library handles SVG graphics effectively for scalable vector illustrations. When working with different formats, ensure your HTML properly specifies the image type in the src attribute or Base64 data URI.
When Should I Use Direct URL vs Base64 Encoding?
Use direct URLs when images are hosted online and file size is a concern. Choose Base64 encoding for self-contained PDFs that don't require internet access. For images stored in cloud services, see our guide on embedding images from Azure Blob Storage. This decision impacts both file size and portability of your generated PDFs.
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
Dim renderer As New ChromePdfRenderer()
Dim html As String = "<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>"
' Render HTML to PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)
' Export PDF
pdf.SaveAs("embedImage.pdf")The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.
How Do I Embed Images Using Base64?
To use Base64 for embedding an image in HTML, first obtain the binary data of the image by reading the image file or receiving it through a network request. Use the Convert.ToBase64String method in .NET to convert the binary data to Base64. Construct the image tag in HTML using "data:image/svg+xml;base64," before the Base64 data. Note that the image type is specified before the Base64 data. Visit the MDN Web Docs on Image Formats for more information on image format types.
Why Use Base64 Encoding for Images?
Base64 encoding creates self-contained PDFs that don't rely on external resources, ensuring images always display without internet connectivity or when original image files are moved or deleted. This technique is useful when creating PDFs in Blazor Server applications or when deploying to Azure where external file access might be restricted. The encoded images become part of the HTML string, making them ideal for HTML string to PDF conversions.
What Are the Performance Considerations?
Base64 encoding increases file size by approximately 33% but eliminates external dependencies, making it ideal for archival purposes or restricted network environments. When performance is critical, consider using async PDF generation to handle multiple image conversions concurrently. For large-scale operations, see our performance optimization guide to maximize throughput.
How Do I Handle Large Image Files?
For large images, compress them before Base64 encoding to minimize PDF file size, or use external URLs if file size is critical. IronPDF offers PDF compression features that can reduce file sizes by up to 75%. When working with multiple images, optimize your rendering settings for the best balance between quality and file size.
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")Ready to see what else you can do? Check out our tutorial page here: Additional Features
Frequently Asked Questions
How can I embed images in a PDF using C# and IronPDF?
To embed images in a PDF using IronPDF, use HTML image tags with IronPDF's Chrome rendering engine. Convert the image to a Base64 string and embed it in an HTML `` tag. Then, use the `RenderHtmlAsPdf` method to render the HTML into a self-contained PDF.
What image formats does IronPDF support when embedding into PDFs?
IronPDF supports embedding various image formats in PDFs, including PNG, JPG, SVG, GIF, and BMP, by using HTML conversion techniques. Ensure your HTML specifies the correct image type in the `src` attribute or Base64 data URI.
Why is the `RenderHtmlAsPdf` method preferred for embedding images in PDFs?
The `RenderHtmlAsPdf` method is preferred because it leverages IronPDF's Chrome rendering engine to accurately convert HTML content, including styling and images, into PDF format. This method supports responsive layouts and ensures the correct rendering of images as seen in web browsers.
When should I use Base64 encoding over direct URLs for images in PDFs?
Base64 encoding should be used when creating self-contained PDFs that do not require internet access to display images. This is particularly useful for offline environments or where image files may not be persistently available. Direct URLs are suitable when images are hosted online, and file size is a concern.
What are the performance considerations of using Base64 encoded images?
Using Base64 encoded images increases the file size by approximately 33% but eliminates external image dependencies. It is ideal for creating robust and portable PDFs, especially in environments with restricted network access. For improved performance, consider async PDF generation.
How can IronPDF assist with embedding large images into PDFs?
For embedding large images, consider compressing the images before encoding them into Base64 to reduce the PDF size. IronPDF offers features for PDF compression to help maintain an optimal balance between quality and file size.
Is it possible to use IronPDF with Blazor Server applications for PDF creation?
Yes, IronPDF can be used with Blazor Server applications to create PDFs. By embedding images using Base64 or other techniques, you can ensure that the PDFs are self-contained and do not depend on external resources, suitable for server-side environments.
Can IronPDF handle SVG graphics when embedding images into PDFs?
Yes, IronPDF can effectively handle SVG graphics when embedding them into PDFs. By converting HTML containing SVGs using the `RenderHtmlAsPdf` method, you can preserve the scalability and quality of vector illustrations.
How does IronPDF optimize the rendering of PDFs with multiple images?
IronPDF optimizes rendering by using advanced settings and compression techniques. For multiple images, you can adjust rendering settings to achieve the best balance between image quality and PDF file size. Refer to the optimization guide for more comprehensive strategies.
What advantages does Base64 encoding provide when embedding images in PDF documents?
Base64 encoding provides the advantage of creating self-contained PDFs that ensure images display consistently regardless of external resource availability. This is especially beneficial in offline scenarios or when embedding sensitive images requiring secure handling.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.