SubSuperScript.cs
  1. //
  2. // This code is part of Document Solutions for Word 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 System.Linq;
  10. using System.Text.RegularExpressions;
  11. using GrapeCity.Documents.Word;
  12.  
  13. namespace DsWordWeb.Demos
  14. {
  15. // This sample shows how to render subscript and superscript text.
  16. public class SubSuperScript
  17. {
  18. public GcWordDocument CreateDocx()
  19. {
  20. var doc = new GcWordDocument();
  21. var sec = doc.Body.Sections.First;
  22. var para = sec.GetRange().Paragraphs.Add();
  23.  
  24. // Get a random 'lorem ipsum' paragraph:
  25. var text = Util.LoremIpsum(1, 18, 20, 20, 20)[0];
  26.  
  27. // Split the paragraph into 'lorem', 'ipsum' and everything else:
  28. const string sub = "lorem";
  29. const string super = "ipsum";
  30. var frags = Regex.Split(text, $"({sub})|({super})");
  31.  
  32. // Create subscript and superscript styles:
  33. var sSub = doc.Styles.Add("My Subscript", StyleType.Character);
  34. sSub.Font.VerticalPosition = VerticalTextPosition.Subscript;
  35. var sSup = doc.Styles.Add("My Superscript", StyleType.Character);
  36. sSup.Font.VerticalPosition = VerticalTextPosition.Superscript;
  37.  
  38. // Add text to a TextLayout using special formats for 'lorem' and 'ipsum':
  39. foreach (var frag in frags)
  40. {
  41. if (frag == sub)
  42. para.GetRange().Runs.Add(frag, sSub);
  43. else if (frag == super)
  44. para.GetRange().Runs.Add(frag, sSup);
  45. else
  46. para.GetRange().Runs.Add(frag);
  47. }
  48.  
  49. // Done:
  50. return doc;
  51. }
  52. }
  53. }
  54.