1 | /*******************************************************************************
|
---|
2 | * You may amend and distribute as you like, but don't remove this header!
|
---|
3 | *
|
---|
4 | * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
|
---|
5 | * See http://www.codeplex.com/EPPlus for details.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2011 Jan Källman
|
---|
8 | *
|
---|
9 | * This library is free software; you can redistribute it and/or
|
---|
10 | * modify it under the terms of the GNU Lesser General Public
|
---|
11 | * License as published by the Free Software Foundation; either
|
---|
12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
13 |
|
---|
14 | * This library is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
17 | * See the GNU Lesser General Public License for more details.
|
---|
18 | *
|
---|
19 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
20 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
21 | *
|
---|
22 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
23 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
24 | *
|
---|
25 | * Code change notes:
|
---|
26 | *
|
---|
27 | * Author Change Date
|
---|
28 | * ******************************************************************************
|
---|
29 | * Jan Källman Initial Release 2010-06-01
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-16
|
---|
31 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Text;
|
---|
35 | using System.Xml;
|
---|
36 | using System.Globalization;
|
---|
37 | using System.Drawing;
|
---|
38 | using System.IO.Packaging;
|
---|
39 |
|
---|
40 | namespace OfficeOpenXml.Drawing.Vml
|
---|
41 | {
|
---|
42 | /// <summary>
|
---|
43 | /// Drawing object used for header and footer pictures
|
---|
44 | /// </summary>
|
---|
45 | public class ExcelVmlDrawingPicture : ExcelVmlDrawingBase
|
---|
46 | {
|
---|
47 | ExcelWorksheet _worksheet;
|
---|
48 | internal ExcelVmlDrawingPicture(XmlNode topNode, XmlNamespaceManager ns, ExcelWorksheet ws) :
|
---|
49 | base(topNode, ns)
|
---|
50 | {
|
---|
51 | _worksheet = ws;
|
---|
52 | }
|
---|
53 | /// <summary>
|
---|
54 | /// Position ID
|
---|
55 | /// </summary>
|
---|
56 | public string Position
|
---|
57 | {
|
---|
58 | get
|
---|
59 | {
|
---|
60 | return GetXmlNodeString("@id");
|
---|
61 | }
|
---|
62 | }
|
---|
63 | /// <summary>
|
---|
64 | /// The width in points
|
---|
65 | /// </summary>
|
---|
66 | public double Width
|
---|
67 | {
|
---|
68 | get
|
---|
69 | {
|
---|
70 | return GetStyleProp("width");
|
---|
71 | }
|
---|
72 | set
|
---|
73 | {
|
---|
74 | SetStyleProp("width",value.ToString(CultureInfo.InvariantCulture) + "pt");
|
---|
75 | }
|
---|
76 | }
|
---|
77 | /// <summary>
|
---|
78 | /// The height in points
|
---|
79 | /// </summary>
|
---|
80 | public double Height
|
---|
81 | {
|
---|
82 | get
|
---|
83 | {
|
---|
84 | return GetStyleProp("height");
|
---|
85 | }
|
---|
86 | set
|
---|
87 | {
|
---|
88 | SetStyleProp("height", value.ToString(CultureInfo.InvariantCulture) + "pt");
|
---|
89 | }
|
---|
90 | }
|
---|
91 | /// <summary>
|
---|
92 | /// Margin Left in points
|
---|
93 | /// </summary>
|
---|
94 | public double Left
|
---|
95 | {
|
---|
96 | get
|
---|
97 | {
|
---|
98 | return GetStyleProp("left");
|
---|
99 | }
|
---|
100 | set
|
---|
101 | {
|
---|
102 | SetStyleProp("left", value.ToString(CultureInfo.InvariantCulture));
|
---|
103 | }
|
---|
104 | }
|
---|
105 | /// <summary>
|
---|
106 | /// Margin top in points
|
---|
107 | /// </summary>
|
---|
108 | public double Top
|
---|
109 | {
|
---|
110 | get
|
---|
111 | {
|
---|
112 | return GetStyleProp("top");
|
---|
113 | }
|
---|
114 | set
|
---|
115 | {
|
---|
116 | SetStyleProp("top", value.ToString(CultureInfo.InvariantCulture));
|
---|
117 | }
|
---|
118 | }
|
---|
119 | /// <summary>
|
---|
120 | /// The Title of the image
|
---|
121 | /// </summary>
|
---|
122 | public string Title
|
---|
123 | {
|
---|
124 | get
|
---|
125 | {
|
---|
126 | return GetXmlNodeString("v:imagedata/@o:title");
|
---|
127 | }
|
---|
128 | set
|
---|
129 | {
|
---|
130 | SetXmlNodeString("v:imagedata/@o:title",value);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | /// <summary>
|
---|
134 | /// The image
|
---|
135 | /// </summary>
|
---|
136 | public Image Image
|
---|
137 | {
|
---|
138 | get
|
---|
139 | {
|
---|
140 | var pck = _worksheet._package.Package;
|
---|
141 | if (pck.PartExists(ImageUri))
|
---|
142 | {
|
---|
143 | var part = pck.GetPart(ImageUri);
|
---|
144 | return Image.FromStream(part.GetStream());
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | return null;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 | internal Uri ImageUri
|
---|
153 | {
|
---|
154 | get;
|
---|
155 | set;
|
---|
156 | }
|
---|
157 | internal string RelId
|
---|
158 | {
|
---|
159 | get
|
---|
160 | {
|
---|
161 | return GetXmlNodeString("v:imagedata/@o:relid");
|
---|
162 | }
|
---|
163 | set
|
---|
164 | {
|
---|
165 | SetXmlNodeString("v:imagedata/@o:relid",value);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | /// <summary>
|
---|
169 | /// Determines whether an image will be displayed in black and white
|
---|
170 | /// </summary>
|
---|
171 | public bool BiLevel
|
---|
172 | {
|
---|
173 | get
|
---|
174 | {
|
---|
175 | return GetXmlNodeString("v:imagedata/@bilevel")=="t";
|
---|
176 | }
|
---|
177 | set
|
---|
178 | {
|
---|
179 | if (value)
|
---|
180 | {
|
---|
181 | SetXmlNodeString("v:imagedata/@bilevel", "t");
|
---|
182 | }
|
---|
183 | else
|
---|
184 | {
|
---|
185 | DeleteNode("v:imagedata/@bilevel");
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | /// <summary>
|
---|
190 | /// Determines whether a picture will be displayed in grayscale mode
|
---|
191 | /// </summary>
|
---|
192 | public bool GrayScale
|
---|
193 | {
|
---|
194 | get
|
---|
195 | {
|
---|
196 | return GetXmlNodeString("v:imagedata/@grayscale")=="t";
|
---|
197 | }
|
---|
198 | set
|
---|
199 | {
|
---|
200 | if (value)
|
---|
201 | {
|
---|
202 | SetXmlNodeString("v:imagedata/@grayscale", "t");
|
---|
203 | }
|
---|
204 | else
|
---|
205 | {
|
---|
206 | DeleteNode("v:imagedata/@grayscale");
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 | /// <summary>
|
---|
211 | /// Defines the intensity of all colors in an image
|
---|
212 | /// Default value is 1
|
---|
213 | /// </summary>
|
---|
214 | public double Gain
|
---|
215 | {
|
---|
216 | get
|
---|
217 | {
|
---|
218 | string v = GetXmlNodeString("v:imagedata/@gain");
|
---|
219 | return GetFracDT(v,1);
|
---|
220 | }
|
---|
221 | set
|
---|
222 | {
|
---|
223 | if (value < 0)
|
---|
224 | {
|
---|
225 | throw (new ArgumentOutOfRangeException("Value must be positive"));
|
---|
226 | }
|
---|
227 | if (value == 1)
|
---|
228 | {
|
---|
229 | DeleteNode("v:imagedata/@gamma");
|
---|
230 | }
|
---|
231 | else
|
---|
232 | {
|
---|
233 | SetXmlNodeString("v:imagedata/@gain", value.ToString("#.0#", CultureInfo.InvariantCulture));
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|
237 | /// <summary>
|
---|
238 | /// Defines the amount of contrast for an image
|
---|
239 | /// Default value is 0;
|
---|
240 | /// </summary>
|
---|
241 | public double Gamma
|
---|
242 | {
|
---|
243 | get
|
---|
244 | {
|
---|
245 | string v = GetXmlNodeString("v:imagedata/@gamma");
|
---|
246 | return GetFracDT(v,0);
|
---|
247 | }
|
---|
248 | set
|
---|
249 | {
|
---|
250 | if (value == 0) //Default
|
---|
251 | {
|
---|
252 | DeleteNode("v:imagedata/@gamma");
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | SetXmlNodeString("v:imagedata/@gamma", value.ToString("#.0#", CultureInfo.InvariantCulture));
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 | /// <summary>
|
---|
261 | /// Defines the intensity of black in an image
|
---|
262 | /// Default value is 0
|
---|
263 | /// </summary>
|
---|
264 | public double BlackLevel
|
---|
265 | {
|
---|
266 | get
|
---|
267 | {
|
---|
268 | string v = GetXmlNodeString("v:imagedata/@blacklevel");
|
---|
269 | return GetFracDT(v, 0);
|
---|
270 | }
|
---|
271 | set
|
---|
272 | {
|
---|
273 | if (value == 0)
|
---|
274 | {
|
---|
275 | DeleteNode("v:imagedata/@blacklevel");
|
---|
276 | }
|
---|
277 | else
|
---|
278 | {
|
---|
279 | SetXmlNodeString("v:imagedata/@blacklevel", value.ToString("#.0#", CultureInfo.InvariantCulture));
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | #region Private Methods
|
---|
285 | private double GetFracDT(string v, double def)
|
---|
286 | {
|
---|
287 | double d;
|
---|
288 | if (v.EndsWith("f"))
|
---|
289 | {
|
---|
290 | v = v.Substring(0, v.Length - 1);
|
---|
291 | if (double.TryParse(v, out d))
|
---|
292 | {
|
---|
293 | d /= 65535;
|
---|
294 | }
|
---|
295 | else
|
---|
296 | {
|
---|
297 | d = def;
|
---|
298 | }
|
---|
299 | }
|
---|
300 | else
|
---|
301 | {
|
---|
302 | if (!double.TryParse(v, out d))
|
---|
303 | {
|
---|
304 | d = def;
|
---|
305 | }
|
---|
306 | }
|
---|
307 | return d;
|
---|
308 | }
|
---|
309 | private void SetStyleProp(string propertyName, string value)
|
---|
310 | {
|
---|
311 | string style = GetXmlNodeString("@style");
|
---|
312 | string newStyle = "";
|
---|
313 | bool found = false;
|
---|
314 | foreach (string prop in style.Split(';'))
|
---|
315 | {
|
---|
316 | string[] split = prop.Split(':');
|
---|
317 | if (split[0] == propertyName)
|
---|
318 | {
|
---|
319 | newStyle += propertyName + ":" + value + ";";
|
---|
320 | found = true;
|
---|
321 | }
|
---|
322 | else
|
---|
323 | {
|
---|
324 | newStyle += prop + ";";
|
---|
325 | }
|
---|
326 | }
|
---|
327 | if (!found)
|
---|
328 | {
|
---|
329 | newStyle += propertyName + ":" + value + ";";
|
---|
330 | }
|
---|
331 | SetXmlNodeString("@style", newStyle.Substring(0, newStyle.Length - 1));
|
---|
332 | }
|
---|
333 | private double GetStyleProp(string propertyName)
|
---|
334 | {
|
---|
335 | string style = GetXmlNodeString("@style");
|
---|
336 | foreach (string prop in style.Split(';'))
|
---|
337 | {
|
---|
338 | string[] split = prop.Split(':');
|
---|
339 | if (split[0] == propertyName && split.Length > 1)
|
---|
340 | {
|
---|
341 | string value = split[1].EndsWith("pt") ? split[1].Substring(0, split[1].Length - 2) : split[1];
|
---|
342 | double ret;
|
---|
343 | if (double.TryParse(value, out ret))
|
---|
344 | {
|
---|
345 | return ret;
|
---|
346 | }
|
---|
347 | else
|
---|
348 | {
|
---|
349 | return 0;
|
---|
350 | }
|
---|
351 | }
|
---|
352 | }
|
---|
353 | return 0;
|
---|
354 | }
|
---|
355 | #endregion
|
---|
356 | }
|
---|
357 | }
|
---|