Customizing PDFs with Images in 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:

Полный код

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


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.