Aligning paragraphs: Near / Center / Justified / Distributed / Far

PDF TIFF SVG JPG C# VB
ParagraphAlign.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. // This sample demonstrates paragraph alignment options
  14. // (top/center/justified/bottom for horizontal LTR text).
  15. public class ParagraphAlign
  16. {
  17. public int CreatePDF(Stream stream)
  18. {
  19. var doc = new GcPdfDocument();
  20. var page = doc.NewPage();
  21. var g = page.Graphics;
  22. var tl = g.CreateTextLayout();
  23. tl.DefaultFormat.Font = StandardFonts.Times;
  24. tl.DefaultFormat.FontSize = 12;
  25. var borderColor = Color.FromArgb(217, 217, 217);
  26.  
  27. var h = (page.Size.Height - 72) / 5;
  28. var bounds = new RectangleF(36, 36, page.Size.Width - 72, h);
  29.  
  30. tl.MaxWidth = bounds.Width;
  31. tl.MaxHeight = bounds.Height;
  32.  
  33. var para = Common.Util.LoremIpsum(1, 5, 5, 10, 12);
  34.  
  35. // 1: ParagraphAlignment.Near
  36. tl.ParagraphAlignment = ParagraphAlignment.Near;
  37. tl.Append("ParagraphAlignment.Near: ");
  38. tl.Append(para);
  39. tl.PerformLayout(true);
  40. g.DrawTextLayout(tl, bounds.Location);
  41. g.DrawRectangle(bounds, borderColor);
  42.  
  43. // 2: ParagraphAlignment.Center
  44. bounds.Offset(0, h);
  45. tl.Clear();
  46. tl.ParagraphAlignment = ParagraphAlignment.Center;
  47. tl.Append("ParagraphAlignment.Center: ");
  48. tl.Append(para);
  49. tl.PerformLayout(true);
  50. g.DrawTextLayout(tl, bounds.Location);
  51. g.DrawRectangle(bounds, borderColor);
  52.  
  53. // 3: ParagraphAlignment.Justified
  54. bounds.Offset(0, h);
  55. tl.Clear();
  56. tl.ParagraphAlignment = ParagraphAlignment.Justified;
  57. tl.Append("ParagraphAlignment.Justified: ");
  58. tl.Append(para);
  59. tl.PerformLayout(true);
  60. g.DrawTextLayout(tl, bounds.Location);
  61. g.DrawRectangle(bounds, borderColor);
  62.  
  63. // 4: ParagraphAlignment.Distributed
  64. bounds.Offset(0, h);
  65. tl.Clear();
  66. tl.ParagraphAlignment = ParagraphAlignment.Distributed;
  67. tl.Append("ParagraphAlignment.Distributed: ");
  68. tl.Append(para);
  69. tl.PerformLayout(true);
  70. g.DrawTextLayout(tl, bounds.Location);
  71. g.DrawRectangle(bounds, borderColor);
  72.  
  73. // 5: ParagraphAlignment.Far
  74. bounds.Offset(0, h);
  75. tl.Clear();
  76. tl.ParagraphAlignment = ParagraphAlignment.Far;
  77. tl.Append("ParagraphAlignment.Far: ");
  78. tl.Append(para);
  79. tl.PerformLayout(true);
  80. g.DrawTextLayout(tl, bounds.Location);
  81. g.DrawRectangle(bounds, borderColor);
  82.  
  83. // Done:
  84. doc.Save(stream);
  85. return doc.Pages.Count;
  86. }
  87. }
  88. }
  89.