WordCharWrap.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 GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Drawing;
  11.  
  12. namespace DsPdfWeb.Demos.Basics
  13. {
  14. // This sample demonstrates the difference between WordWrap and CharWrap
  15. // text wrapping modes.
  16. public class WordCharWrap
  17. {
  18. public int CreatePDF(Stream stream)
  19. {
  20. var str =
  21. "Lose nothing in your documents! Document Solutions for PDF includes text and paragraph formatting, " +
  22. "special characters, multiple languages, RTL support, vertical and rotated text on all supported platforms.";
  23.  
  24. var doc = new GcPdfDocument();
  25. var page = doc.NewPage();
  26. var g = page.Graphics;
  27.  
  28. var tl = g.CreateTextLayout();
  29. tl.Append(str);
  30. tl.DefaultFormat.Font = StandardFonts.Times;
  31. tl.DefaultFormat.FontSize = 12;
  32. tl.MaxWidth = 72 * 3;
  33.  
  34. tl.WrapMode = WrapMode.WordWrap;
  35. tl.PerformLayout(true);
  36.  
  37. var dy = tl.Lines[0].Height + 72 / 16;
  38. var rc = new RectangleF(72, 72 + dy, tl.MaxWidth.Value, 72 * 1.4F);
  39.  
  40. g.DrawString("WrapMode.WordWrap:", tl.DefaultFormat, new PointF(rc.Left, rc.Top - dy));
  41. g.DrawTextLayout(tl, rc.Location);
  42. g.DrawRectangle(rc, Color.CornflowerBlue);
  43.  
  44. rc.Offset(0, 72 * 2);
  45. tl.WrapMode = WrapMode.CharWrap;
  46. tl.PerformLayout(false);
  47. g.DrawString("WrapMode.CharWrap:", tl.DefaultFormat, new PointF(rc.Left, rc.Top - dy));
  48. g.DrawTextLayout(tl, rc.Location);
  49. g.DrawRectangle(rc, Color.CornflowerBlue);
  50.  
  51. // Done:
  52. doc.Save(stream);
  53. return doc.Pages.Count;
  54. }
  55. }
  56. }
  57.