LinkedStyle.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 GrapeCity.Documents.Word;
  11.  
  12. namespace DsWordWeb.Demos
  13. {
  14. // This example shows how to create and use a linked style,
  15. // which can be used as both a paragraph and a character style.
  16. public class LinkedStyle
  17. {
  18. public GcWordDocument CreateDocx()
  19. {
  20. var log = new List<string>();
  21. void Log(string message)
  22. {
  23. log.Add(message);
  24. }
  25.  
  26. var doc = new GcWordDocument();
  27. // Change the default:
  28. doc.HideLinkedCharacterStyles = false;
  29.  
  30. // Create a linked style:
  31. var linkedStyle = doc.Styles.AddLinkedStyle("Linked Style", doc.Styles[BuiltInStyleId.Heading1]);
  32. Log($"Linked paragraph style is linked: {linkedStyle.Linked}"); // Linked paragraph style is linked: True
  33. Log($"Linked paragraph style name: {linkedStyle.Name}"); // Linked paragraph style name: Linked Style
  34. Log($"Linked paragraph style type: {linkedStyle.Type}"); // Linked paragraph style type: Paragraph
  35. Log($"Linked character style is linked: {linkedStyle.LinkStyle.Linked}"); // Linked character style is linked: True
  36. Log($"Linked character style name: {linkedStyle.LinkStyle.Name}"); // Linked character style name: Linked Style Char
  37. Log($"Linked character style type: {linkedStyle.LinkStyle.Type}"); // Linked character style type: Character
  38.  
  39. // Change the linked style's formatting (reflects on both linked styles):
  40. linkedStyle.Font.Bold = true;
  41. Log($"Linked paragraph style is bold: {linkedStyle.Font.Bold}"); // Linked paragraph style is bold: True
  42. Log($"Linked character style is bold: {linkedStyle.LinkStyle.Font.Bold}"); // Linked character style is bold: True
  43.  
  44. // Apply the linked style to a paragraph:
  45. doc.Body.Paragraphs.Add("This paragraph is formatted with a linked style that is applied to the whole paragraph as a paragraph style.", linkedStyle);
  46.  
  47. // Apply the linked style to a run:
  48. var paragraph = doc.Body.Paragraphs.Add("In this paragraph, ", doc.Styles[BuiltInStyleId.Normal]);
  49. var run = paragraph.GetRange().Runs.Add("this run is formatted with the same linked style applied to the run as a character style.", linkedStyle);
  50.  
  51. Log($"HideLinkedCharacterStyles is: {doc.HideLinkedCharacterStyles}"); // HideLinkedCharacterStyles is: False
  52. Log($"Styles collection has character linked styles: {doc.Styles.FirstOrDefault(x => x.Linked && x.Type == StyleType.Character) != null}"); // Style collection has character linked styles: True
  53. Log($"Run style name: {run.Style.Name}"); // Run style name: Linked Style Char
  54.  
  55. // Toggle the HideLinkedCharacterStyles property:
  56. doc.HideLinkedCharacterStyles = true;
  57. Log($"HideLinkedCharacterStyles is: {doc.HideLinkedCharacterStyles}"); // HideLinkedCharacterStyles is: True
  58. Log($"Styles collection has character linked styles: {doc.Styles.FirstOrDefault(x => x.Linked && x.Type == StyleType.Character) != null}"); // Style collection has character linked styles: False
  59. Log($"Run style name: {run.Style.Name}"); // Run style name: Linked Style
  60.  
  61. doc.Body.Paragraphs.Add("Log (see example code for details):", doc.Styles[BuiltInStyleId.Heading2]);
  62. log.ForEach(m_ => doc.Body.Paragraphs.Add(m_, doc.Styles[BuiltInStyleId.NoSpacing]));
  63.  
  64. // Done:
  65. return doc;
  66. }
  67. }
  68. }
  69.