InlineImages.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. '' This sample shows how to insert arbitrary objecs (images in this sample)
  14. '' into a block of text so that those objects keep their positions relative
  15. '' to the surrounding text, and are laid out exactly like other text runs,
  16. '' and can participate in text flow.
  17. Public Class InlineImages
  18. Function CreatePDF(ByVal stream As Stream) As Integer
  19. '' Get the images to use as inline objects:
  20. Using imgPuffins As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "puffins-small.jpg")),
  21. imgFerns As GCDRAW.Image = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "ferns-small.jpg"))
  22. '' The image alignment to use:
  23. Dim ia = New ImageAlign(ImageAlignHorz.Center, ImageAlignVert.Bottom, True, True, True, False, False)
  24. '' Create and set up the document:
  25. Dim doc = New GcPdfDocument()
  26. Dim page = doc.NewPage()
  27. Dim g = page.Graphics
  28. '' Create and set up a TextLayout object to print the text:
  29. Dim tl = g.CreateTextLayout()
  30. tl.MaxWidth = page.Size.Width
  31. tl.MaxHeight = page.Size.Height
  32. tl.MarginLeft = 36
  33. tl.MarginRight = 36
  34. tl.MarginTop = 36
  35. tl.MarginBottom = 36
  36. tl.DefaultFormat.Font = StandardFonts.Times
  37. tl.DefaultFormat.FontSize = 12
  38. tl.DefaultFormat.BackColor = Color.LightGoldenrodYellow
  39. tl.TextAlignment = TextAlignment.Justified
  40. '' Create inline objects using the images and arbitrary sizes:
  41. Dim ioPuffins = New InlineObject(imgPuffins, 36, 24)
  42. Dim ioFerns = New InlineObject(imgFerns, 36, 24)
  43. '' Build up the text:
  44. tl.Append("The 'Inline objects' feature of the TextLayout class allows inserting arbitrary objects" +
  45. "into a block of text. Those objects are then treated exactly like other text runs, " +
  46. "and keep their positions relative to the surrounding text. " +
  47. "In this sample, we insert some images into the text as inline objects, " +
  48. "use the TextLayout class to position them along with text, and draw them " +
  49. "using the GcGraphics.DrawImage method.")
  50. tl.Append("Here are some puffins:")
  51. tl.Append(ioPuffins)
  52. tl.Append("and here are some ferns:")
  53. tl.Append(ioFerns)
  54. tl.Append("The end.")
  55. ''
  56. Debug.Assert(tl.InlineObjects.Count = 0, "InlineObjects is filled by RecalculateGlyphs")
  57. '' This method fetches and measures the glyphs needed to render the text,
  58. '' because we draw the same text a few times with different layout,
  59. '' we call this once before the loop below:
  60. tl.RecalculateGlyphs()
  61. ''
  62. Debug.Assert(tl.InlineObjects.Count = 2, "InlineObjects is filled by RecalculateGlyphs")
  63. '' In a loop, draw the text and inline images in 3 different locations
  64. '' and bounds on the page:
  65. For i = 0 To 2
  66. tl.MarginTop = tl.ContentRectangle.Bottom + 36
  67. tl.MarginLeft = 36 + 72 * i
  68. tl.MarginRight = 36 + 72 * i
  69. '' Note passing 'false' here, we do not need to recalc the glyphs because
  70. '' the text has not changed:
  71. tl.PerformLayout(False)
  72. '' Draw the text and images:
  73. g.DrawTextLayout(tl, PointF.Empty)
  74. For Each inlineObj In tl.InlineObjects
  75. g.DrawImage(CType(inlineObj.Object, GCDRAW.Image), inlineObj.ObjectRect.ToRectangleF(), Nothing, ia)
  76. Next
  77. Next
  78. ''
  79. '' Done:
  80. doc.Save(stream)
  81. Return doc.Pages.Count
  82. End Using
  83. End Function
  84. End Class
  85.