ImageAlignment.cs
// 
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// © MESCIUS inc. All rights reserved.
// 
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsPdfWeb.Demos
{
    // Using image alignment options in DsPdf.
    // (This example replaces the older RawImages example; the RawImage class is obsolete and should not be used.)
    //
    // 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 used in DsPdf.
    public class ImageAlignment
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();

            // Text format used to draw captions:
            var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 12 };

            // Action to draw the image with various alignment options:
            Action<GCDRAW.Image, Page, ImageAlign, bool> drawImage = (image_, page_, ia_, clip_) =>
            {
                var rect = new RectangleF(72, 72, 72 * 4, 72 * 4);
                var clipped = clip_ ? "4×4インチの矩形に切り取り" : "切り取りなし";
                var align = ia_ == ImageAlign.Default ? "規定の配置" :
                    ia_ == ImageAlign.CenterImage ? "中央寄せ" :
                    ia_ == ImageAlign.StretchImage ? "矩形にあわせる" : "カスタム";
                // Draw image caption:
                page_.Graphics.DrawString($"{doc.Pages.IndexOf(page_) + 1}ページ: 画像の場所 (1\",1\"), {clipped}, 配置 {align}:", tf, new PointF(72, 36));
                var clip = clip_ ? new Nullable<RectangleF>(rect) : new Nullable<RectangleF>();
                // Draw the image:
                page_.Graphics.DrawImage(image_, rect, clip, ia_, out RectangleF[] imageRects);
                // Show the image outline:
                page_.Graphics.DrawRectangle(imageRects[0], Color.Red, 1, null);
                // Show image/clip area:
                page_.Graphics.DrawRectangle(rect, Color.Blue, 1, null);
            };

            // Create images from JPEG files (using the same Image class instance to draw an image
            // in a PDF ensures that the image data will be reused and not duplicated in the PDF):
            using var image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "puffins.jpg"));
            using var imageSmall = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "puffins-small.jpg"));

            // 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(image, doc.NewPage(), ImageAlign.Default, false);

            // Page 2: draw image with clipping, with default alignment:
            drawImage(image, doc.NewPage(), ImageAlign.Default, true);

            // Page 3: draw image with clipping, with CenterImage alignment:
            drawImage(image, doc.NewPage(), ImageAlign.CenterImage, true);

            // Page 4: draw image without clipping and stretched image:
            drawImage(image, doc.NewPage(), ImageAlign.StretchImage, false);

            // Page 5: draw image without clipping, fit into the rectangle, preserving aspect ratio:
            var ia = new ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Center, true, true, true, false, false);
            drawImage(image, 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(imageSmall, doc.NewPage(), ia, false);

            // PDF ドキュメントを保存します。
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}