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 Ease
Get 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.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy 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");Deploy to test on your live environment
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 leverages 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.
:path=/static-assets/pdf/content-code-examples/how-to/add-images-to-pdfs-embed-image.csusing 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");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.
:path=/static-assets/pdf/content-code-examples/how-to/add-images-to-pdfs-base64-image.csusing 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");Ready to see what else you can do? Check out our tutorial page here: Additional Features
Frequently Asked Questions
How do I add images to PDF documents in C#?
With IronPDF, you can add images to PDFs by including them in HTML using the tag and then converting the HTML to PDF using the RenderHtmlAsPdf method. This approach leverages IronPDF's Chrome rendering engine to accurately render images within your PDF documents.
What image formats can I embed in PDFs?
IronPDF supports all major image formats including PNG, JPG, SVG, GIF, and BMP when embedding through HTML conversion. The library handles SVG graphics particularly well for scalable vector illustrations, ensuring your images render properly in the generated PDF.
Should I use direct URLs or Base64 encoding for images?
Use direct URLs when images are hosted online and file size is a concern. Choose Base64 encoding with IronPDF when creating self-contained PDFs that don't require internet access. Base64 encoding ensures your PDFs display images without external dependencies.
Can I add images to existing PDF files?
Yes, IronPDF allows you to add images to existing PDFs using either an image stamper or HTML stamper. This feature enables you to overlay images onto specific pages or positions within your existing PDF documents.
How do I convert an image to Base64 for PDF embedding?
Convert your image to a Base64 string and embed it directly in an HTML tag using a data URI. IronPDF's RenderHtmlAsPdf method will then process this Base64-encoded image and embed it into your PDF, creating a self-contained document.
Does image embedding preserve CSS styling and layouts?
Yes, IronPDF's Chrome rendering engine preserves CSS layouts and styling when converting HTML with images to PDF. The library supports responsive CSS layouts, ensuring images render exactly as they would appear in a web browser.







