SlidePages.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Pdf
  8. Imports GrapeCity.Documents.Text
  9. Imports GrapeCity.Documents.Drawing
  10. Imports GCTEXT = GrapeCity.Documents.Text
  11. Imports GCDRAW = GrapeCity.Documents.Drawing
  12.  
  13. '' Creates pages of 'slides' from all images found in a directory.
  14. ''
  15. '' IMPORTANT NOTE: When you render an image in DsPdf multiple times (e.g. rendering
  16. '' the same image as part of a page header on all pages), it will automatically be
  17. '' added to a dictionary and reused throughout the document, provided you use
  18. '' the same image object on all pages. So rather than loading the same image from
  19. '' file (or stream) each time it is needed, it is always preferable to load the image
  20. '' once and cache it in an image object. This applies to all image types used in DsPdf.
  21. Public Class SlidePages
  22. Function CreatePDF(ByVal stream As Stream) As Integer
  23. Dim doc = New GcPdfDocument()
  24. '' Get a font for captions:
  25. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf"))
  26. '' GcPdfDocument.ImageOptions controls various image-related settings.
  27. '' In particular, we can lower the JPEG quality from the default 75% to reduce the file size:
  28. doc.ImageOptions.JpegQuality = 50
  29.  
  30. '' Load all images from the Resources/Images folder:
  31. Dim images = New List(Of Tuple(Of String, IImage))
  32. For Each fname In Directory.GetFiles(Path.Combine("Resources", "Images"), "*", SearchOption.AllDirectories)
  33. images.Add(Tuple.Create(Path.GetFileName(fname), Util.ImageFromFile(fname)))
  34. Next
  35.  
  36. images.Shuffle()
  37. '' Print all images as slide sheets in a 3x4 grid with 1/2" margins all around:
  38. Const margin = 36.0F
  39. Const rows = 4
  40. Const cols = 3
  41. Dim gapx = 72.0F / 4, gapy = gapx
  42. Dim sWidth = (doc.PageSize.Width - margin * 2 + gapx) / cols
  43. Dim sHeight = (doc.PageSize.Height - margin * 2 + gapy) / rows
  44. If sWidth > sHeight Then
  45. gapx += sWidth - sHeight
  46. sWidth = sHeight
  47. Else
  48. gapy += sHeight - sWidth
  49. sHeight = sWidth
  50. End If
  51.  
  52. Const sMargin = 72.0F / 6
  53. '' Set up image alignment that would center images within the specified area:
  54. Dim ia = New ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Center, True, True, True, False, False)
  55. '' Text format for image captions:
  56. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = sMargin * 0.65F}
  57. '' Insertion point:
  58. Dim ip = New PointF(margin, margin)
  59. Dim g = doc.NewPage().Graphics
  60. For i = 0 To images.Count() - 1
  61. Dim rect = New RectangleF(ip, New SizeF(sWidth - gapx, sHeight - gapy))
  62. g.FillRectangle(rect, Color.LightGray)
  63. g.DrawRectangle(rect, Color.Black, 0.5F)
  64. rect.Inflate(-sMargin, -sMargin)
  65. '' We get the actual rectangle where the image was drawn from the DrawImage method
  66. '' (via an output parameter) so that we can draw a thin border exactly around the image
  67. '' (an array is required as the image can be tiled, in which case multiple rectangles
  68. '' will be returned):
  69. Dim imageRect As RectangleF() = Nothing
  70. g.DrawImage(images(i).Item2, rect, Nothing, ia, imageRect)
  71. g.DrawRectangle(imageRect(0), Color.DarkGray, 1)
  72. '' Print image file name as caption in the bottom slide margin:
  73. g.DrawString(Path.GetFileName(images(i).Item1), tf,
  74. New RectangleF(rect.X, rect.Bottom, rect.Width, sMargin),
  75. TextAlignment.Center, ParagraphAlignment.Near, False)
  76. ip.X += sWidth
  77. If ip.X + sWidth > doc.PageSize.Width AndAlso i < images.Count() - 1 Then
  78. ip.X = margin
  79. ip.Y += sHeight
  80. If ip.Y + sHeight > doc.PageSize.Height Then
  81. g = doc.NewPage().Graphics
  82. ip.Y = margin
  83. End If
  84. End If
  85. Next
  86. ''
  87. '' Done:
  88. doc.Save(stream)
  89. '' Dispose images (can be done only after saving the document):
  90. images.ForEach(Sub(t_) t_.Item2.Dispose())
  91. Return doc.Pages.Count
  92. End Function
  93. End Class
  94.