'' This code is part of Document Solutions for PDF samples.
'' Copyright (c) MESCIUS inc. All rights reserved.
Imports System.IO
Imports System.Drawing
Imports System.Text
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing

'' Using JPEG and JPEG2000 images in DsPdf.
'' IMPORTANT NOTE: When you render an image in DsPdf multiple times (e.g. rendering
'' the same image as part of a page header on all pages), it will automatically be
'' added to a dictionary and reused throughout the document, provided you use
'' the same image object on all pages. So rather than loading the same image from
'' file (or stream) each time it is needed, it is always preferable to load the image
'' once and cache it in an image object. This applies to all image types available in
'' DsPdf (Image, RawImage, ImageWrapper).
Public Class RawImages
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        '' PDF format allows inserting JPEG and JPEG2000 images into the document 'as is',
        '' without converting to PDF native image formats. To do that in DsPdf,
        '' the RawImage class can be used, as the code below demonstrates.
        '' Create instances of RawImage from JPEG files:
        Using img As RawImage = RawImage.FromFile(Path.Combine("Resources", "Images", "puffins.jpg"), RawImageFormat.Jpeg, 800, 532),
            imgSmall As RawImage = RawImage.FromFile(Path.Combine("Resources", "ImagesBis", "puffins-small.jpg"), RawImageFormat.Jpeg, 144, 96)
            '' Text format used to draw captions:
            Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 12}

            '' Action to draw the image using various options:
            Dim drawImage As Action(Of RawImage, Page, ImageAlign, Boolean) =
            Sub(ByVal image_, ByVal page_, ByVal ia_, ByVal clip_)
                Dim rect = New RectangleF(72, 72, 72 * 4, 72 * 4)
                Dim clipped = If(clip_, "clipped to a 4""x4"" rectangle", "without clipping")
                Dim align = If(ia_ Is ImageAlign.Default, "ImageAlign.Default",
                    If(ia_ Is ImageAlign.CenterImage, "ImageAlign.CenterImage",
                    If(ia_ Is ImageAlign.StretchImage, "ImageAlign.StretchImage", "custom ImageAlign")))
                '' Draw image caption:
                page_.Graphics.DrawString($"Page {doc.Pages.IndexOf(page_) + 1}: Image drawn at (1"", 1 ""), {clipped}, using {align}:", tf, New PointF(72, 36))
                Dim clip As RectangleF? = If(clip_, rect, New RectangleF?)
                '' Draw the image:
                Dim imageRects As RectangleF() = Nothing
                page_.Graphics.DrawImage(image_, rect, clip, ia_, imageRects)
                '' Show the image outline:
                page_.Graphics.DrawRectangle(imageRects(0), Color.Red, 1, DashStyle.Solid)
                '' Show image/clip area:
                page_.Graphics.DrawRectangle(rect, Color.Blue, 1, DashStyle.Solid)
            End Sub

            '' The ImageAlign class provides various image alignment options.
            '' It also defines a few static instances with some commonly used
            '' combinations of options demonstrated below.

            '' Page 1: draw image without clipping, with default alignment:
            drawImage(img, doc.NewPage(), ImageAlign.Default, False)

            '' Page 2: draw image with clipping, with default alignment:
            drawImage(img, doc.NewPage(), ImageAlign.Default, True)

            '' Page 3: draw image with clipping, with CenterImage alignment:
            drawImage(img, doc.NewPage(), ImageAlign.CenterImage, True)

            '' Page 4: draw image without clipping and stretched image:
            drawImage(img, doc.NewPage(), ImageAlign.StretchImage, False)

            '' Page 5: draw image without clipping, fit into the rectangle, preserving aspect ratio:
            Dim ia = New ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Center, True, True, True, False, False)
            drawImage(img, doc.NewPage(), ia, False)

            '' Page 6: draw a small image tiled, without clipping, fit into the rectangle, preserving aspect ratio:
            ia = New ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, False, False, True, True, True)
            drawImage(imgSmall, doc.NewPage(), ia, False)
            '' Done:
            Return doc.Pages.Count
        End Using
    End Function
End Class