How to Add Images in PDF using C#

Introduction

We are all familiar with the existence of PDF, both with the old historical file size and its popularity today. PDFs can be used in many ways, from displaying static images to programmatic documents such as interactive reports, database queries, etc.

It has numerous uses — one of the prominent examples is the various organizations who use it to distribute their magazines and newsletters online. More firms have started generating PDFs for other systems through various services, making it even more popular. Having multiple uses can also boost productivity for other available software applications.

From the developer's point of view, adding images to PDF documents programmatically is a very tough task because images have many formats and are not easy to manipulate programmatically. So, here is where we come up with a solution. We will use the IronPDF C# library to add images programmatically to our PDF document. Let's take a look at what IronPDF is, and how we can use it to add photos to PDF documents

IronPDF: C# PDF Library

The 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, 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 both Windows platforms and Linux configurations, 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 or create PDF files. Let's take a look at how we can use IronPDF to add images to a PDF document.

Create or Open C# Project

To add images to a PDF document, we have to create a C# project. Follow the steps below to develop the C# Project. I am using the Visual Studio 2019 version. The latest version is recommended for a smooth experience.

  • Open Visual Studio 2019.
  • Click on the "Create a New Project" button.
  • Select "C# Console application" from the Project templates and click on the "Next" button. You can choose a platform according to your needs.
  • Next, give a name to your project and click on the "Next" button.
  • Choose Target framework >= .NET core 3.1 version and click on the "Create" button.

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. We will see how to install the IronPDF library in our project in the next section below.

Install the IronPDF Library

Our project is ready to install the IronPDF library. We can install the IronPDF library in multiple ways.

  • Using NuGet Package Manager
  • Using Package Manager Console

Using NuGet Package Manager

To install the library using NuGet Package Manager, follow the steps below:

  • Go to the Tools > NuGet Package Manager > Manage NuGet Package for Solution from the main menu options.
  • This will open the NuGet Package Manager windows. Go to the browse tab and search for IronPDF. Select the IronPDF library and click on the "Install" button.

It will start installing the library, and after installation, we will be able to use it in our project.

Using Package Manager Console

Below are the steps to install the IronPDF library using the Console.

  • Go to the Package Manager Console (usually located at the bottom of Visual Studio).
  • Write the following command to start the installation of the IronPDF library.
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. We have to import the IronPDF namespace before using it in our project. So, write the following line in your code file:

using IronPDF;
using IronPDF;
Imports IronPDF
VB   C#

Adding Bitmaps and Images to the PDF Document

In this section, we will write the code to add images to a PDF document using IronPDF. We can create a new document with images and add images to an existing PDF file. We can add images in two ways in PDF. We can use a direct image file or converted images to bytes. Furthermore, we can use multiple image formats. The second way to add an image is using System.Drawing.Bitmap. The following code shows the addition of images using reading bytes of Image. We will convert the image to PDF. 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
VB   C#

This program will load the image first. The ReadAllBytes function converts the image into bytes format in the above code. After that, we set the base URI of the image. We make an HTML string where we store the binary data of our image. After that, we will render that HTML string to PDF by using the RenderHtmlAsPdf function. It will create a PDF page in the Adobe PDF document.

We will look at how we can use the Bitmap image in the PDF document in the following code. 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 link to understand more about it. The following code will show how we can use it in our code:

/* 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")
VB   C#

In the above code, we use the ImateToDataUri function to convert it into URI and then the operator draws the image to a PDF document using the RenderHtmlAsPdf function. We can use multiple images. We will see an image page in an output PDF document. Visit this link for a more detailed tutorial.

Licensing

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 30-day trial version free 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 this link for more details.

You can download the software product from this link.