Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2913_MatlabScriptProblemInstanceProvider/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Drawing/Vml/ExcelVmlDrawingPictureCollection.cs

Last change on this file was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 6.7 KB
Line 
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 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Text;
35using System.Xml;
36using System.Collections;
37using System.Globalization;
38using OfficeOpenXml.Utils;
39
40namespace OfficeOpenXml.Drawing.Vml
41{
42    public class ExcelVmlDrawingPictureCollection : ExcelVmlDrawingBaseCollection, IEnumerable
43    {
44        internal List<ExcelVmlDrawingPicture> _images;
45        ExcelPackage _pck;
46        ExcelWorksheet _ws;
47        internal ExcelVmlDrawingPictureCollection(ExcelPackage pck, ExcelWorksheet ws, Uri uri) :
48            base(pck, ws, uri)
49        {           
50            _pck = pck;
51            _ws = ws;
52            if (uri == null)
53            {
54                VmlDrawingXml.LoadXml(CreateVmlDrawings());
55                _images = new List<ExcelVmlDrawingPicture>();
56            }
57            else
58            {
59                AddDrawingsFromXml();
60            }
61        }
62
63        private void AddDrawingsFromXml()
64        {
65            var nodes = VmlDrawingXml.SelectNodes("//v:shape", NameSpaceManager);
66            _images = new List<ExcelVmlDrawingPicture>();
67            foreach (XmlNode node in nodes)
68            {
69                var img = new ExcelVmlDrawingPicture(node, NameSpaceManager, _ws);
70                var rel = Part.GetRelationship(img.RelId);
71                img.ImageUri = UriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri);
72                _images.Add(img);
73            }
74        }
75
76        private string CreateVmlDrawings()
77        {
78            string vml=string.Format("<xml xmlns:v=\"{0}\" xmlns:o=\"{1}\" xmlns:x=\"{2}\">",
79                ExcelPackage.schemaMicrosoftVml,
80                ExcelPackage.schemaMicrosoftOffice,
81                ExcelPackage.schemaMicrosoftExcel);
82           
83             vml+="<o:shapelayout v:ext=\"edit\">";
84             vml+="<o:idmap v:ext=\"edit\" data=\"1\"/>";
85             vml+="</o:shapelayout>";
86
87             vml+="<v:shapetype id=\"_x0000_t202\" coordsize=\"21600,21600\" o:spt=\"202\" path=\"m,l,21600r21600,l21600,xe\">";
88             vml+="<v:stroke joinstyle=\"miter\" />";
89             vml+="<v:path gradientshapeok=\"t\" o:connecttype=\"rect\" />";
90             vml+="</v:shapetype>";
91             vml+= "</xml>";
92
93            return vml;
94        }
95        internal ExcelVmlDrawingPicture Add(string id, Uri uri, string name, double width, double height)
96        {
97            XmlNode node = AddImage(id, uri, name, width, height);
98            var draw = new ExcelVmlDrawingPicture(node, NameSpaceManager, _ws);
99            draw.ImageUri = uri;
100            _images.Add(draw);
101            return draw;
102        }
103        private XmlNode AddImage(string id, Uri targeUri, string Name, double width, double height)
104        {
105            var node = VmlDrawingXml.CreateElement("v", "shape", ExcelPackage.schemaMicrosoftVml);
106            VmlDrawingXml.DocumentElement.AppendChild(node);
107            node.SetAttribute("id", id);
108            node.SetAttribute("o:type", "#_x0000_t75");
109            node.SetAttribute("style", string.Format("position:absolute;margin-left:0;margin-top:0;width:{0}pt;height:{1}pt;z-index:1", width.ToString(CultureInfo.InvariantCulture), height.ToString(CultureInfo.InvariantCulture)));
110            //node.SetAttribute("fillcolor", "#ffffe1");
111            //node.SetAttribute("insetmode", ExcelPackage.schemaMicrosoftOffice, "auto");
112
113            node.InnerXml = string.Format("<v:imagedata o:relid=\"\" o:title=\"{0}\"/><o:lock v:ext=\"edit\" rotation=\"t\"/>",  Name);
114            return node;
115        }
116        /// <summary>
117        /// Indexer
118        /// </summary>
119        /// <param name="Index">Index</param>
120        /// <returns>The VML Drawing Picture object</returns>
121        public ExcelVmlDrawingPicture this[int Index]
122        {
123            get
124            {
125                return _images[Index] as ExcelVmlDrawingPicture;
126            }
127        }
128        public int Count
129        {
130            get
131            {
132                return _images.Count;
133            }
134        }
135
136        int _nextID = 0;
137        /// <summary>
138        /// returns the next drawing id.
139        /// </summary>
140        /// <returns></returns>
141        internal string GetNewId()
142        {
143            if (_nextID == 0)
144            {
145                foreach (ExcelVmlDrawingComment draw in this)
146                {
147                    if (draw.Id.Length > 3 && draw.Id.StartsWith("vml"))
148                    {
149                        int id;
150                        if (int.TryParse(draw.Id.Substring(3, draw.Id.Length - 3), out id))
151                        {
152                            if (id > _nextID)
153                            {
154                                _nextID = id;
155                            }
156                        }
157                    }
158                }
159            }
160            _nextID++;
161            return "vml" + _nextID.ToString();
162        }
163        #region IEnumerable Members
164
165        IEnumerator IEnumerable.GetEnumerator()
166        {
167            return _images.GetEnumerator();
168        }
169
170        #endregion
171    }
172}
Note: See TracBrowser for help on using the repository browser.