PDF Creator ASP.NET Core samples · .NET 10 and .NET 11

Images

Adds images to a PDF page: at their natural size, with a given width and the height that keeps the aspect ratio, stretched to a given width and height, rotated and semi-transparent.

C# code of this sample

Pages/ImagesDemo.cshtml.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using ExpertPdf.PdfCreator;

namespace ExpertPdf.PdfCreator.Samples.Pages
{
    public class ImagesDemoModel : PageModel
    {
        [BindProperty]
        public bool Transparent { get; set; } = true;

        public string Message { get; set; }

        public void OnGet()
        {
        }

        public IActionResult OnPost()
        {
            try
            {
                return File(CreatePdf(), "application/pdf", "Images.pdf");
            }
            catch (Exception ex)
            {
                Message = "The PDF document was not created. " + ex.Message;
                return Page();
            }
        }

        private byte[] CreatePdf()
        {
            // LicensingManager.LicenseKey = "your license key";

            Document document = new Document();
            PdfPage page = document.AddPage();
            PdfFont font = document.AddFont(new Font("Arial", 10, GraphicsUnit.Point), true);

            string boxImage = Path.Combine(Helper.FilesFolder, @"Images\box.jpg");

            // the natural size of the image; the result of AddElement gives the bounds of the image in the page,
            // used to place the next image to its right
            page.AddElement(new TextElement(0, 0, "Natural size", font));
            AddElementResult result = page.AddElement(new ImageElement(0, 15, boxImage));

            // a given width, the height that keeps the aspect ratio
            float x = result.EndPageBounds.Right + 20;
            page.AddElement(new TextElement(x, 0, "Width 100 points", font));
            result = page.AddElement(new ImageElement(x, 15, 100, boxImage));

            // a given width and height: the image is stretched
            x = result.EndPageBounds.Right + 20;
            page.AddElement(new TextElement(x, 0, "150 x 100 points", font));
            page.AddElement(new ImageElement(x, 15, 150, 100, boxImage));

            // an image from a System.Drawing.Image object
            using (Image logo = Image.FromFile(Path.Combine(Helper.FilesFolder, @"img\logo.jpg")))
            {
                page.AddElement(new TextElement(0, 300, "From a System.Drawing.Image", font));
                page.AddElement(new ImageElement(0, 315, 250, logo));

                if (Transparent)
                {
                    // Transparency goes from 0 (invisible) to 100 (opaque); Rotate turns the element clockwise
                    ImageElement watermark = new ImageElement(300, 400, 250, logo);
                    watermark.Transparency = 40;
                    watermark.Rotate(-30);
                    page.AddElement(watermark);
                }
            }

            byte[] pdf = document.Save();
            document.Close();
            return pdf;
        }
    }
}