TextJustifyRules.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. using GCTEXT = GrapeCity.Documents.Text;
  11. using GCDRAW = GrapeCity.Documents.Drawing;
  12.  
  13. namespace DsPdfWeb.Demos.Basics
  14. {
  15. // TBD:
  16.  
  17. public class TextJustifyRules
  18. {
  19. public int CreatePDF(Stream stream)
  20. {
  21. var arialbd = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arialbd.ttf"));
  22. var arialuni = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arialuni.ttf"));
  23.  
  24. var doc = new GcPdfDocument();
  25. var page = doc.NewPage();
  26. var g = page.Graphics;
  27.  
  28. var tl = g.CreateTextLayout();
  29. //tl.LineBreakingRules = LineBreakingRules.Unicode;
  30. //tl.WordBoundaryRules = WordBoundaryRules.Unicode;
  31. //tl.TextExtensionStrategy = TextExtensionStrategy.Default;
  32. tl.TextAlignment = TextAlignment.Distributed;
  33. tl.JustifiedSpaceExtension = 0f;
  34. tl.JustifiedTextExtension = 20f;
  35.  
  36. var tf = new TextFormat { FontSize = 26f, Font = arialuni };
  37. var tfInfo = new TextFormat { FontSize = 11f, Font = arialbd };
  38.  
  39. float marginx = 120, marginy = 36;
  40. tl.MaxWidth = page.Size.Width - marginx * 2;
  41. var text = "abcdefg!1010101010abc;999999本列島で使され99 555";
  42.  
  43. float DrawText(TextLayout tl, float y)
  44. {
  45. var pt = new PointF(marginx, y + 20);
  46. tl.Append(text, tf);
  47. tl.PerformLayout(true);
  48. var rc = new RectangleF(pt, new SizeF(tl.ContentWidth, tl.ContentHeight));
  49. g.FillRectangle(rc, Color.PaleGoldenrod);
  50. g.DrawString($"LineBreakingRules.{tl.LineBreakingRules}, TextExtensionStrategy.{tl.TextExtensionStrategy}:",
  51. tfInfo, new PointF(marginx / 2f, y));
  52. g.DrawTextLayout(tl, pt);
  53. tl.Clear();
  54. return rc.Bottom + 16;
  55. }
  56.  
  57. float y = marginy, dy = marginy * 3.5f;
  58. y = DrawText(tl, y);
  59. tl.TextExtensionStrategy = TextExtensionStrategy.EastAsianExcel;
  60. y = DrawText(tl, y);
  61. tl.TextExtensionStrategy = TextExtensionStrategy.Excel;
  62. y = DrawText(tl, y);
  63. tl.LineBreakingRules = LineBreakingRules.Simplified;
  64. tl.WordBoundaryRules = WordBoundaryRules.Simplified;
  65. tl.TextExtensionStrategy = TextExtensionStrategy.Default;
  66. y = DrawText(tl, y);
  67. tl.TextExtensionStrategy = TextExtensionStrategy.EastAsianExcel;
  68. y = DrawText(tl, y);
  69. tl.TextExtensionStrategy = TextExtensionStrategy.Excel;
  70. y = DrawText(tl, y);
  71.  
  72. // Done:
  73. doc.Save(stream);
  74. return doc.Pages.Count;
  75. }
  76. }
  77. }
  78.