Generate pages of slides from all images in a directory

PDF TIFF SVG JPG C# VB
SlidePages.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 System.Linq;
  9. using System.Collections.Generic;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Drawing;
  13. using DsPdfWeb.Demos.Common;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsPdfWeb.Demos
  18. {
  19. // Creates pages of 'slides' from all images found in a directory.
  20. //
  21. // IMPORTANT NOTE: When you render an image in DsPdf multiple times (e.g. rendering
  22. // the same image as part of a page header on all pages), it will automatically be
  23. // added to a dictionary and reused throughout the document, provided you use
  24. // the same image object on all pages. So rather than loading the same image from
  25. // file (or stream) each time it is needed, it is always preferable to load the image
  26. // once and cache it in an image object. This applies to all image types used in DsPdf.
  27. public class SlidePages
  28. {
  29. public int CreatePDF(Stream stream)
  30. {
  31. var doc = new GcPdfDocument();
  32. // Get a font for captions:
  33. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf"));
  34. // GcPdfDocument.ImageOptions allow you to control various image-related settings.
  35. // In particular, we can lower the JPEG quality from the default 75% to reduce the file size:
  36. doc.ImageOptions.JpegQuality = 50;
  37.  
  38. // Load all images from the Resources/Images folder:
  39. List<Tuple<string, IImage>> images = new List<Tuple<string, IImage>>();
  40. foreach (var fname in Directory.GetFiles(Path.Combine("Resources", "Images"), "*", SearchOption.AllDirectories))
  41. images.Add(Tuple.Create(Path.GetFileName(fname), Util.ImageFromFile(fname)));
  42. images.Shuffle();
  43. // Print all images as slide sheets in a 3x4 grid with 1/2" margins all around:
  44. const float margin = 36;
  45. const int rows = 4;
  46. const int cols = 3;
  47. float gapx = 72f / 4, gapy = gapx;
  48. float sWidth = (doc.PageSize.Width - margin * 2 + gapx) / cols;
  49. float sHeight = (doc.PageSize.Height - margin * 2 + gapy) / rows;
  50. if (sWidth > sHeight)
  51. {
  52. gapx += sWidth - sHeight;
  53. sWidth = sHeight;
  54. }
  55. else
  56. {
  57. gapy += sHeight - sWidth;
  58. sHeight = sWidth;
  59. }
  60. const float sMargin = 72f / 6;
  61. // Set up image alignment that would center images within the specified area:
  62. var ia = new ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Center, true, true, true, false, false);
  63. // Text format for image captions:
  64. var tf = new TextFormat() { Font = font, FontSize = sMargin * 0.65f };
  65. // Insertion point:
  66. var ip = new PointF(margin, margin);
  67. var g = doc.NewPage().Graphics;
  68. for (int i = 0; i < images.Count(); ++i)
  69. {
  70. var rect = new RectangleF(ip, new SizeF(sWidth - gapx, sHeight - gapy));
  71. g.FillRectangle(rect, Color.LightGray);
  72. g.DrawRectangle(rect, Color.Black, 0.5f);
  73. rect.Inflate(-sMargin, -sMargin);
  74. // We get the actual rectangle where the image was drawn from the DrawImage method
  75. // (via an output parameter) so that we can draw a thin border exactly around the image
  76. // (an array is required as the image can be tiled, in which case multiple rectangles
  77. // will be returned):
  78. g.DrawImage(images[i].Item2, rect, null, ia, out RectangleF[] imageRect);
  79. g.DrawRectangle(imageRect[0], Color.DarkGray, 1);
  80. // Print image file name as caption in the bottom slide margin:
  81. g.DrawString(Path.GetFileName(images[i].Item1), tf,
  82. new RectangleF(rect.X, rect.Bottom, rect.Width, sMargin),
  83. TextAlignment.Center, ParagraphAlignment.Near, false);
  84. ip.X += sWidth;
  85. if (ip.X + sWidth > doc.PageSize.Width && i < images.Count() - 1)
  86. {
  87. ip.X = margin;
  88. ip.Y += sHeight;
  89. if (ip.Y + sHeight > doc.PageSize.Height)
  90. {
  91. g = doc.NewPage().Graphics;
  92. ip.Y = margin;
  93. }
  94. }
  95. }
  96. // Done:
  97. doc.Save(stream);
  98. // Dispose images after saving the PDF:
  99. images.ForEach(t_ => t_.Item2.Dispose());
  100. return doc.Pages.Count;
  101. }
  102. }
  103. }
  104.