ImageLinks.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 System.Numerics
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Drawing
  11. Imports GrapeCity.Documents.Pdf.Annotations
  12. Imports GrapeCity.Documents.Pdf.Actions
  13. Imports GCTEXT = GrapeCity.Documents.Text
  14. Imports GCDRAW = GrapeCity.Documents.Drawing
  15.  
  16. '' This sample loads all images found in a directory, then renders each image
  17. '' in the largest possible size on a separate page of the PDF.
  18. '' Finally it inserts a TOC of image thumbnails linked to the large images
  19. '' into the document.
  20. '' See also SlidePages.
  21. Public Class ImageLinks
  22. Private Class ImageInfo
  23. Public Property Name As String
  24. Public Property Image As IImage
  25. Public Property PageIdx As Integer
  26. End Class
  27.  
  28. Function CreatePDF(ByVal stream As Stream) As Integer
  29. Dim doc = New GcPdfDocument()
  30. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf"))
  31. '' 1/4" page margins all around:
  32. Const margin = 36.0F
  33.  
  34. '' Load all images from the Resources/Images folder:
  35. Dim imageInfos As New List(Of ImageInfo)
  36. For Each fname In Directory.GetFiles(Path.Combine("Resources", "Images"), "*", SearchOption.AllDirectories)
  37. imageInfos.Add(New ImageInfo() With {.Name = Path.GetFileName(fname), .Image = Util.ImageFromFile(fname)})
  38. Next
  39. imageInfos.Shuffle()
  40. '' Set up image alignment that would center images horizontally and align to top vertically:
  41. Dim ia = New ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Top, True, True, True, False, False)
  42. '' Image rectangle for full-sized images - whole page:
  43. Dim rBig = New RectangleF(margin, margin, doc.PageSize.Width - margin * 2, doc.PageSize.Height - margin * 2)
  44. '' Render all images full-size, one image per page:
  45. For i = 0 To imageInfos.Count - 1
  46. Dim g = doc.NewPage().Graphics
  47. Dim ii = imageInfos(i)
  48. g.DrawImage(ii.Image, rBig, Nothing, ia)
  49. ii.PageIdx = i
  50. Next
  51. '' Insert page(s) with thumbnails into the beginning of the document as a 4x5 grid (see SlidePages):
  52. Const rows = 5
  53. Const cols = 4
  54. Dim gapx = 72.0F / 4, gapy = gapx
  55. Dim sWidth = (doc.PageSize.Width - margin * 2 + gapx) / cols
  56. Dim sHeight = (doc.PageSize.Height - margin * 2 + gapy) / rows
  57. If (sWidth > sHeight) Then
  58. gapx += sWidth - sHeight
  59. sWidth = sHeight
  60. Else
  61. gapy += sHeight - sWidth
  62. sHeight = sWidth
  63. End If
  64. Const sMargin = 72.0F / 6
  65. '' Center thumbnails vertically too:
  66. ia.AlignVert = ImageAlignVert.Center
  67. '' Text format for image captions:
  68. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = sMargin * 0.65F}
  69. '' Insertion point:
  70. Dim ip = New PointF(margin, margin)
  71. Dim page = doc.Pages.Insert(0)
  72. For i = 0 To imageInfos.Count() - 1
  73. Dim ii = imageInfos(i)
  74. Dim rect = New RectangleF(ip, New SizeF(sWidth - gapx, sHeight - gapy))
  75. '' Add a link to the page where the full-sized image is (the page index
  76. '' will be updated when we know how many pages are in TOC, see below):
  77. page.Annotations.Add(New LinkAnnotation(rect, New DestinationFit(ii.PageIdx)))
  78. '' Draw thumbnail:
  79. Dim g = page.Graphics
  80. g.FillRectangle(rect, Color.LightGray)
  81. g.DrawRectangle(rect, Color.Black, 0.5F)
  82. rect.Inflate(-sMargin, -sMargin)
  83. Dim imageRect As RectangleF() = Nothing
  84. g.DrawImage(ii.Image, rect, Nothing, ia, imageRect)
  85. g.DrawRectangle(imageRect(0), Color.DarkGray, 1)
  86. '' Print image file name as caption in the bottom slide margin:
  87. g.DrawString(ii.Name, tf,
  88. New RectangleF(rect.X, rect.Bottom, rect.Width, sMargin),
  89. TextAlignment.Center, ParagraphAlignment.Near, False)
  90. ip.X += sWidth
  91. If (ip.X + sWidth > doc.PageSize.Width) Then
  92. ip.X = margin
  93. ip.Y += sHeight
  94. If (ip.Y + sHeight > doc.PageSize.Height) Then
  95. page = doc.Pages.Insert(doc.Pages.IndexOf(page) + 1)
  96. ip.Y = margin
  97. End If
  98. End If
  99. Next
  100. '' We now go through all TOC pages, updating page indices in links' destinations
  101. '' to account for the TOC pages inserted in the beginning of the document:
  102. Dim tocPages = doc.Pages.IndexOf(page) + 1
  103. For i = 0 To tocPages - 1
  104. For Each ann In doc.Pages(i).Annotations
  105. If TypeOf ann Is LinkAnnotation AndAlso TypeOf CType(ann, LinkAnnotation).Dest Is DestinationFit Then
  106. Dim link = DirectCast(ann, LinkAnnotation)
  107. Dim dest = DirectCast(CType(ann, LinkAnnotation).Dest, DestinationFit)
  108. link.Dest = New DestinationFit(dest.PageIndex.Value + tocPages)
  109. End If
  110. Next
  111. Next
  112. ''
  113. '' Done:
  114. doc.Save(stream)
  115. imageInfos.ForEach(Sub(ii_) ii_.Image.Dispose())
  116. Return doc.Pages.Count
  117. End Function
  118. End Class
  119.