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