Skip Navigation Links.
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.
Server Settings
IP: Port: Password:
Skip Navigation Links
C# Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HiQPdfClient;

namespace HiQPdf_Demo
{
    public partial class PdfImages : System.Web.UI.Page
    {
        protected void buttonCreatePdf_Click(object sender, EventArgs e)
        {
            string serverIP = textBoxServerIP.Text;
            uint serverPort = uint.Parse(textBoxServerPort.Text);
            string serverPassword = textBoxServerPassword.Text;

            // 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, Server.MapPath("~") + @"\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, Server.MapPath("~") + @"\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, Server.MapPath("~") + @"\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, Server.MapPath("~") + @"\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, Server.MapPath("~") + @"\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, Server.MapPath("~") + @"\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(Server.MapPath("~") + @"\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, Server.MapPath("~") + @"\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();

                // inform the browser about the binary data format
                HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");

                // let the browser know how to open the PDF document and the file name
                HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=PdfImages.pdf; size={0}",
                            pdfBuffer.Length.ToString()));

                // write the PDF buffer to HTTP response
                HttpContext.Current.Response.BinaryWrite(pdfBuffer);

                // call End() method of HTTP response to stop ASP.NET page processing
                HttpContext.Current.Response.End();
            }
            finally
            {
                document.Close();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Master.SelectNode("pdfImages");

                Master.LoadCodeSample("PdfImages");
            }
        }
    }
}