VerticalText.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.Collections.Generic;
  8. using System.Drawing;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // Demonstrates rendering of vertical text in LTR and RTL modes.
  16. // Also shows how to have text flow around rectangular objects.
  17. // See also JapaneseColumns.
  18. public class VerticalText
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. var doc = new GcPdfDocument();
  23. var page = doc.NewPage();
  24. // Use Landscape orientation:
  25. page.Landscape = true;
  26. var g = page.Graphics;
  27. // Some sample texts in Japanese, English and Arabic:
  28. string text1 = "学校教育の「国語」で教えられる。";
  29. string text2 = " flow direction. ";
  30. string text3 = "النص العربي 12 + 34 = 46 مع الأرقام ";
  31. // Init font cache and get the required fonts:
  32. var fc = new FontCollection();
  33. fc.RegisterDirectory(Path.Combine("Resources", "Fonts"));
  34. var fYuMin = fc.FindFamilyName("Yu Mincho");
  35. var fTimes = fc.FindFamilyName("Times New Roman");
  36. var fArial = fc.FindFamilyName("Arial");
  37. // Create text formats:
  38. var tf1 = new TextFormat() { Font = fYuMin };
  39. var tf2 = new TextFormat() { Font = fTimes };
  40. var tf3 = new TextFormat() { Font = fArial };
  41. // Create TextLayout and set some options on it:
  42. var tl = g.CreateTextLayout();
  43. tl.FirstLineIndent = 36;
  44. tl.TextAlignment = TextAlignment.Justified;
  45. // This setting justifies the last line too:
  46. tl.LastLineIsEndOfParagraph = false;
  47. // Set all margins to 1":
  48. tl.MarginAll = tl.Resolution;
  49. tl.MaxWidth = page.Size.Width;
  50. tl.MaxHeight = page.Size.Height;
  51. // RTL layout:
  52. tl.RightToLeft = false;
  53. // Build a list of objects for the text to flow around:
  54. tl.ObjectRects = new List<ObjectRect>()
  55. {
  56. new ObjectRect(540, 100, 120, 160),
  57. new ObjectRect(100, 290, 170, 100),
  58. new ObjectRect(500, 350, 170, 100)
  59. };
  60. // Fill corresponding rectangels on page so that we can see them:
  61. foreach (var or in tl.ObjectRects)
  62. g.FillRectangle(or.ToRectangleF(), Color.PaleVioletRed);
  63.  
  64. // Add text to layout:
  65. for (int i = 0; i < 3; i++)
  66. {
  67. tl.Append(text1, tf1);
  68. tl.Append("Horizontal Top To Bottom" + text2, tf2);
  69. tl.AppendLine(text3, tf3);
  70. }
  71. // Perform and draw first layout:
  72. tl.PerformLayout(true);
  73. g.DrawTextLayout(tl, PointF.Empty);
  74. g.FillRectangle(tl.ContentRectangle, Color.FromArgb(20, Color.Red));
  75.  
  76. // Create 2nd layout - vertical rotated counter-clockwise:
  77. var t = tl.ContentHeight;
  78. tl.Clear();
  79. tl.RotateSidewaysCounterclockwise = true;
  80. tl.FlowDirection = FlowDirection.VerticalLeftToRight;
  81. tl.MarginTop += t;
  82. // Add text to layout:
  83. for (int i = 0; i < 3; i++)
  84. {
  85. tl.Append(text1, tf1);
  86. tl.Append("Vertical Left To Right" + text2, tf2);
  87. tl.AppendLine(text3, tf3);
  88. }
  89. // Perform and draw second layout:
  90. tl.PerformLayout(true);
  91. g.DrawTextLayout(tl, PointF.Empty);
  92. g.FillRectangle(tl.ContentRectangle, Color.FromArgb(20, Color.Green));
  93.  
  94. // Create 3rd layout - vertical:
  95. tl.Clear();
  96. tl.FlowDirection = FlowDirection.VerticalRightToLeft;
  97. tl.RotateSidewaysCounterclockwise = false;
  98. // Add text to layout:
  99. for (int i = 0; i < 3; i++)
  100. {
  101. tl.Append(text1, tf1);
  102. tl.Append("Vertical Right To Left" + text2, tf2);
  103. tl.AppendLine(text3, tf3);
  104. }
  105. // Perform and draw third layout:
  106. tl.PerformLayout(true);
  107. g.DrawTextLayout(tl, PointF.Empty);
  108. g.FillRectangle(tl.ContentRectangle, Color.FromArgb(20, Color.Blue));
  109. // Done:
  110. doc.Save(stream);
  111. return doc.Pages.Count;
  112. }
  113. }
  114. }
  115.