ImageAlignment.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Drawing;
  11. using GCTEXT = GrapeCity.Documents.Text;
  12. using GCDRAW = GrapeCity.Documents.Drawing;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // Using image alignment options in DsPdf.
  17. // (This example replaces the older RawImages example; the RawImage class is obsolete and should not be used.)
  18. //
  19. // NOTE: When you render an image in DsPdf multiple times (e.g. rendering
  20. // the same image as part of a page header on all pages), it will automatically be
  21. // added to a dictionary and reused throughout the document, provided you use
  22. // the same image object on all pages. So rather than loading the same image from
  23. // file (or stream) each time it is needed, it is always preferable to load the image
  24. // once and cache it in an image object. This applies to all image types used in DsPdf.
  25. public class ImageAlignment
  26. {
  27. public int CreatePDF(Stream stream)
  28. {
  29. var doc = new GcPdfDocument();
  30.  
  31. // Text format used to draw captions:
  32. var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 12 };
  33.  
  34. // Action to draw the image with various alignment options:
  35. Action<GCDRAW.Image, Page, ImageAlign, bool> drawImage = (image_, page_, ia_, clip_) =>
  36. {
  37. var rect = new RectangleF(72, 72, 72 * 4, 72 * 4);
  38. var clipped = clip_ ? "clipped to a 4\"x4\" rectangle" : "without clipping";
  39. var align = ia_ == ImageAlign.Default ? "ImageAlign.Default" :
  40. ia_ == ImageAlign.CenterImage ? "ImageAlign.CenterImage" :
  41. ia_ == ImageAlign.StretchImage ? "ImageAlign.StretchImage" : "custom ImageAlign";
  42. // Draw image caption:
  43. page_.Graphics.DrawString($"Page {doc.Pages.IndexOf(page_) + 1}: Image drawn at (1\",1\"), {clipped}, using {align}:", tf, new PointF(72, 36));
  44. var clip = clip_ ? new Nullable<RectangleF>(rect) : new Nullable<RectangleF>();
  45. // Draw the image:
  46. page_.Graphics.DrawImage(image_, rect, clip, ia_, out RectangleF[] imageRects);
  47. // Show the image outline:
  48. page_.Graphics.DrawRectangle(imageRects[0], Color.Red, 1, null);
  49. // Show image/clip area:
  50. page_.Graphics.DrawRectangle(rect, Color.Blue, 1, null);
  51. };
  52.  
  53. // Create images from JPEG files (using the same Image class instance to draw an image
  54. // in a PDF ensures that the image data will be reused and not duplicated in the PDF):
  55. using var image = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "puffins.jpg"));
  56. using var imageSmall = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "puffins-small.jpg"));
  57.  
  58. // The ImageAlign class provides various image alignment options.
  59. // It also defines a few static instances with some commonly used
  60. // combinations of options demonstrated below.
  61.  
  62. // Page 1: draw image without clipping, with default alignment:
  63. drawImage(image, doc.NewPage(), ImageAlign.Default, false);
  64.  
  65. // Page 2: draw image with clipping, with default alignment:
  66. drawImage(image, doc.NewPage(), ImageAlign.Default, true);
  67.  
  68. // Page 3: draw image with clipping, with CenterImage alignment:
  69. drawImage(image, doc.NewPage(), ImageAlign.CenterImage, true);
  70.  
  71. // Page 4: draw image without clipping and stretched image:
  72. drawImage(image, doc.NewPage(), ImageAlign.StretchImage, false);
  73.  
  74. // Page 5: draw image without clipping, fit into the rectangle, preserving aspect ratio:
  75. var ia = new ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Center, true, true, true, false, false);
  76. drawImage(image, doc.NewPage(), ia, false);
  77.  
  78. // Page 6: draw a small image tiled, without clipping, fit into the rectangle, preserving aspect ratio:
  79. ia = new ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, false, false, true, true, true);
  80. drawImage(imageSmall, doc.NewPage(), ia, false);
  81.  
  82. // Done:
  83. doc.Save(stream);
  84. return doc.Pages.Count;
  85. }
  86. }
  87. }
  88.  
  89.