Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
From a developer's perspective, programmatically adding images to PDF documents is a challenging task due to the variety of image formats and their complexity to manipulate. Therefore, the IronPDF C# library is recommended to add images to the PDF document programmatically. Let's explore what IronPDF is and how to use it effectively.
ReadAllBytes
C# methodimg
tagThe IronPDF C# library is a PDF library written in C# and targeting the PDF object model. This library provides the developer with a way to create, edit, and save PDF files without the need to tightly maintain relevance to specific APIs such as Adobe's Acrobat. The IronPDF C# library can be used when you don't want to use Adobe Acrobat or another individual piece of software.
This library assists developers with plenty of tools for creating, editing PDF files with C#, and saving PDF files and features that other .NET PDF libraries do not offer. Many developers prefer the IronPDF C# library because it provides everything they are looking for in a PDF library on Windows, Linux, and macOS platforms, and at no cost whatsoever! IronPDF constantly adds features and extends its services to make it the best utility for your PDF needs. The library excels beyond the bare-bones requirements for those who need to browse, search, find, extract data from PDF files or create PDF files. Let's take a look at how the IronPDF is used to add images to a PDF document.
To add images to a PDF document, the latest version of Visual Studio is recommended for creating a C# project for a smooth experience.
Visual Studio starting up UI
Create a Console Application in Visual Studio
By following the above steps, you will be able to create a new C# project easily. You can use an already existing C# project. Just open the project and install the IronPDF library.
The IronPDF library can be installed in multiple ways.
To install the library using NuGet Package Manager, follow the steps below:
Navigate to NuGet Package Manager
Install the IronPdf package from the NuGet Package Manager
Below are the steps to install the IronPDF library using the Console.
Install-Package IronPdf
It will start the installation, and you will be able to see the progress of the installation. After installation, you will be able to use the IronPDF library in your project very quickly.
The library has been installed, and now it's time to write code for adding images to the PDF document. Starting with importing the IronPDF namespace. So, write the following line in your code file:
using IronPdf;
using IronPdf;
Imports IronPdf
There are multiple ways to add images to PDF documents using IronPDF: use a direct image file, convert images to bytes, or use System.Drawing.Bitmap
. Furthermore, the IronPDF library supports multiple image formats.
Let's take a look:
using IronPdf;
using System.IO;
using System.Drawing;
/* Example 1: From an Image File or Bytes*/
static void Main(string [] args)
{
var renderer = new IronPdf.ChromePdfRenderer();
var pngBinaryData = File.ReadAllBytes("embed_me.png");
var ImgDataURI = @"data:image/png;base64," + Convert.ToBase64String(base64bytes);
var ImgHtml = $"<img src='{ImgDataURI}'>";
using var pdfdoc = renderer.RenderHtmlAsPdf(ImgHtml);
pdfdoc.SaveAs("embeded_example_1.pdf");
}
using IronPdf;
using System.IO;
using System.Drawing;
/* Example 1: From an Image File or Bytes*/
static void Main(string [] args)
{
var renderer = new IronPdf.ChromePdfRenderer();
var pngBinaryData = File.ReadAllBytes("embed_me.png");
var ImgDataURI = @"data:image/png;base64," + Convert.ToBase64String(base64bytes);
var ImgHtml = $"<img src='{ImgDataURI}'>";
using var pdfdoc = renderer.RenderHtmlAsPdf(ImgHtml);
pdfdoc.SaveAs("embeded_example_1.pdf");
}
Imports IronPdf
Imports System.IO
Imports System.Drawing
' Example 1: From an Image File or Bytes
Shared Sub Main(ByVal args() As String)
Dim renderer = New IronPdf.ChromePdfRenderer()
Dim pngBinaryData = File.ReadAllBytes("embed_me.png")
Dim ImgDataURI = "data:image/png;base64," & Convert.ToBase64String(base64bytes)
Dim ImgHtml = $"<img src='{ImgDataURI}'>"
Dim pdfdoc = renderer.RenderHtmlAsPdf(ImgHtml)
pdfdoc.SaveAs("embeded_example_1.pdf")
End Sub
This program will load the image first. The ReadAllBytes
function converts the image into bytes format in the above code. After that, the image data will be encoded into base64 and placed into an HTML tag as a string. After that, the HTML string will be rendered to PDF by using the RenderHtmlAsPdf
method function. It will create a PDF page in the PDF document.
The next example will show how to use the Bitmap image in the PDF document. IronPDF has a beneficial method to embed a System.Drawing.Image
to an HTML document which may then be rendered as a PDF. Visit the following ImageUtilities API to understand more about it. The following code will show how it works:
/* Example 2: From a System.Drawing.Bitmap */
//System.Drawing.BitMap MyImage
string DataURI = IronPdf.Util.ImageToDataUri(MyImage);
var ImgHtml = $"<img src='{DataURI}'>";
using var pdfdoc2 = renderer.RenderHtmlAsPdf(ImgHtml);
pdfdoc2.SaveAs("emeded_example_2.pdf");
/* Example 2: From a System.Drawing.Bitmap */
//System.Drawing.BitMap MyImage
string DataURI = IronPdf.Util.ImageToDataUri(MyImage);
var ImgHtml = $"<img src='{DataURI}'>";
using var pdfdoc2 = renderer.RenderHtmlAsPdf(ImgHtml);
pdfdoc2.SaveAs("emeded_example_2.pdf");
' Example 2: From a System.Drawing.Bitmap
'System.Drawing.BitMap MyImage
Dim DataURI As String = IronPdf.Util.ImageToDataUri(MyImage)
Dim ImgHtml = $"<img src='{DataURI}'>"
Dim pdfdoc2 = renderer.RenderHtmlAsPdf(ImgHtml)
pdfdoc2.SaveAs("emeded_example_2.pdf")
In the above code, the ImageToDataUri
function is used to convert it into URI and then the operator draws the image to a PDF document using the RenderHtmlAsPdf
function. This applies to multiple images.
Additionally, IronPDF is also capable of rendering charts in PDFs, adding barcodes to PDF documents, enhancing PDF security with passwords and watermarking PDF files, and even handling PDF forms programmatically.
IronPDF is an excellent PDF library that assists you in creating and customizing PDF files, and it is available to purchase today. However, IronPDF is entirely free for development purposes. You can also activate the free trial version for production without any payment details. After purchasing IronPDF, Iron Software makes you a fantastic offer to purchase five Iron Software packages for the price of just two. Yes! You heard right — you can purchase a suite of five Iron Software products for the price of just two. Buy it now! Visit the IronPDF licensing page for more details.
9 .NET API products for your office documents