푸터 콘텐츠로 바로가기
IRONPDF 사용

C# Add Image to PDF (Developer Tutorial)

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.

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 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.

Create or Open C# Project

To add images to a PDF document, the latest version of Visual Studio is recommended for creating a C# project for a smooth experience.

  • Open Visual Studio.

How to Add Images in PDF using C#, Figure 1: Visual Studio starting up UI Visual Studio starting up UI

  • 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.

How to Add Images in PDF using C#, Figure 2: Create a Console Application in Visual Studio Create a Console Application in Visual Studio

  • Next, give a name to your project and click on the Next button.
  • Choose Target .NET 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.

Install the IronPDF Library

The IronPDF library can be installed 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 Tools > NuGet Package Manager > Manage NuGet Package for Solution from the main menu options.

How to Add Images in PDF using C#, Figure 3: Navigate to NuGet Package Manager Navigate to NuGet Package Manager

  • This will open the NuGet Package Manager window. Go to the browse tab and search for IronPDF. Select the IronPDF library and click on the "Install" button.

How to Add Images in PDF using C#, Figure 4: Install the IronPdf package from the NuGet Package Manager Install the IronPdf package from the NuGet Package Manager

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.

How to Add Images in PDF using C#, Figure 5:


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

Adding Bitmaps and Images to the PDF Document

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;

class PDFImageAdder
{
    /* This method demonstrates how to convert an image file to a PDF document in C# */
    static void Main(string[] args)
    {
        // Initialize IronPdf Renderer
        var renderer = new IronPdf.ChromePdfRenderer();

        // Read the PNG image file into binary format
        var pngBinaryData = File.ReadAllBytes("embed_me.png");

        // Convert image binary data to base64 for embedding in HTML
        var ImgDataURI = "data:image/png;base64," + Convert.ToBase64String(pngBinaryData);

        // Embed the image as a base64 data URI in an HTML <img> tag
        var ImgHtml = $"<img src='{ImgDataURI}'>";

        // Render the HTML as a PDF document
        using var pdfdoc = renderer.RenderHtmlAsPdf(ImgHtml);

        // Save the rendered PDF document
        pdfdoc.SaveAs("embedded_example_1.pdf");
    }
}
using IronPdf;
using System.IO;
using System.Drawing;

class PDFImageAdder
{
    /* This method demonstrates how to convert an image file to a PDF document in C# */
    static void Main(string[] args)
    {
        // Initialize IronPdf Renderer
        var renderer = new IronPdf.ChromePdfRenderer();

        // Read the PNG image file into binary format
        var pngBinaryData = File.ReadAllBytes("embed_me.png");

        // Convert image binary data to base64 for embedding in HTML
        var ImgDataURI = "data:image/png;base64," + Convert.ToBase64String(pngBinaryData);

        // Embed the image as a base64 data URI in an HTML <img> tag
        var ImgHtml = $"<img src='{ImgDataURI}'>";

        // Render the HTML as a PDF document
        using var pdfdoc = renderer.RenderHtmlAsPdf(ImgHtml);

        // Save the rendered PDF document
        pdfdoc.SaveAs("embedded_example_1.pdf");
    }
}
$vbLabelText   $csharpLabel

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 <img> tag as a string. After that, the HTML string will be rendered to a 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 a Bitmap image in the PDF document. IronPDF has a useful method to embed a System.Drawing.Image in 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:

using IronPdf;
using System.Drawing;

class PDFImageAdder
{
    static void Main(string[] args)
    {
        // Initialize IronPdf Renderer
        var renderer = new IronPdf.ChromePdfRenderer();

        // Create a Bitmap image
        Bitmap MyImage = new Bitmap("Path-to-Your-Image");

        // Convert the Bitmap image to a Data URI
        string DataURI = IronPdf.Util.ImageToDataUri(MyImage);

        // Embed the image as a Data URI in an HTML <img> tag
        var ImgHtml = $"<img src='{DataURI}'>";

        // Render the HTML to PDF
        using var pdfdoc2 = renderer.RenderHtmlAsPdf(ImgHtml);

        // Save the PDF document
        pdfdoc2.SaveAs("embedded_example_2.pdf");
    }
}
using IronPdf;
using System.Drawing;

class PDFImageAdder
{
    static void Main(string[] args)
    {
        // Initialize IronPdf Renderer
        var renderer = new IronPdf.ChromePdfRenderer();

        // Create a Bitmap image
        Bitmap MyImage = new Bitmap("Path-to-Your-Image");

        // Convert the Bitmap image to a Data URI
        string DataURI = IronPdf.Util.ImageToDataUri(MyImage);

        // Embed the image as a Data URI in an HTML <img> tag
        var ImgHtml = $"<img src='{DataURI}'>";

        // Render the HTML to PDF
        using var pdfdoc2 = renderer.RenderHtmlAsPdf(ImgHtml);

        // Save the PDF document
        pdfdoc2.SaveAs("embedded_example_2.pdf");
    }
}
$vbLabelText   $csharpLabel

