Images

This example demonstrates how to draw images on a PDF document.

Rendering images in a pdf document

Any image can be rendered in a pdf

Clicking generate button will launch a pdf with images , any type of images are supported like gif, png , tif , bmp, emf

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;

namespace OfficeComponent.Samples
{
    class ImagesExample : ExampleBase
    {
        public ImagesExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {
        }

        public ImagesExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {
        }

        public override string Execute()
        {
            // Create a new instance of PdfDocument class.
            PdfDocument doc = new PdfDocument();
            string jpgImage = CommonDataPath + "\\Spring.jpg";
            string tifImage = CommonDataPath + "\\256.tif";
            string bmpImage = CommonDataPath + "\\mask2.bmp";
            string emfImage = CommonDataPath + "\\MAPPING2.EMF";
            string multiframeImage = CommonDataPath + "\\mul.tif";
            string gifImage = CommonDataPath + "\\Ani.gif";
            string pngImage = CommonDataPath + "\\PdfLogo.png";

            doc.ViewerPreferences.PageMode = PdfPageDisplayMode.DisplayFullScreen;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Tahoma", 20f, FontStyle.Bold), false);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Black);

            PdfSection section = doc.Sections.Add();
            PdfPage page = section.Pages.Add();
            PdfGraphics g = page.Graphics;

            //Metafile
            g = page.Graphics;
            PdfMetafile metafile = new PdfMetafile(emfImage);
            page.Graphics.DrawString("Metafile", font, brush, new PointF(10, 0));
            g.DrawImage(metafile, new PointF(0, 50));

            //Bitmap with Tiff image mask
            page = section.Pages.Add();
            g = page.Graphics;
            PdfImage image = new PdfBitmap(tifImage);
            ((PdfBitmap)image).Mask = new PdfImageMask(new PdfBitmap(bmpImage));
            page.Graphics.DrawString("Image mask", font, brush, new PointF(10, 0));
            g.DrawImage(image, 80, 40);

            //Image pagination -jpg
            image = new PdfBitmap(jpgImage);

            PdfLayoutSettings format = new PdfLayoutSettings();
            format.Layout = PdfLayoutType.Paginate;

            PointF location = new PointF(0, 400);
            SizeF size = new SizeF(400, -1);
            RectangleF rect = new RectangleF(location, size);
            page.Graphics.DrawString("Image Pagination", font, brush, new PointF(10, 360));
            image.Draw(page, rect, format);

            //Multiframe Tiff image
            PdfBitmap tiffImage = new PdfBitmap(multiframeImage);

            int frameCount = tiffImage.FrameCount;

            for (int i = 0; i < frameCount; i++)
            {
                page = section.Pages.Add();
                section.PageSettings.Margins.All = 0;
                g = page.Graphics;
                tiffImage.ActiveFrame = i;
                g.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height);
            }

            page = section.Pages.Add();
            g = page.Graphics;
            image = new PdfBitmap(gifImage);
            g.DrawImage(image, 0, 0, page.Graphics.ClientSize.Width, image.Height);
            page.Graphics.DrawString("Gif Image", font, brush, new PointF(10, 0));

            section.Pages[section.Pages.Count - 2].Graphics.DrawString("Tiff Image", font, brush, new PointF(10, 10));
            section.PageSettings.Transition.PageDuration = 1;
            section.PageSettings.Transition.Duration = 1;
            section.PageSettings.Transition.Style = PdfTransitionEffect.Fly;

            //PNG Image
            page = section.Pages.Add();
            g = page.Graphics;
            image = new PdfBitmap(pngImage);
            page.Graphics.DrawString("PNG Image", font, brush, new PointF(10, 10));
            g.DrawImage(image, new PointF(150, 150));




            // Save and close the document.
            var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
            doc.Save(outputPath);
            doc.Close(true);

            return outputPath;
        }
    }
}
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics

Namespace OfficeComponent.Samples
	Friend Class ImagesExample
		Inherits ExampleBase
		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)
		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)
		End Sub

		Public Overrides Function Execute() As String
			' Create a new instance of PdfDocument class.
			Dim doc As New PdfDocument()
			Dim jpgImage As String = CommonDataPath & "\Spring.jpg"
			Dim tifImage As String = CommonDataPath & "\256.tif"
			Dim bmpImage As String = CommonDataPath & "\mask2.bmp"
			Dim emfImage As String = CommonDataPath & "\MAPPING2.EMF"
			Dim multiframeImage As String = CommonDataPath & "\mul.tif"
			Dim gifImage As String = CommonDataPath & "\Ani.gif"
			Dim pngImage As String = CommonDataPath & "\PdfLogo.png"

			doc.ViewerPreferences.PageMode = PdfPageDisplayMode.DisplayFullScreen
			Dim font As New PdfTrueTypeFont(New Font("Tahoma", 20F, FontStyle.Bold), False)
			Dim brush As New PdfSolidBrush(Color.Black)

			Dim section As PdfSection = doc.Sections.Add()
			Dim page As PdfPage = section.Pages.Add()
			Dim g As PdfGraphics = page.Graphics

			'Metafile
			g = page.Graphics
			Dim metafile As New PdfMetafile(emfImage)
			page.Graphics.DrawString("Metafile", font, brush, New PointF(10, 0))
			g.DrawImage(metafile, New PointF(0, 50))

			'Bitmap with Tiff image mask
			page = section.Pages.Add()
			g = page.Graphics
			Dim image As PdfImage = New PdfBitmap(tifImage)
			CType(image, PdfBitmap).Mask = New PdfImageMask(New PdfBitmap(bmpImage))
			page.Graphics.DrawString("Image mask", font, brush, New PointF(10, 0))
			g.DrawImage(image, 80, 40)

			'Image pagination -jpg
			image = New PdfBitmap(jpgImage)

			Dim format As New PdfLayoutSettings()
			format.Layout = PdfLayoutType.Paginate

			Dim location As New PointF(0, 400)
			Dim size As New SizeF(400, -1)
			Dim rect As New RectangleF(location, size)
			page.Graphics.DrawString("Image Pagination", font, brush, New PointF(10, 360))
			image.Draw(page, rect, format)

			'Multiframe Tiff image
			Dim tiffImage As New PdfBitmap(multiframeImage)

			Dim frameCount As Integer = tiffImage.FrameCount

			For i As Integer = 0 To frameCount - 1
				page = section.Pages.Add()
				section.PageSettings.Margins.All = 0
				g = page.Graphics
				tiffImage.ActiveFrame = i
				g.DrawImage(tiffImage, 0, 0, page.GetClientSize().Width, page.GetClientSize().Height)
			Next i

			page = section.Pages.Add()
			g = page.Graphics
			image = New PdfBitmap(gifImage)
			g.DrawImage(image, 0, 0, page.Graphics.ClientSize.Width, image.Height)
			page.Graphics.DrawString("Gif Image", font, brush, New PointF(10, 0))

			section.Pages(section.Pages.Count - 2).Graphics.DrawString("Tiff Image", font, brush, New PointF(10, 10))
			section.PageSettings.Transition.PageDuration = 1
			section.PageSettings.Transition.Duration = 1
			section.PageSettings.Transition.Style = PdfTransitionEffect.Fly

			'PNG Image
			page = section.Pages.Add()
			g = page.Graphics
			image = New PdfBitmap(pngImage)
			page.Graphics.DrawString("PNG Image", font, brush, New PointF(10, 10))
			g.DrawImage(image, New PointF(150, 150))




			' Save and close the document.
			Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
			doc.Save(outputPath)
			doc.Close(True)

			Return outputPath
		End Function
	End Class
End Namespace