//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;
namespace DsWordWeb.Demos
{
// This example shows how to create and use a linked style,
// which can be used as both a paragraph and a character style.
public class LinkedStyle
{
public GcWordDocument CreateDocx()
{
var log = new List<string>();
void Log(string message)
{
log.Add(message);
}
var doc = new GcWordDocument();
// Change the default:
doc.HideLinkedCharacterStyles = false;
// Create a linked style:
var linkedStyle = doc.Styles.AddLinkedStyle("Linked Style", doc.Styles[BuiltInStyleId.Heading1]);
Log($"Linked paragraph style is linked: {linkedStyle.Linked}"); // Linked paragraph style is linked: True
Log($"Linked paragraph style name: {linkedStyle.Name}"); // Linked paragraph style name: Linked Style
Log($"Linked paragraph style type: {linkedStyle.Type}"); // Linked paragraph style type: Paragraph
Log($"Linked character style is linked: {linkedStyle.LinkStyle.Linked}"); // Linked character style is linked: True
Log($"Linked character style name: {linkedStyle.LinkStyle.Name}"); // Linked character style name: Linked Style Char
Log($"Linked character style type: {linkedStyle.LinkStyle.Type}"); // Linked character style type: Character
// Change the linked style's formatting (reflects on both linked styles):
linkedStyle.Font.Bold = true;
Log($"Linked paragraph style is bold: {linkedStyle.Font.Bold}"); // Linked paragraph style is bold: True
Log($"Linked character style is bold: {linkedStyle.LinkStyle.Font.Bold}"); // Linked character style is bold: True
// Apply the linked style to a paragraph:
doc.Body.Paragraphs.Add("This paragraph is formatted with a linked style that is applied to the whole paragraph as a paragraph style.", linkedStyle);
// Apply the linked style to a run:
var paragraph = doc.Body.Paragraphs.Add("In this paragraph, ", doc.Styles[BuiltInStyleId.Normal]);
var run = paragraph.GetRange().Runs.Add("this run is formatted with the same linked style applied to the run as a character style.", linkedStyle);
Log($"HideLinkedCharacterStyles is: {doc.HideLinkedCharacterStyles}"); // HideLinkedCharacterStyles is: False
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
Log($"Run style name: {run.Style.Name}"); // Run style name: Linked Style Char
// Toggle the HideLinkedCharacterStyles property:
doc.HideLinkedCharacterStyles = true;
Log($"HideLinkedCharacterStyles is: {doc.HideLinkedCharacterStyles}"); // HideLinkedCharacterStyles is: True
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
Log($"Run style name: {run.Style.Name}"); // Run style name: Linked Style
doc.Body.Paragraphs.Add("Log (see example code for details):", doc.Styles[BuiltInStyleId.Heading2]);
log.ForEach(m_ => doc.Body.Paragraphs.Add(m_, doc.Styles[BuiltInStyleId.NoSpacing]));
// Done:
return doc;
}
}
}