In the above code, the ImageToDataUri function is used to convert an image into a URI format. The image is then drawn in the 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.

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 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.

자주 묻는 질문

C#을 사용하여 PDF에 이미지를 추가하려면 어떻게 해야 하나요?

C#에서 이미지를 base64 형식으로 변환하고 HTML  related to C#을 사용하여 PDF에 이미지를 추가하려면 어떻게 해야 하나요? 태그에 임베드한 다음 IronPDF의 RenderHtmlAsPdf 메서드를 사용하여 HTML을 PDF로 변환하여 이미지를 PDF에 추가할 수 있습니다.

PDF에 이미지를 추가하기 위한 C# 프로젝트를 설정하는 단계는 무엇인가요?

PDF에 이미지를 추가하기 위한 C# 프로젝트를 설정하려면 Visual Studio를 열고 새 C# 콘솔 애플리케이션을 만든 다음 프로젝트가 .NET Framework 버전 3.1 이상을 대상으로 하는지 확인합니다. NuGet 패키지 관리자 또는 패키지 관리자 콘솔을 통해 IronPDF 라이브러리를 설치합니다.

PDF 임베딩을 위해 C#에서 이미지를 base64로 변환하려면 어떻게 해야 하나요?

C#에서는 이미지 파일을 바이트 배열로 읽은 다음 Convert.ToBase64String를 사용하여 이미지를 base64로 변환할 수 있습니다. 이 base64 문자열을 HTML  related to PDF 임베딩을 위해 C#에서 이미지를 base64로 변환하려면 어떻게 해야 하나요? 태그에 임베드하여 IronPDF를 사용하여 PDF로 변환할 수 있습니다.

PDF에 임베드할 수 있는 이미지 형식은 무엇인가요?

IronPDF는 여러 이미지 형식을 지원하므로 PDF 문서에 JPEG, PNG, BMP 등 다양한 유형의 이미지를 삽입할 수 있습니다.

C#을 사용하여 PDF에 비트맵 이미지를 삽입하는 방법은 무엇인가요?

C#을 사용하여 PDF에 비트맵 이미지를 삽입하려면 ImageToDataUri 함수를 사용하여 비트맵을 데이터 URI로 변환하고 HTML  related to C#을 사용하여 PDF에 비트맵 이미지를 삽입하는 방법은 무엇인가요? 태그에 삽입한 다음 IronPDF의 RenderHtmlAsPdf 메서드를 사용하여 PDF로 렌더링합니다.

이미지를 삽입하면서 PDF 보안을 강화하려면 어떻게 해야 하나요?

IronPDF는 비밀번호 및 권한 추가와 같은 PDF 보안을 강화하는 기능을 제공합니다. 이는 PDF 생성 프로세스에서 보안 옵션을 설정하여 이미지 삽입과 함께 수행할 수 있습니다.

PDF에 이미지와 함께 차트와 바코드를 추가할 수 있나요?

예, IronPDF를 사용하면 이미지 외에도 PDF에 차트와 바코드를 추가할 수 있습니다. 이러한 요소는 HTML과 CSS를 사용하여 생성 및 렌더링한 다음 IronPDF를 사용하여 PDF로 변환할 수 있습니다.

IronPDF의 라이선스 옵션은 무엇인가요?

IronPDF는 개발을 위한 무료 평가판과 광범위한 사용을 위한 비용 효율적인 라이선스 옵션을 제공합니다. 자세한 내용은 IronPDF 라이선스 페이지에서 확인할 수 있습니다.

패키지 관리자 콘솔을 사용하여 IronPDF를 설치하려면 어떻게 하나요?

패키지 관리자 콘솔을 사용하여 IronPDF를 설치하려면 Install-Package IronPdf 명령을 사용합니다.

IronPDF는 .NET 10과 호환되나요?

예 - IronPDF는 .NET 10은 물론 .NET 9, 8, 7, 6, .NET Core, .NET Standard 및 .NET Framework를 포함한 이전 버전과도 완벽하게 호환됩니다.

커티스 차우
기술 문서 작성자

커티스 차우는 칼턴 대학교에서 컴퓨터 과학 학사 학위를 취득했으며, Node.js, TypeScript, JavaScript, React를 전문으로 하는 프론트엔드 개발자입니다. 직관적이고 미적으로 뛰어난 사용자 인터페이스를 만드는 데 열정을 가진 그는 최신 프레임워크를 활용하고, 잘 구성되고 시각적으로 매력적인 매뉴얼을 제작하는 것을 즐깁니다.

커티스는 개발 분야 외에도 사물 인터넷(IoT)에 깊은 관심을 가지고 있으며, 하드웨어와 소프트웨어를 통합하는 혁신적인 방법을 연구합니다. 여가 시간에는 게임을 즐기거나 디스코드 봇을 만들면서 기술에 대한 애정과 창의성을 결합합니다.