HiQPdf Documentation

PDF Images

Quickly Create High Quality PDFs

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#
private void buttonCreatePdf_Click(object sender, EventArgs e)
{
    // create a PDF document
    PdfDocument document = new PdfDocument();

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

    // set a background color for the page
    PdfRectangle backgroundRectangle = new PdfRectangle(page1.DrawableRectangle);
    backgroundRectangle.BackColor = Color.WhiteSmoke;
    page1.Layout(backgroundRectangle);

    // create the true type fonts that can be used in document text
    Font sysFont = new Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point);
    PdfFont pdfFont = document.CreateFont(sysFont);
    PdfFont pdfFontEmbed = document.CreateFont(sysFont, true);

    float crtYPos = 20;
    float crtXPos = 5;

    #region Layout transparent image

    PdfText titleTextTransImage = new PdfText(crtXPos, crtYPos,
            "PNG image with alpha transparency:", pdfFontEmbed);
    titleTextTransImage.ForeColor = Color.Navy;
    PdfLayoutInfo textLayoutInfo = page1.Layout(titleTextTransImage);

    // advance the Y position in the PDF page
    crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

    // layout a PNG image with alpha transparency
    PdfImage transparentPdfImage = new PdfImage(crtXPos, crtYPos, Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo_small.png");

    PdfLayoutInfo imageLayoutInfo = page1.Layout(transparentPdfImage);

    // advance the Y position in the PDF page
    crtYPos += imageLayoutInfo.LastPageRectangle.Height + 10;

    #endregion

    #region Layout resized transparent image

    PdfText titleTextTransImageResized = new PdfText(crtXPos, crtYPos,
            "The transparent PNG image below is resized:", pdfFontEmbed);
    titleTextTransImageResized.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTextTransImageResized);

    // advance the Y position in the PDF page
    crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

    // layout a PNG image with alpha transparency
    PdfImage transparentResizedPdfImage = new PdfImage(crtXPos, crtYPos, 50, Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo_small.png");
    imageLayoutInfo = page1.Layout(transparentResizedPdfImage);

    // advance the Y position in the PDF page
    crtYPos += imageLayoutInfo.LastPageRectangle.Height + 10;

    #endregion

    #region Layout rotated transparent image

    PdfText titleTextTransImageRotated = new PdfText(crtXPos, crtYPos,
            "The transparent PNG image below is rotated 180 degrees counter clockwise:", pdfFontEmbed);
    titleTextTransImageRotated.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTextTransImageRotated);

    // advance the Y position in the PDF page
    crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

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

    imageLayoutInfo = page1.Layout(transparentRotatedPdfImage);

    // advance the Y position in the PDF page
    crtYPos += 50 + 10;

    #endregion

    #region Layout clipped transparent image

    PdfText titleTextTransClippedImage = new PdfText(crtXPos, crtYPos,
            "The transparent PNG image below is clipped:", pdfFontEmbed);
    titleTextTransClippedImage.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTextTransClippedImage);

    // advance the Y position in the PDF page
    crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

    // layout a clipped PNG image with alpha transparency
    PdfImage transparentClippedPdfImage = new PdfImage(crtXPos, crtYPos, 50, Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo_small.png");
    transparentClippedPdfImage.ClipRectangle = new RectangleF(crtXPos, crtYPos, 50, 25);

    imageLayoutInfo = page1.Layout(transparentClippedPdfImage);

    // advance the Y position in the PDF page
    crtYPos += transparentClippedPdfImage.ClipRectangle.Height + 10;

    #endregion

    #region Layout JPEG image

    PdfText titleTextOpaqueImage = new PdfText(crtXPos, crtYPos, "The JPG image below is opaque:", pdfFontEmbed);
    titleTextOpaqueImage.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTextOpaqueImage);

    // advance the Y position in the PDF page
    crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

    // layout an opaque JPG image
    PdfImage opaquePdfImage = new PdfImage(crtXPos, crtYPos, Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo_small.jpg");

    imageLayoutInfo = page1.Layout(opaquePdfImage);

    // advance the Y position in the PDF page
    crtYPos += imageLayoutInfo.LastPageRectangle.Height + 10;

    #endregion

    #region Layout clipped JPEG image

    PdfText titleTextClippedImage = new PdfText(crtXPos, crtYPos, "The JPG image below is clipped:", pdfFontEmbed);
    titleTextClippedImage.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTextClippedImage);

    // advance the Y position in the PDF page
    crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

    // layout a clipped image
    PdfImage clippedPdfImage = new PdfImage(crtXPos, crtYPos, 50, Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo_small.jpg");
    clippedPdfImage.ClipRectangle = new RectangleF(crtXPos, crtYPos, 50, 25);

    imageLayoutInfo = page1.Layout(clippedPdfImage);

    // advance the Y position in the PDF page
    crtYPos += clippedPdfImage.ClipRectangle.Height + 10;

    #endregion

    #region Layout a vectorial SVG image

    PdfText titleTextSvgImage = new PdfText(crtXPos, crtYPos, "Vectorial SVG image:", pdfFontEmbed);
    titleTextSvgImage.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTextSvgImage);

    // advance the Y position in the PDF page
    crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

    string svgImageCode = System.IO.File.ReadAllText(Application.StartupPath + @"\DemoFiles\Svg\SvgImage.svg");

    PdfHtml svgImage = new PdfHtml(crtXPos, crtYPos, svgImageCode, null);
    PdfLayoutInfo svgLayoutInfo = page1.Layout(svgImage);

    // advance the Y position in the PDF page
    crtYPos += svgImage.ConversionInfo.PdfRegions[0].Rectangle.Height + 10;

    #endregion

    #region Layout JPEG image on multiple pages

    PdfText titleTexMultiPageImage = new PdfText(crtXPos, crtYPos, "The JPG image below is laid out on 2 pages:", pdfFontEmbed);
    titleTexMultiPageImage.ForeColor = Color.Navy;
    textLayoutInfo = page1.Layout(titleTexMultiPageImage);

    // advance the Y position in the PDF page
    crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

    // layout an opaque JPG image on 2 pages
    PdfImage paginatedPdfImage = new PdfImage(crtXPos, crtYPos, Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo_big.jpg");

    imageLayoutInfo = page1.Layout(paginatedPdfImage);

    #endregion

    // get the last page
    PdfPage crtPage = document.Pages[imageLayoutInfo.LastPageIndex];
    crtYPos = imageLayoutInfo.LastPageRectangle.Bottom + 10;

    #region Layout the screenshot of a HTML document

    PdfText titleTextHtmlImage = new PdfText(crtXPos, crtYPos, "HTML document screenshot:", pdfFontEmbed);
    titleTextHtmlImage.ForeColor = Color.Navy;
    textLayoutInfo = crtPage.Layout(titleTextHtmlImage);

    // advance the Y position in the PDF page
    crtYPos += textLayoutInfo.LastPageRectangle.Height + 10;

    string htmlFile = Application.StartupPath + @"\DemoFiles\Html\Logo.Html";

    PdfHtmlImage htmlRasterImage = new PdfHtmlImage(crtXPos, crtYPos, htmlFile);
    htmlRasterImage.BrowserWidth = 400;
    PdfLayoutInfo htmlLayoutInfo = crtPage.Layout(htmlRasterImage);

    // advance the Y position in the PDF page
    crtYPos += htmlRasterImage.ConversionInfo.PdfRegions[0].Rectangle.Height + 10;

    #endregion

    Cursor = Cursors.WaitCursor;
    string pdfFile = Application.StartupPath + @"\DemoOutput\PdfText.pdf";
    try
    {
        document.WriteToFile(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Cannot create the PDF document. {0}", ex.Message));
        return;
    }
    finally
    {
        document.Close();
        Cursor = Cursors.Arrow;
    }

    // open the created PDF document
    try
    {
        System.Diagnostics.Process.Start(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("The PDF document was created but cannot open '{0}'. {1}", pdfFile, ex.Message));
    }
}
See Also

Other Resources