TextAlign.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.Drawing;
  7. using System.IO;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10.  
  11. namespace DsPdfWeb.Demos.Basics
  12. {
  13. // Demonstrates text alignment options (horizontal alignment for LRT text):
  14. // Left / Centered / Right / Justified.
  15. public class TextAlign
  16. {
  17. public int CreatePDF(Stream stream)
  18. {
  19. // Helper function to generate a paragraph of random text:
  20. Func<string> makePara = () => Common.Util.LoremIpsum(1, 3, 4, 15, 20);
  21. //
  22. var doc = new GcPdfDocument();
  23. var g = doc.NewPage().Graphics;
  24. // Using Graphics.CreateTextLayout() ensures that TextLayout's resolution
  25. // is set to the same value as that of the graphics (which is 72 dpi by default):
  26. var tl = g.CreateTextLayout();
  27. tl.DefaultFormat.Font = StandardFonts.Times;
  28. tl.DefaultFormat.FontSize = 12;
  29. // Set layout size (full page with 1" margins):
  30. tl.MaxWidth = doc.Pages.Last.Size.Width - 72 * 2;
  31. tl.MaxHeight = doc.Pages.Last.Size.Height - 72 * 2;
  32. // Strong text format for 'headers':
  33. var tf = new TextFormat(tl.DefaultFormat) { Font = StandardFonts.TimesBold };
  34. // Insertion point:
  35. var ip = new PointF(72, 72);
  36. // TextAlignment controls how text is aligned horizontally within the layout rectangle.
  37. // We render 5 paragraphs with different alignments.
  38. // Leading:
  39. tl.TextAlignment = TextAlignment.Leading;
  40. tl.Append("TextAlignment.Leading: ", tf);
  41. tl.Append(makePara());
  42. tl.PerformLayout(true);
  43. g.DrawTextLayout(tl, ip);
  44. // Advance insertion point, adding one line's height between paragraphs:
  45. ip.Y += tl.ContentHeight + tl.Lines[0].Height;
  46. // Center:
  47. tl.Clear();
  48. tl.TextAlignment = TextAlignment.Center;
  49. tl.Append("TextAlignment.Center: ", tf);
  50. tl.Append(makePara());
  51. tl.PerformLayout(true);
  52. g.DrawTextLayout(tl, ip);
  53. // Advance insertion point, adding one line's height between paragraphs:
  54. ip.Y += tl.ContentHeight + tl.Lines[0].Height;
  55. // Trailing:
  56. tl.Clear();
  57. tl.TextAlignment = TextAlignment.Trailing;
  58. tl.Append("TextAlignment.Trailing: ", tf);
  59. tl.Append(makePara());
  60. tl.PerformLayout(true);
  61. g.DrawTextLayout(tl, ip);
  62. // Advance insertion point, adding one line's height between paragraphs:
  63. ip.Y += tl.ContentHeight + tl.Lines[0].Height;
  64. // Justified:
  65. tl.Clear();
  66. tl.TextAlignment = TextAlignment.Justified;
  67. tl.Append("TextAlignment.Justified: ", tf);
  68. tl.Append(makePara());
  69. tl.PerformLayout(true);
  70. g.DrawTextLayout(tl, ip);
  71. // Advance insertion point, adding one line's height between paragraphs:
  72. ip.Y += tl.ContentHeight + tl.Lines[0].Height;
  73. // Distributed:
  74. tl.Clear();
  75. tl.TextAlignment = TextAlignment.Distributed;
  76. tl.Append("TextAlignment.Distributed: ", tf);
  77. tl.Append(makePara());
  78. tl.PerformLayout(true);
  79. g.DrawTextLayout(tl, ip);
  80. // Done:
  81. doc.Save(stream);
  82. return doc.Pages.Count;
  83. }
  84. }
  85. }
  86.