26 public class ThemeReader : IDocumentReader
29 private Stream stream;
35 public virtual string DocumentType {
get {
return @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme"; } }
68 public void Init(Stream stream,
Workbook workbook, IOptions readerOptions, Action<Stream, Workbook, string, IOptions, int?> inlinePluginHandler)
71 this.Workbook = workbook;
72 this.Options = readerOptions;
73 this.InlinePluginHandler = inlinePluginHandler;
86 ColorScheme colorScheme =
null;
87 using (XmlReader reader = XmlReader.Create(stream, XmlStreamUtils.CreateSettings()))
91 if (reader.NodeType != XmlNodeType.Element)
95 if (XmlStreamUtils.IsElement(reader,
"theme") && colorScheme ==
null)
97 string themeName = reader.GetAttribute(
"name");
98 Workbook.WorkbookTheme =
new Theme(themeName);
99 colorScheme =
new ColorScheme();
100 Workbook.WorkbookTheme.Colors = colorScheme;
102 else if (XmlStreamUtils.IsElement(reader,
"clrScheme") && colorScheme !=
null)
104 ReadClrScheme(reader, colorScheme);
113 throw new IOException(
"The XML entry could not be read from the input stream. Please see the inner exception:", ex);
121 private static void ReadClrScheme(XmlReader reader, ColorScheme colorScheme)
123 colorScheme.Name = reader.GetAttribute(
"name") ??
string.Empty;
124 using (XmlReader subtree = reader.ReadSubtree())
127 while (subtree.Read())
129 if (subtree.NodeType != XmlNodeType.Element)
133 string slot = subtree.LocalName;
134 IColor color = ReadColorEntry(subtree);
138 colorScheme.Dark1 = color;
141 colorScheme.Light1 = color;
144 colorScheme.Dark2 = color;
147 colorScheme.Light2 = color;
150 colorScheme.Accent1 = color;
153 colorScheme.Accent2 = color;
156 colorScheme.Accent3 = color;
159 colorScheme.Accent4 = color;
162 colorScheme.Accent5 = color;
165 colorScheme.Accent6 = color;
168 colorScheme.Hyperlink = color;
171 colorScheme.FollowedHyperlink = color;
183 private static IColor ReadColorEntry(XmlReader reader)
185 if (reader.IsEmptyElement)
189 int slotDepth = reader.Depth;
190 while (reader.Read())
192 if (reader.Depth == slotDepth)
196 if (reader.NodeType == XmlNodeType.Element)
198 if (reader.LocalName.Equals(
"sysClr", StringComparison.OrdinalIgnoreCase))
200 string val = reader.GetAttribute(
"val");
201 SystemColor systemColor =
new SystemColor
203 ColorValue = ParseSystemColor(val)
205 string lastColor = reader.GetAttribute(
"lastClr");
206 if (lastColor !=
null)
208 systemColor.LastColor = lastColor;
213 else if (reader.LocalName.Equals(
"srgbClr", StringComparison.OrdinalIgnoreCase))
215 SrgbColor color =
new SrgbColor
217 ColorValue = reader.GetAttribute(
"val")
233 private static SystemColor.Value ParseSystemColor(
string value)
235 if (
string.IsNullOrEmpty(value))
237 throw new IOException(
"The system color entry was null or empty");
241 return SystemColor.MapStringToValue(value);
245 throw new IOException(
"The system color entry '" + value +
"' could not be parsed", ex);
void Init(Stream stream, Workbook workbook, IOptions readerOptions, Action< Stream, Workbook, string, IOptions, int?> inlinePluginHandler)
Initialization method (interface implementation).
Action< Stream, Workbook, string, IOptions, int?> InlinePluginHandler
Reference to the ReaderPlugInHandler, to be used for post operations in the Execute method.