CharacterFormatting.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.Drawing;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GCTEXT = GrapeCity.Documents.Text;
  12. using GCDRAW = GrapeCity.Documents.Drawing;
  13.  
  14. namespace DsPdfWeb.Demos.Basics
  15. {
  16. // Demonstrates the basics of character formatting in DsPdf.
  17. //
  18. // Character formatting in DsPdf is done via GrapeCity.Documents.Text.TextFormat class.
  19. // An instance of that class with the required formatting options
  20. // is passed to most text rendering methods available in DsPdf (e.g. DrawString).
  21. // Rendering text with different character formatting in the same paragraph
  22. // is done by using TextLayout/DrawTextLayout.
  23. // See also TextRendering, MultiFormattedText, ParagraphAlign,
  24. // ParagraphFormatting, TextAlign.
  25. public class CharacterFormatting
  26. {
  27. public int CreatePDF(Stream stream)
  28. {
  29. var doc = new GcPdfDocument();
  30. var page = doc.NewPage();
  31. var g = page.Graphics;
  32. const float In = 72f, vStep = In / 2;
  33. var ip = new PointF(In, In);
  34.  
  35. // 1. The only mandatory property that must be set on a TextFormat is Font:
  36. var tf = new TextFormat() { Font = StandardFonts.Times };
  37.  
  38. g.DrawString("1. The only mandatory property that must always be set on a TextFormat is Font." +
  39. "Even FontSize is optional, and defaults to 12pts.", tf, ip);
  40. ip.Y += vStep * 2;
  41.  
  42. // 2. Standard font properties are available:
  43. tf.Underline = true;
  44. tf.Strikethrough = true;
  45. tf.FontSize = 10;
  46. g.DrawString("2. Standard properties are available, here we turn Underline and Strikethrough on, and set FontSize to 10.", tf, ip);
  47. ip.Y += vStep;
  48.  
  49. // 3. TextFormat.FontStyle allows emulating bold and/or italic styles
  50. // using a regular font (see also BoldItalicEmulation):
  51. tf.Underline = tf.Strikethrough = false;
  52. tf.FontStyle = GCTEXT.FontStyle.BoldItalic;
  53. tf.FontSize = 12;
  54. g.DrawString("3. Using TextFormat.FontStyle.BoldItalic to emulate bold italic style.", tf, ip);
  55. ip.Y += vStep;
  56.  
  57. // 4. Other properties include foreground and background colors:
  58. tf.FontStyle = GCTEXT.FontStyle.Regular;
  59. tf.ForeColor = Color.DarkSlateBlue;
  60. tf.BackColor = Color.PaleGreen;
  61. g.DrawString("4. Using TextFormat.ForeColor and TextFormat.BackColor to colorize the text.", tf, ip);
  62. ip.Y += vStep;
  63.  
  64. // 5. Different text formats may be mixed in the same paragraph.
  65. // For that, TextLayout and GcPdfGraphics.DrawTextLayout must be used:
  66. TextLayout tl = g.CreateTextLayout();
  67. tl.Append("5. Different text formats can be easily mixed in the same paragraph",
  68. new TextFormat() { Font = StandardFonts.Times });
  69. tl.Append("when the paragraph is built with TextLayout",
  70. new TextFormat() { Font = StandardFonts.TimesBold, BackColor = Color.PaleTurquoise });
  71. tl.Append("as this sample paragraph shows.",
  72. new TextFormat() { Font = StandardFonts.HelveticaBoldItalic, ForeColor = Color.DarkOrange });
  73. tl.Append("Various other options are available on TextFormat, including",
  74. new TextFormat() { Font = StandardFonts.Times, ForeColor = Color.DarkSlateBlue });
  75. tl.Append(" GlyphAdvanceFactor ",
  76. new TextFormat() { Font = StandardFonts.TimesBoldItalic, Underline = true });
  77. tl.Append(" (spreading glyphs out ",
  78. new TextFormat() { Font = StandardFonts.Times, GlyphAdvanceFactor = 1.5f, ForeColor = Color.BlueViolet });
  79. tl.Append("or putting them closer together), ",
  80. new TextFormat() { Font = StandardFonts.Times, GlyphAdvanceFactor = 0.8f, ForeColor = Color.BlueViolet });
  81. tl.Append("TransverseOffset",
  82. new TextFormat() { Font = StandardFonts.TimesBoldItalic, Underline = true });
  83. tl.Append(" (lowering the glyphs below the base line, ",
  84. new TextFormat() { Font = StandardFonts.Times, TransverseOffset = -5, ForeColor = Color.MediumVioletRed });
  85. tl.Append("or raising them above it)",
  86. new TextFormat() { Font = StandardFonts.Times, TransverseOffset = 5, ForeColor = Color.MediumVioletRed });
  87. tl.Append(" and more (for example, specific fonts' features are accessible via TextFormat.FontFeatures).",
  88. new TextFormat() { Font = StandardFonts.Times, FontFeatures = new FontFeature[] { new FontFeature(FeatureTag.clig) } });
  89.  
  90. // For this sample, we just set the max width of the text layout,
  91. // in a real app you would probably set at least the MaxHeight too:
  92. tl.MaxWidth = page.Size.Width - In * 2;
  93. tl.PerformLayout(true);
  94. g.DrawTextLayout(tl, ip);
  95. // Done:
  96. doc.Save(stream);
  97. return doc.Pages.Count;
  98. }
  99. }
  100. }
  101.