CharacterStyles.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 GrapeCity.Documents.Word;
  9. using Range = GrapeCity.Documents.Word.Range;
  10.  
  11. namespace DsWordWeb.Demos
  12. {
  13. // This sample demonstrates how to create, specify and apply
  14. // character styles.
  15. // It creates a paragraph of text, splits it into two runs,
  16. // then creates and applies two different character styles
  17. // to the two runs.
  18. // The part creating and splitting the paragraph is based
  19. // on the CharacterFormatting sample.
  20. public class CharacterStyles
  21. {
  22. public GcWordDocument CreateDocx()
  23. {
  24. // Get a sample paragraph of text:
  25. var lorem = Util.LoremIpsumPar();
  26. // The code below is similar to FormatChars code that creates and splits a paragraph:
  27. GcWordDocument doc = new GcWordDocument();
  28. Paragraph p = doc.Body.Paragraphs.Add(lorem);
  29. Range r = p.GetRange();
  30. Run run = r.Runs.First;
  31. Text text = run.GetRange().Texts.First;
  32. Text tIpsum = text.Split(lorem.Length / 2);
  33. Run rIpsum = run.Split(tIpsum, InsertLocation.Before);
  34. // We now have two runs, create and apply different styles to each:
  35. //
  36. // Create a new char style "Lorem" for the first half:
  37. Style sLorem = doc.Styles.Add("Lorem", StyleType.Character);
  38. sLorem.Font.Name = "Times New Roman";
  39. sLorem.Font.Size = 16;
  40. sLorem.Font.Bold = true;
  41. sLorem.Font.Italic = true;
  42. sLorem.Font.Underline = Underline.Thick;
  43. // 'text' was split into 2 halves, but the head can be still
  44. // accessed via the 'text' variable. We set its style to the
  45. // newly created style (the style of the 2nd half remains default):
  46. text.ParentRun.Style = sLorem;
  47. // Create a new char style "Ipsum" for the 2nd half:
  48. Style sIpsum = doc.Styles.Add("Ipsum", StyleType.Character);
  49. sIpsum.Font.Name = "Gabriola";
  50. sIpsum.Font.Size = 18;
  51. sIpsum.Font.Color.RGB = Color.BlueViolet;
  52. rIpsum.Style = sIpsum;
  53. // Done:
  54. return doc;
  55. }
  56. }
  57. }
  58.