HiQPdf Documentation

PDF Images

HiQPdf Client for .NET Core

The PDF images are represented in HiQPdf software by the PdfImage class. A PdfImage object can be created from a System.Drawing.Image object or from an image file from disk. Images can be laid out at any position in a PDF canvas, you can let the image free to the right and the bottom of the canvas or you can limit it in a box.

The images can be opaque in PDF or they can be drawn using the alpha transparency of the image. By default the images rendered by the PdfImage objects are using the image transparency. To force the library to always draw an opaque image in PDF you have to set the PdfImageAlphaBlending property on false.

The text objects are paginated by default, which means that when an image gets to the bottom of a PDF page while flowing down it will automatically continue on the next PDF page.

It is also possible to write vectorial SVG images to PDF and this can be done with a PdfHtml object as you can see in the demo application below.

PDF Images Demo

In this demo you can learn how to layout image objects in a PDF document. There are basically four types of images that can be laid out in a PDF document which are exemplified in this demo: PNG images with alpha transparency, opaque JPEG images, vectorial SVG images and images resulted from HTML documents rasterization. From this demo you can also learn how to layout a rotated image in PDF using rotations and traslations of the coordinates system.

Demo Source Code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;

using HiQPdfClient;
using Microsoft.AspNetCore.Hosting.Server;

namespace HiQPdf_Demo.Controllers
{
    public class PdfImagesController : Controller
    {
        IWebHostEnvironment m_hostingEnvironment;
        public PdfImagesController(IWebHostEnvironment hostingEnvironment)
        {
            m_hostingEnvironment = hostingEnvironment;
        }

