Incorporating Images into PDFs with C# and .NET

Integrating graphics into PDF documents can significantly enhance their visual appeal and convey information more effectively. Using C# and .NET, you can easily add various types of graphics to your PDFs, such as images, shapes, and vector graphics. This article will guide you through the process of integrating graphics into PDFs using Sautinsoft.PDF library.

Graphics can be used in PDFs for various purposes, including:

  • Enhancing visual appeal.
  • Illustrating complex information.
  • Adding company logos and branding.
  • Creating diagrams and charts.
  • Designing custom forms and templates.

The following example shows how to easily create a PDF document and incorporate an image on its first page:

  1. Add SautinSoft.PDF from NuGet.
  2. Create a new document and add a page.
  3. Load the image from a file.
  4. Set the location of the bottom-left corner of the image.
  5. Draw the image to the page.
  6. Save the document.

Output result:

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;
using SautinSoft.Pdf.Content;

class Program
{
    /// <summary>
    /// Add shapes to PDF files.
    /// </summary>
    /// <remarks>
    /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/draw-images-to-pdf.php
    /// </remarks>
    static void Main()
    {
        // Before starting this example, please get a free 100-day trial key:
        // https://sautinsoft.com/start-for-free/

        // Apply the key here:
        // PdfDocument.SetLicense("...");

        using (var document = new PdfDocument())
        {
            // Add a page.
            var page = document.Pages.Add();

            // Load the image from a file.
            var image = PdfImage.Load(@"..\..\..\parrot.png");

            // Set the location of the bottom-left corner of the image.
            // We want the top-left corner of the image to be at location (50, 50)
            // from the top-left corner of the page.
            // NOTE: In PDF, location (0, 0) is at the bottom-left corner of the page
            // and the positive y axis extends vertically upward.
            double x = 50, y = page.CropBox.Top - 50 - image.Size.Height;

            // Draw the image to the page.
            page.Content.DrawImage(image, new PdfPoint(x, y));

            document.Save("Parrot.pdf");
        }

        System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Parrot.pdf") { UseShellExecute = true });
    }
}

Download

Option Infer On

Imports System
Imports SautinSoft.Pdf
Imports System.IO
Imports SautinSoft.Pdf.Content

Friend Class Program
	''' <summary>
	''' Add shapes to PDF files.
	''' </summary>
	''' <remarks>
	''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/draw-images-to-pdf.php
	''' </remarks>
	Shared Sub Main()
		' Before starting this example, please get a free license:
		' https://sautinsoft.com/start-for-free/

		' Apply the key here:
		' PdfDocument.SetLicense("...");

		Using document = New PdfDocument()
			' Add a page.
			Dim page = document.Pages.Add()

			' Load the image from a file.
			Dim image = PdfImage.Load("..\..\..\parrot.png")

			' Set the location of the bottom-left corner of the image.
			' We want the top-left corner of the image to be at location (50, 50)
			' from the top-left corner of the page.
			' NOTE: In PDF, location (0, 0) is at the bottom-left corner of the page
			' and the positive y axis extends vertically upward.
			Dim x As Double = 50, y As Double = page.CropBox.Top - 50 - image.Size.Height

			' Draw the image to the page.
			page.Content.DrawImage(image, New PdfPoint(x, y))

			document.Save("Parrot.pdf")
		End Using

		System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Parrot.pdf") With {.UseShellExecute = True})
	End Sub
End Class

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.