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");
$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;

// 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);
}
$vbLabelText   $csharpLabel

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  related to How can IronPDF be used to add images to PDFs? 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  related to What steps are involved in adding images to PDFs using IronPDF? 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  related to What steps are involved in adding images to PDFs using IronPDF? tag?

To embed an image in a PDF using the  related to How do you embed an image in a PDF using the  tag? tag, include the image in HTML with the  related to How do you embed an image in a PDF using the  tag? 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.

Chaknith related to Embed with Base64 Example
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.