Effortless Image Transformation in PDFs using C# and .NET

Working with images in PDF documents is an important part of software development, especially when it comes to creating reports, brochures, or any other documents where precise placement and manipulation of graphic elements are required. In this article, we will look at how to use C# and .NET for managing the positioning and conversion of images in PDF documents using SautinSoft.PDF components.

It is important to understand several key points related to image processing in PDF:

  • Positioning: Determines the location of the image on the page.
  • Transformation: Includes zooming, rotating and reflecting the image.
  • Step-by-step guide:

    1. Add SautinSoft.PDF from NuGet.
    2. Load an image from a file.
    3. Set the location of the four images at the corners of the page.
    4. Draw four images and do: Scale, Rotate, Mirror, Transform.
    5. Save the document.

    Полный код

    using System;
    using SautinSoft.Pdf;
    using System.IO;
    using SautinSoft.Pdf.Content;
    
    class Program
    {
        /// <summary>
        /// Export and import images to PDF file.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/image-positioning-and-transformations.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())
            {
                var page = document.Pages.Add();
    
                // Load the image from a file.
                var image = PdfImage.Load(@"..\..\..\submit.png");
    
                double margin = 50;
    
                // Set the location of the first image in the top-left corner of the page (with a specified margin).
                double x = margin;
                double y = page.CropBox.Top - margin - 100;
    
                // Draw the first image.
                page.Content.DrawImage(image, new PdfPoint(x, y), new PdfSize(100, 100));
    
                // Set the location of the second image in the top-right corner of the page (with the same margin).
                x = page.CropBox.Right - margin - 100;
                y = page.CropBox.Top - margin - 100;
    
                // Initialize the transformation.
                var transform = PdfMatrix.Identity;
                // Use the translate operation to position the image.
                transform.Translate(x, y);
                // Use the scale operation to resize the image.
                // NOTE: The unit square of user space, bounded by user coordinates (0, 0) and (1, 1),
                // corresponds to the boundary of the image in the image space.
                transform.Scale(100, 100);
                // Use the scale operation to flip the image horizontally.
                transform.Scale(-1, 1, 0.5, 0);
    
                // Draw the second image.
                page.Content.DrawImage(image, transform);
    
                // Set the location of the third image in the bottom-left corner of the page (with the same margin).
                x = margin;
                y = margin;
    
                // Initialize the transformation.
                transform = PdfMatrix.Identity;
                // Use the translate operation to position the image.
                transform.Translate(x, y);
                // Use the scale operation to resize the image.
                transform.Scale(100, 100);
                // Use the scale operation to flip the image vertically.
                transform.Scale(1, -1, 0, 0.5);
    
                // Draw the third image.
                page.Content.DrawImage(image, transform);
    
                // Set the location of the fourth image in the bottom-right corner of the page (with the same margin).
                x = page.CropBox.Right - margin - 100;
                y = margin;
    
                // Initialize the transformation.
                transform = PdfMatrix.Identity;
                // Use the translate operation to position the image.
                transform.Translate(x, y);
                // Use the scale operation to resize the image.
                transform.Scale(100, 100);
                // Use the scale operation to flip the image horizontally and vertically.
                transform.Scale(-1, -1, 0.5, 0.5);
    
                // Draw the fourth image.
                page.Content.DrawImage(image, transform);
    
                document.Save("PositioningTransformations.pdf");
            }
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("PositioningTransformations.pdf") { UseShellExecute = true });
        }
    }

    Download

    Option Infer On
    
    Imports System
    Imports SautinSoft.Pdf
    Imports System.IO
    Imports SautinSoft.Pdf.Content
    
    Friend Class Program
    	''' <summary>
    	''' Export and import images to PDF file.
    	''' </summary>
    	''' <remarks>
    	''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/image-positioning-and-transformations.php
    	''' </remarks>
    	Shared Sub 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 document = New PdfDocument()
    			Dim page = document.Pages.Add()
    
    			' Load the image from a file.
    			Dim image = PdfImage.Load("..\..\..\submit.png")
    
    			Dim margin As Double = 50
    
    			' Set the location of the first image in the top-left corner of the page (with a specified margin).
    			Dim x As Double = margin
    			Dim y As Double = page.CropBox.Top - margin - image.Size.Height
    
    			' Draw the first image.
    			page.Content.DrawImage(image, New PdfPoint(x, y))
    
    			' Set the location of the second image in the top-right corner of the page (with the same margin).
    			x = page.CropBox.Right - margin - image.Size.Width
    			y = page.CropBox.Top - margin - image.Size.Height
    
    			' Initialize the transformation.
    			Dim transform = PdfMatrix.Identity
    			' Use the translate operation to position the image.
    			transform.Translate(x, y)
    			' Use the scale operation to resize the image.
    			' NOTE: The unit square of user space, bounded by user coordinates (0, 0) and (1, 1),
    			' corresponds to the boundary of the image in the image space.
    			transform.Scale(image.Size.Width, image.Size.Height)
    			' Use the scale operation to flip the image horizontally.
    			transform.Scale(-1, 1, 0.5, 0)
    
    			' Draw the second image.
    			page.Content.DrawImage(image, transform)
    
    			' Set the location of the third image in the bottom-left corner of the page (with the same margin).
    			x = margin
    			y = margin
    
    			' Initialize the transformation.
    			transform = PdfMatrix.Identity
    			' Use the translate operation to position the image.
    			transform.Translate(x, y)
    			' Use the scale operation to resize the image.
    			transform.Scale(image.Size.Width, image.Size.Height)
    			' Use the scale operation to flip the image vertically.
    			transform.Scale(1, -1, 0, 0.5)
    
    			' Draw the third image.
    			page.Content.DrawImage(image, transform)
    
    			' Set the location of the fourth image in the bottom-right corner of the page (with the same margin).
    			x = page.CropBox.Right - margin - image.Size.Width
    			y = margin
    
    			' Initialize the transformation.
    			transform = PdfMatrix.Identity
    			' Use the translate operation to position the image.
    			transform.Translate(x, y)
    			' Use the scale operation to resize the image.
    			transform.Scale(image.Size.Width, image.Size.Height)
    			' Use the scale operation to flip the image horizontally and vertically.
    			transform.Scale(-1, -1, 0.5, 0.5)
    
    			' Draw the fourth image.
    			page.Content.DrawImage(image, transform)
    
    			document.Save("PositioningTransformations.pdf")
    		End Using
    		System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("PositioningTransformations.pdf") With {.UseShellExecute = True})
    	End Sub
    End Class
    

    Download


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



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

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