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