        // GET: PdfImages
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreatePdf(IFormCollection collection)
        {
            string serverIP = collection["textBoxServerIP"];
            uint serverPort = uint.Parse(collection["textBoxServerPort"]);
            string serverPassword = collection["textBoxServerPassword"];

            // create a PDF document
            PdfDocument document = new PdfDocument(serverIP, serverPort);

            // use server password if necessary
            if (serverPassword.Length > 0)
                document.ServerPassword = serverPassword;

            // set a demo serial number
            document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // create a page in document
            PdfPage page1 = document.AddPage();

            // set a background color for the page
            PdfRectangle backgroundRectangle = new PdfRectangle(0, 0, page1.Size.Width, page1.Size.Height);
            backgroundRectangle.BackColor = PdfColor.WhiteSmoke;
            page1.Layout(backgroundRectangle);

            // create the true type fonts that can be used in document text
            PdfFont pdfFontEmbed = document.CreateFontFromName("Times New Roman", 10, true);

            float startYPos = 20;
            float startXPos = 5;

            #region Layout transparent image

            PdfText titleTextTransImage = new PdfText(startXPos, startYPos,
                    "PNG image with alpha transparency:", pdfFontEmbed);
            titleTextTransImage.ForeColor = PdfColor.Navy;
            page1.Layout(titleTextTransImage);

            // layout a PNG image with alpha transparency
            PdfImage transparentPdfImage = new PdfImage(0, 0, m_hostingEnvironment.WebRootPath + "/DemoFiles/Images/HiQPdfLogo_small.png");
            document.Layout(transparentPdfImage, 10);

            #endregion

            #region Layout resized transparent image

            PdfText titleTextTransImageResized = new PdfText(0, 0,
                    "The transparent PNG image below is resized:", pdfFontEmbed);
            titleTextTransImageResized.ForeColor = PdfColor.Navy;
            document.Layout(titleTextTransImageResized, 10);

            // layout a PNG image with alpha transparency
            PdfImage transparentResizedPdfImage = new PdfImage(0, 0, 50, m_hostingEnvironment.WebRootPath + "/DemoFiles/Images/HiQPdfLogo_small.png");
            document.Layout(transparentResizedPdfImage, 10);

            #endregion

            #region Layout rotated transparent image

            PdfText titleTextTransImageRotated = new PdfText(0, 0,
                    "The transparent PNG image below is rotated 180 degrees counter clockwise:", pdfFontEmbed);
            titleTextTransImageRotated.ForeColor = PdfColor.Navy;
            document.Layout(titleTextTransImageRotated, 10);

            // rotate the PNG image with alpha transparency 180 degrees counter clockwise
            PdfImage transparentRotatedPdfImage = new PdfImage(0, 0, 50, m_hostingEnvironment.WebRootPath + "/DemoFiles/Images/HiQPdfLogo_small.png");
            // translate the coordinates system to image location
            transparentRotatedPdfImage.SetTranslation(0, 0);
            // rotate the coordinates system counter clockwise
            transparentRotatedPdfImage.SetRotationAngle(180);
            // translate back the coordinates system
            transparentRotatedPdfImage.SetTranslation(-50, -50);

            document.Layout(transparentRotatedPdfImage, 10);

            #endregion

            #region Layout clipped transparent image

            PdfText titleTextTransClippedImage = new PdfText(0, 0,
                    "The transparent PNG image below is clipped:", pdfFontEmbed);
            titleTextTransClippedImage.ForeColor = PdfColor.Navy;
            document.Layout(titleTextTransClippedImage, 0, true, true, 60, true, false);

            // layout a clipped PNG image with alpha transparency
            PdfImage transparentClippedPdfImage = new PdfImage(0, 0, 50, m_hostingEnvironment.WebRootPath + "/DemoFiles/Images/HiQPdfLogo_small.png");
            transparentClippedPdfImage.ClipRectangle = new RectangleFloat(0, 0, 50, 25);

            document.Layout(transparentClippedPdfImage, 10);

            #endregion

            #region Layout JPEG image

            PdfText titleTextOpaqueImage = new PdfText(0, 0, "The JPG image below is opaque:", pdfFontEmbed);
            titleTextOpaqueImage.ForeColor = PdfColor.Navy;
            document.Layout(titleTextOpaqueImage, 0, true, true, transparentClippedPdfImage.ClipRectangle.Height + 10, true, false);

            // layout an opaque JPG image
            PdfImage opaquePdfImage = new PdfImage(0, 0, m_hostingEnvironment.WebRootPath + "/DemoFiles/Images/HiQPdfLogo_small.jpg");

            document.Layout(opaquePdfImage, 10);

            #endregion

            #region Layout clipped JPEG image

            PdfText titleTextClippedImage = new PdfText(0, 0, "The JPG image below is clipped:", pdfFontEmbed);
            titleTextClippedImage.ForeColor = PdfColor.Navy;
            document.Layout(titleTextClippedImage, 10);

            // layout a clipped image
            PdfImage clippedPdfImage = new PdfImage(0, 0, 50, m_hostingEnvironment.WebRootPath + "/DemoFiles/Images/HiQPdfLogo_small.jpg");
            clippedPdfImage.ClipRectangle = new RectangleFloat(0, 0, 50, 25);

            document.Layout(clippedPdfImage, 10);

            #endregion

            #region Layout a vectorial SVG image

            PdfText titleTextSvgImage = new PdfText(0, 0, "Vectorial SVG image:", pdfFontEmbed);
            titleTextSvgImage.ForeColor = PdfColor.Navy;
            document.Layout(titleTextSvgImage, 0, true, true, clippedPdfImage.ClipRectangle.Height + 10, true, false);

            string svgImageCode = System.IO.File.ReadAllText(m_hostingEnvironment.WebRootPath + "/DemoFiles/Svg/SvgImage.svg");

            PdfHtml svgImage = new PdfHtml(0, 0, svgImageCode, null);
            document.Layout(svgImage, 10);

            #endregion

            #region Layout JPEG image on multiple pages

            PdfText titleTexMultiPageImage = new PdfText(0, 0, "The JPG image below is laid out on 2 pages:", pdfFontEmbed);
            titleTexMultiPageImage.ForeColor = PdfColor.Navy;
            document.Layout(titleTexMultiPageImage, 10);

            // layout an opaque JPG image on 2 pages
            PdfImage paginatedPdfImage = new PdfImage(0, 0, m_hostingEnvironment.WebRootPath + "/DemoFiles/Images/HiQPdfLogo_big.jpg");

            document.Layout(paginatedPdfImage, 10);

            #endregion

            #region Layout the screenshot of a HTML document

            PdfText titleTextHtmlImage = new PdfText(0, 0, "HTML document screenshot:", pdfFontEmbed);
            titleTextHtmlImage.ForeColor = PdfColor.Navy;
            document.Layout(titleTextHtmlImage, 10);

            string htmlFile = "http://www.hiqpdf.com/ClientDemo" + @"/DemoFiles/Html/Logo.Html";

            PdfHtmlImage htmlRasterImage = new PdfHtmlImage(0, 0, htmlFile);
            htmlRasterImage.BrowserWidth = 400;
            document.Layout(htmlRasterImage, 10);

            #endregion

            try
            {
                // write the PDF document to a memory buffer
                byte[] pdfBuffer = document.WriteToMemory();

                FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
                fileResult.FileDownloadName = "PdfImages.pdf";

                return fileResult;
            }
            finally
            {
                document.Close();
            }
        }
    }
}
See Also

Other Resources