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.
How to Add Images to PDFs
- Download the IronPDF C# library
- Prepare the image file to be embedded
- Use the
img
tag to embed images in HTML - Render the HTML to PDF using the
RenderHtmlAsPdf
method - Embed the image using Base64 encoding
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;
// Create an instance of ChromePdfRenderer, which utilizes a Chrome-based rendering engine.
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Define the HTML content that needs to be converted to PDF.
// Here, an image from the specified URL is embedded within the HTML.
string html = @"<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>";
// Convert the HTML to a PDF document.
// RenderHtmlAsPdf method takes an HTML string and creates a PdfDocument object.
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
// Save the resulting PDF document to the specified file path.
// This method writes the PDF to a file named "embedImage.pdf" in the working directory.
pdf.SaveAs("embedImage.pdf");
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;
// Create a ChromePdfRenderer instance to render HTML to PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
try
{
// Import image file binary data
byte[] binaryData = File.ReadAllBytes("ironpdf-logo-text-dotnet.svg");
// Convert the binary data to a base64 encoded string
string imgDataUri = Convert.ToBase64String(binaryData);
// Embed the base64 encoded image in an HTML img tag
string html = $"<img src='data:image/svg+xml;base64,{imgDataUri}'>";
// Use the renderer to convert the HTML to a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
// Save the generated PDF document to the specified path
pdf.SaveAs("embedImageBase64.pdf");
Console.WriteLine("PDF has been successfully created and saved.");
}
catch (FileNotFoundException ex)
{
// Handle specific case when the image file could not be found
Console.WriteLine("The specified image file was not found: " + ex.Message);
}
catch (Exception ex)
{
// Handle any other exceptions that may occur during execution
Console.WriteLine("An error occurred: " + ex.Message);
}
Frequently Asked Questions
What is the advantage of embedding images in a PDF?
Embedding an image in a PDF ensures that the PDF file is self-contained and does not rely on external sources. This allows the PDF to display the image seamlessly, even without an internet connection or external files.
How can IronPDF be used to add images to PDFs?
IronPDF can render HTML strings, files, and web URLs to PDF. By embedding images in HTML using the tag, and then converting the HTML to a PDF document, images can be added to PDFs using IronPDF.
What steps are involved in adding images to PDFs using IronPDF?
To add images to PDFs using IronPDF, you need to download the IronPDF C# library, prepare the image file, use the tag to embed images in HTML, render the HTML to PDF using the RenderHtmlAsPdf method, and optionally, embed the image using Base64 encoding.
How do you embed an image in a PDF using the
tag?
To embed an image in a PDF using the tag, include the image in HTML with the
tag, then use IronPDF's RenderHtmlAsPdf method to convert the HTML to a PDF.
What is the process for embedding an image using Base64 encoding?
To embed an image using Base64 encoding, you need to read the binary data of the image, convert it to a Base64 string using the Convert.ToBase64String method, create an HTML image tag with the Base64 data, and then render the HTML to a PDF.
Can existing PDFs have images added using IronPDF?
Yes, you can stamp images onto existing PDF documents using either an image stamper or an HTML stamper tutorial provided by IronPDF.
Is an internet connection required to display embedded images in PDFs?
No, once an image is embedded in a PDF, it does not require an internet connection to be displayed since the PDF is self-contained.
What coding languages are used in the examples provided?
The examples provided use C# programming language to demonstrate how to embed images in PDFs using IronPDF.
What is the purpose of using Base64 encoding for images in HTML?
Base64 encoding is used to embed image data directly in HTML, allowing images to be displayed without separate image files. This is particularly useful when converting HTML to PDF with embedded images.
Where can I find more information on image format types for Base64 embedding?
More information on image format types for Base64 embedding can be found on the MDN Web Docs on Image Formats.