Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
This tutorial provides a comprehensive guide on how to add images to PDF documents in .NET applications using the Iron PDF library. It begins with installing the Iron PDF package via the NuGet Package Manager. The process involves importing the Iron PDF namespace and setting the license key. You'll learn to create a Chrome PDF renderer to convert HTML containing image tags into a PDF. The tutorial details embedding images using online URLs and base64 encoding.
For URL images, prepare an HTML string with an image tag pointing to an online source, render it to a PDF with the RenderHtmlAsPdf
method, and save the file. For base64 images, convert the image's binary data into a base64 string, embed it in an HTML tag, render, and save the resulting PDF. Finally, the tutorial demonstrates running the application to verify the successful embedding of images. By following these steps, developers can easily add images to PDFs and explore more advanced features of Iron PDF for versatile document generation.
// Importing the necessary namespace from the Iron PDF library
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Set your IronPDF license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
// Creating a new instance of the HtmlToPdf class, which uses Chrome rendering
var Renderer = new HtmlToPdf();
// Example of embedding an image using an online URL
string htmlWithImageUrl = @"
<html>
<body>
<h1>Image from URL</h1>
<img src='https://example.com/path/to/image.jpg' alt='Sample Image'/>
</body>
</html>";
// Render the HTML to a PDF file and save it to the specified file path
PdfDocument pdfFromUrlImage = Renderer.RenderHtmlAsPdf(htmlWithImageUrl);
pdfFromUrlImage.SaveAs("UrlImageExample.pdf");
// Example of embedding an image using base64 encoding
string base64Image = "iVBORw0KGgoAAAANSUhEUgAAAAUA" +
"AAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
"//8/w38GIAXDIBKE0DHxgljNBAAO" +
"9TXL0Y4OHwAAAABJRU5ErkJggg=="; // Sample base64 string
string htmlWithBase64Image = $@"
<html>
<body>
<h1>Image from Base64</h1>
<img src='data:image/png;base64,{base64Image}' alt='Sample Base64 Image'/>
</body>
</html>";
// Render the HTML to a PDF file and save it to the specified file path
PdfDocument pdfFromBase64Image = Renderer.RenderHtmlAsPdf(htmlWithBase64Image);
pdfFromBase64Image.SaveAs("Base64ImageExample.pdf");
// The PDFs should now contain the images as specified
}
}
// Importing the necessary namespace from the Iron PDF library
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Set your IronPDF license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE";
// Creating a new instance of the HtmlToPdf class, which uses Chrome rendering
var Renderer = new HtmlToPdf();
// Example of embedding an image using an online URL
string htmlWithImageUrl = @"
<html>
<body>
<h1>Image from URL</h1>
<img src='https://example.com/path/to/image.jpg' alt='Sample Image'/>
</body>
</html>";
// Render the HTML to a PDF file and save it to the specified file path
PdfDocument pdfFromUrlImage = Renderer.RenderHtmlAsPdf(htmlWithImageUrl);
pdfFromUrlImage.SaveAs("UrlImageExample.pdf");
// Example of embedding an image using base64 encoding
string base64Image = "iVBORw0KGgoAAAANSUhEUgAAAAUA" +
"AAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
"//8/w38GIAXDIBKE0DHxgljNBAAO" +
"9TXL0Y4OHwAAAABJRU5ErkJggg=="; // Sample base64 string
string htmlWithBase64Image = $@"
<html>
<body>
<h1>Image from Base64</h1>
<img src='data:image/png;base64,{base64Image}' alt='Sample Base64 Image'/>
</body>
</html>";
// Render the HTML to a PDF file and save it to the specified file path
PdfDocument pdfFromBase64Image = Renderer.RenderHtmlAsPdf(htmlWithBase64Image);
pdfFromBase64Image.SaveAs("Base64ImageExample.pdf");
// The PDFs should now contain the images as specified
}
}
' Importing the necessary namespace from the Iron PDF library
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Set your IronPDF license key
License.LicenseKey = "YOUR_LICENSE_KEY_HERE"
' Creating a new instance of the HtmlToPdf class, which uses Chrome rendering
Dim Renderer = New HtmlToPdf()
' Example of embedding an image using an online URL
Dim htmlWithImageUrl As String = "
<html>
<body>
<h1>Image from URL</h1>
<img src='https://example.com/path/to/image.jpg' alt='Sample Image'/>
</body>
</html>"
' Render the HTML to a PDF file and save it to the specified file path
Dim pdfFromUrlImage As PdfDocument = Renderer.RenderHtmlAsPdf(htmlWithImageUrl)
pdfFromUrlImage.SaveAs("UrlImageExample.pdf")
' Example of embedding an image using base64 encoding
Dim base64Image As String = "iVBORw0KGgoAAAANSUhEUgAAAAUA" & "AAAFCAYAAACNbyblAAAAHElEQVQI12P4" & "//8/w38GIAXDIBKE0DHxgljNBAAO" & "9TXL0Y4OHwAAAABJRU5ErkJggg==" ' Sample base64 string
Dim htmlWithBase64Image As String = $"
<html>
<body>
<h1>Image from Base64</h1>
<img src='data:image/png;base64,{base64Image}' alt='Sample Base64 Image'/>
</body>
</html>"
' Render the HTML to a PDF file and save it to the specified file path
Dim pdfFromBase64Image As PdfDocument = Renderer.RenderHtmlAsPdf(htmlWithBase64Image)
pdfFromBase64Image.SaveAs("Base64ImageExample.pdf")
' The PDFs should now contain the images as specified
End Sub
End Class
Further Reading: How to Add Images to PDFs