ImagesToPdf.vb
''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GCDRAW = GrapeCity.Documents.Drawing

Public Class ImagesToPdf
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()

        ' Load the page images from the Resources/ImagesBis folder:
        Dim pageImages = New List(Of (FileName As String, PageImage As GCDRAW.Image))()
        For Each fname In Directory.GetFiles(Path.Combine("Resources", "ImagesBis"), "Wetlands-page*.png", SearchOption.TopDirectoryOnly)
            pageImages.Add((Path.GetFileName(fname), GCDRAW.Image.FromFile(fname)))
        Next

        ' Optional: sort the page images by file name:
        pageImages.Sort(Function(a, b) String.Compare(a.FileName, b.FileName))

        ' Align images to the top-left corner without scaling, cropping, or tiling, while preserving aspect ratio:
        Dim align = New GCDRAW.ImageAlign(GCDRAW.ImageAlignHorz.Left, GCDRAW.ImageAlignVert.Top, False, False, True, False, False)

        ' Add one page per image and draw the image on the page:
        For Each pageImage In pageImages
            Dim page = doc.Pages.Add()
            page.Graphics.DrawImage(pageImage.PageImage, page.Bounds, Nothing, align)
        Next

        ' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class