ImagesToPdf.cs
//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Collections.Generic;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Drawing;

namespace DsPdfWeb.Demos
{
    // Shows how to create a PDF document from images, with each image representing a full document page.
    public class ImagesToPdf
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();

            // Load the page images from the Resources/ImagesBis folder:
            var pageImages = new List<(string FileName, Image Image)>();
            foreach (var fname in Directory.GetFiles(Path.Combine("Resources", "ImagesBis"), "Wetlands-page*.png", SearchOption.TopDirectoryOnly))
                pageImages.Add((Path.GetFileName(fname), Image.FromFile(fname)));

            // Optional: sort the page images by file name:
            pageImages.Sort((a, b) => string.Compare(a.FileName, b.FileName));

            // Align images to the top-left corner without scaling, cropping, or tiling, while preserving aspect ratio:
            var align = new ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, false, false, true, false, false);

            // Add one page per image and draw the image on the page:
            foreach (var pageImage in pageImages)
            {
                var page = doc.Pages.Add();
                page.Graphics.DrawImage(pageImage.Image, page.Bounds, null, align);
            }

            // Done:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}