VerticalTextJP.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.Drawing;
  10. using GrapeCity.Documents.Pdf;
  11. using GrapeCity.Documents.Text;
  12. using GCTEXT = GrapeCity.Documents.Text;
  13. using GCDRAW = GrapeCity.Documents.Drawing;
  14.  
  15. namespace DsPdfWeb.Demos.Basics
  16. {
  17. // Draws vertical right-to-left Japanese text using a layout with horizontal columns.
  18. // See also ArabicColumns, MultiLang and VerticalText.
  19. public class VerticalTextJP
  20. {
  21. const string text = "日本語(にほんご、にっぽんご)は、主として、日本列島で使用されてきた言語である。日本手話を母語とする者などを除いて、ほぼ全ての日本在住者は日本語を第一言語とする。日本国は法令上、公用語を明記していないが、事実上の公用語となっており、学校教育の「国語」で教えられる。使用者は、日本国内を主として約\uFF11億\uFF13千万人。日本語の文法体系や音韻体系を反映する手話として日本語対応手話がある。";
  22.  
  23. public int CreatePDF(Stream stream)
  24. {
  25. using (var clouds = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "clouds.jpg")))
  26. using (var firth = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "firth.jpg")))
  27. using (var lavender = GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "lavender.jpg")))
  28. {
  29. var yumin = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "yumin.ttf"));
  30. var ia = new ImageAlign(ImageAlignHorz.Left, ImageAlignVert.Top, true, true, true, false, false);
  31.  
  32. var doc = new GcPdfDocument();
  33.  
  34. // The TextLayout that will hold and render the text:
  35. var tl = new TextLayout(72);
  36. tl.FirstLineIndent = 18;
  37. tl.ParagraphSpacing = 6;
  38. tl.FlowDirection = FlowDirection.VerticalRightToLeft;
  39. tl.TextAlignment = TextAlignment.Justified;
  40. var tf = new TextFormat() { Font = yumin, FontSize = 12 };
  41. // Repeat test text to fill a few pages:
  42. for (int i = 0; i < 25; ++i)
  43. {
  44. tl.Append(text, tf);
  45. tl.AppendLine();
  46. }
  47.  
  48. // Layout text in 4 horizontal columns:
  49. // (The logic/code in this sample is identical to ArabicColumns):
  50. const int NCOLS = 4;
  51. var margin = 36f;
  52. var gap = 18f;
  53. var page = doc.NewPage();
  54. page.Landscape = true;
  55. var colHeight = (page.Size.Height - margin * 2 - gap * (NCOLS - 1)) / NCOLS;
  56. tl.MaxWidth = page.Size.Width;
  57. tl.MaxHeight = page.Size.Height;
  58. tl.MarginLeft = tl.MarginRight = margin;
  59. tl.MarginTop = margin;
  60. tl.MarginBottom = margin + (colHeight + gap) * (NCOLS - 1);
  61. // We can specify arbitrary rectangles for the text to flow around.
  62. // In this case, we add 3 areas to draw some images:
  63. tl.ObjectRects = new List<ObjectRect>()
  64. {
  65. new ObjectRect(page.Size.Width - margin - 267, margin, 267, 200),
  66. new ObjectRect(margin + 100, margin + 60, 133, 100),
  67. new ObjectRect(margin, page.Size.Height - margin - 301, 200, 301),
  68. };
  69. // Convert object rects to image areas, adjust to provide nice looking padding:
  70. var rClouds = tl.ObjectRects[0].ToRectangleF();
  71. rClouds.Inflate(-4, -3);
  72. var rFirth = tl.ObjectRects[1].ToRectangleF();
  73. rFirth.Inflate(-4, -3);
  74. var rLavender = tl.ObjectRects[2].ToRectangleF();
  75. rLavender.Inflate(-4, -3);
  76. page.Graphics.DrawImage(clouds, rClouds, null, ia);
  77. page.Graphics.DrawImage(firth, rFirth, null, ia);
  78. page.Graphics.DrawImage(lavender, rLavender, null, ia);
  79.  
  80. // THE call: it calculates the glyphs needed to draw the text, and lays it out:
  81. tl.PerformLayout(true);
  82.  
  83. // Loop while there is still text to render:
  84. bool done = false;
  85. while (!done)
  86. {
  87. for (int col = 0; col < NCOLS; ++col)
  88. {
  89. int nextcol = (col < NCOLS - 1) ? col + 1 : 0;
  90. // TextSplitOptions tell TextLayout.Split() how to layout the remaining text.
  91. // In this case we advance from column to column by updating top and bottom margins:
  92. var tso = new TextSplitOptions(tl)
  93. {
  94. RestMarginTop = margin + (colHeight + gap) * nextcol,
  95. RestMarginBottom = margin + (colHeight + gap) * (NCOLS - 1 - nextcol)
  96. };
  97. var split = tl.Split(tso, out TextLayout rest);
  98. page.Graphics.DrawTextLayout(tl, PointF.Empty);
  99. if (split != SplitResult.Split)
  100. {
  101. done = true;
  102. break;
  103. }
  104. tl = rest;
  105. }
  106. if (!done)
  107. {
  108. page = doc.NewPage();
  109. page.Landscape = true;
  110. // We only want to render images on the first page, so clear ObjectRect:
  111. if (tl.ObjectRects != null)
  112. {
  113. tl.ObjectRects = null;
  114. // We need to redo the layout, but no need to recalculate the glyphs:
  115. tl.PerformLayout(false);
  116. }
  117. }
  118. }
  119. // Done:
  120. doc.Save(stream);
  121. return doc.Pages.Count;
  122. }
  123. }
  124. }
  125. }
  126.