npoi
535 строк · 15.5 Кб
1using System;2using System.Xml;3using System.Xml.Serialization;4using System.Text;5using System.Collections.Generic;6using System.IO;7using NPOI.OpenXml4Net.Util;8using NPOI.OpenXmlFormats.Vml;9
10namespace NPOI.OpenXmlFormats.Wordprocessing11{
12[XmlInclude(typeof(CT_Picture))]13[XmlInclude(typeof(CT_Object))]14[XmlInclude(typeof(CT_Background))]15
16[Serializable]17[XmlType(Namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main")]18[XmlRoot(Namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", IsNullable = true)]19public class CT_PictureBase20{21private List<object> itemsField;22
23private List<ItemsChoiceType9> itemsElementNameField;24
25public CT_PictureBase()26{27this.itemsElementNameField = new List<ItemsChoiceType9>();28this.itemsField = new List<object>();29}30
31[XmlAnyElement(Namespace = "urn:schemas-microsoft-com:office:office", Order = 0)]32[XmlAnyElement(Namespace = "urn:schemas-microsoft-com:vml", Order = 0)]33[XmlChoiceIdentifier("ItemsElementName")]34public List<object> Items35{36get37{38return this.itemsField;39}40set41{42this.itemsField =value;43}44}45
46[XmlElement("ItemsElementName", Order = 1)]47[XmlIgnore]48public List<ItemsChoiceType9> ItemsElementName49{50get51{52return this.itemsElementNameField;53}54set55{56this.itemsElementNameField = value;57}58}59
60public void Set(object obj)61{62if (obj is CT_Group)63{64var group = (CT_Group)obj;65foreach (var item in group.Items)66{67/*XmlSerializer xmlse = new XmlSerializer(item.GetType());68StringBuilder output = new StringBuilder();
69XmlWriterSettings settings = new XmlWriterSettings();
70
71settings.Encoding = Encoding.UTF8;
72settings.OmitXmlDeclaration = true;
73XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
74ns.Add("v", "urn:schemas-microsoft-com:vml");
75ns.Add("o", "urn:schemas-microsoft-com:office:office");
76XmlWriter writer = XmlWriter.Create(output, settings);
77xmlse.Serialize(writer, item, ns);
78
79XmlDocument xmlDoc = new XmlDocument();
80
81xmlDoc.LoadXml(output.ToString());*/
82
83lock (this)84{85this.itemsField.Add(item);86this.itemsElementNameField.Add(ItemsChoiceType9.vml);87}88}89}90else91{92XmlSerializer xmlse = new XmlSerializer(obj.GetType());93StringBuilder output = new StringBuilder();94XmlWriterSettings settings = new XmlWriterSettings();95
96settings.Encoding = Encoding.UTF8;97settings.OmitXmlDeclaration = true;98XmlSerializerNamespaces ns = new XmlSerializerNamespaces();99ns.Add("v", "urn:schemas-microsoft-com:vml");100ns.Add("o", "urn:schemas-microsoft-com:office:office");101XmlWriter writer = XmlWriter.Create(output, settings);102xmlse.Serialize(writer, obj, ns);103
104XmlDocument xmlDoc = new XmlDocument();105
106xmlDoc.LoadXml(output.ToString());107
108lock (this)109{110this.itemsField.Add(xmlDoc.DocumentElement.CloneNode(true));111this.itemsElementNameField.Add(ItemsChoiceType9.vml);112}113}114}115}116
117[Serializable]118[XmlType(Namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main")]119[XmlRoot(Namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", IsNullable = true)]120public class CT_Picture : CT_PictureBase121{122
123private CT_Rel movieField;124
125private CT_Control controlField;126
127public CT_Picture()128{129//this.controlField = new CT_Control();130//this.movieField = new CT_Rel();131}132
133[XmlElement(Order = 0)]134public CT_Rel movie135{136get137{138return this.movieField;139}140set141{142this.movieField = value;143}144}145
146[XmlElement(Order = 1)]147public CT_Control control148{149get150{151return this.controlField;152}153set154{155this.controlField = value;156}157}158
159// added because they are called in XWPFRun - perhaps another CT_Picture must be used instead160public Dml.Picture.CT_PictureNonVisual AddNewNvPicPr()161{162throw new NotImplementedException();163}164
165public Dml.CT_BlipFillProperties AddNewBlipFill()166{167throw new NotImplementedException();168}169
170public Dml.CT_ShapeProperties AddNewSpPr()171{172throw new NotImplementedException();173}174
175public static CT_Picture Parse(XmlNode node, XmlNamespaceManager namespaceManager)176{177if (node == null)178return null;179CT_Picture ctObj = new CT_Picture();180foreach (XmlNode childNode in node.ChildNodes)181{182if (childNode.LocalName == "movie")183ctObj.movie = CT_Rel.Parse(childNode, namespaceManager);184else if (childNode.LocalName == "control")185ctObj.control = CT_Control.Parse(childNode, namespaceManager);186else if(childNode.Prefix == "o")187{188ctObj.ItemsElementName.Add(ItemsChoiceType9.office);189ctObj.Items.Add(childNode);190}else if(childNode.Prefix=="v")191{192ctObj.ItemsElementName.Add(ItemsChoiceType9.vml);193ctObj.Items.Add(childNode);194}195}196return ctObj;197}198
199
200
201internal void Write(StreamWriter sw, string nodeName)202{203sw.Write(string.Format("<w:{0}>", nodeName));204if (this.movie != null)205this.movie.Write(sw, "movie");206if (this.control != null)207this.control.Write(sw, "control");208foreach (var childnode in Items)209{210if (childnode is XmlNode)211{212sw.Write(((XmlNode)childnode).OuterXml);213}214else if (childnode is CT_Shape)215{216((CT_Shape)childnode).Write(sw, "shape");217}218else if (childnode is CT_Shapetype)219{220((CT_Shapetype)childnode).Write(sw, "shapetype");221}222}223sw.WriteEndW(nodeName);224}225}226
227[Serializable]228
229[XmlType(Namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main")]230[XmlRoot(Namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", IsNullable = true)]231public class CT_Background : CT_PictureBase232{233
234private string colorField;235
236private ST_ThemeColor themeColorField;237
238private bool themeColorFieldSpecified;239
240private byte[] themeTintField;241
242private byte[] themeShadeField;243
244public CT_Background()245{246this.themeColorField = ST_ThemeColor.none;247}248
249public static CT_Background Parse(XmlNode node, XmlNamespaceManager namespaceManager)250{251if (node == null)252return null;253CT_Background ctObj = new CT_Background();254ctObj.color = XmlHelper.ReadString(node.Attributes["w:color"]);255if (node.Attributes["w:themeColor"] != null)256ctObj.themeColor = (ST_ThemeColor)Enum.Parse(typeof(ST_ThemeColor), node.Attributes["w:themeColor"].Value);257ctObj.themeTint = XmlHelper.ReadBytes(node.Attributes["w:themeTint"]);258ctObj.themeShade = XmlHelper.ReadBytes(node.Attributes["w:themeShade"]);259
260return ctObj;261}262
263
264
265internal void Write(StreamWriter sw, string nodeName)266{267sw.Write(string.Format("<w:{0}", nodeName));268if(themeColorField!= ST_ThemeColor.none)269XmlHelper.WriteAttribute(sw, "w:themeColor", this.themeColor.ToString());270XmlHelper.WriteAttribute(sw, "w:themeTint", this.themeTint);271XmlHelper.WriteAttribute(sw, "w:themeShade", this.themeShade);272XmlHelper.WriteAttribute(sw, "w:color", this.color);273sw.Write("/>");274}275
276[XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]277public string color278{279get280{281return this.colorField;282}283set284{285this.colorField = value;286}287}288
289[XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]290public ST_ThemeColor themeColor291{292get293{294return this.themeColorField;295}296set297{298this.themeColorField = value;299}300}301
302[XmlIgnore]303public bool themeColorSpecified304{305get306{307return this.themeColorFieldSpecified;308}309set310{311this.themeColorFieldSpecified = value;312}313}314
315[XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, DataType = "hexBinary")]316public byte[] themeTint317{318get319{320return this.themeTintField;321}322set323{324this.themeTintField = value;325}326}327
328[XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, DataType = "hexBinary")]329public byte[] themeShade330{331get332{333return this.themeShadeField;334}335set336{337this.themeShadeField = value;338}339}340}341
342
343[Serializable]344
345[XmlType(Namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main")]346[XmlRoot(Namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", IsNullable = true)]347public class CT_Object : CT_PictureBase348{349
350private CT_Control controlField;351
352private ulong dxaOrigField;353
354private bool dxaOrigFieldSpecified;355
356private ulong dyaOrigField;357
358private bool dyaOrigFieldSpecified;359
360public CT_Object()361{362//this.controlField = new CT_Control();363}364
365[XmlElement(Order = 0)]366public CT_Control control367{368get369{370return this.controlField;371}372set373{374this.controlField = value;375}376}377
378[XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]379public ulong dxaOrig380{381get382{383return this.dxaOrigField;384}385set386{387this.dxaOrigField = value;388}389}390
391[XmlIgnore]392public bool dxaOrigSpecified393{394get395{396return this.dxaOrigFieldSpecified;397}398set399{400this.dxaOrigFieldSpecified = value;401}402}403
404[XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]405public ulong dyaOrig406{407get408{409return this.dyaOrigField;410}411set412{413this.dyaOrigField = value;414}415}416
417[XmlIgnore]418public bool dyaOrigSpecified419{420get421{422return this.dyaOrigFieldSpecified;423}424set425{426this.dyaOrigFieldSpecified = value;427}428}429public static CT_Object Parse(XmlNode node, XmlNamespaceManager namespaceManager)430{431if (node == null)432return null;433CT_Object ctObj = new CT_Object();434ctObj.dxaOrig = XmlHelper.ReadULong(node.Attributes["w:dxaOrig"]);435ctObj.dyaOrig = XmlHelper.ReadULong(node.Attributes["w:dyaOrig"]);436foreach (XmlNode childNode in node.ChildNodes)437{438if (childNode.LocalName == "control")439ctObj.control = CT_Control.Parse(childNode, namespaceManager);440}441return ctObj;442}443
444
445
446internal void Write(StreamWriter sw, string nodeName)447{448sw.Write(string.Format("<w:{0}", nodeName));449XmlHelper.WriteAttribute(sw, "w:dxaOrig", this.dxaOrig);450XmlHelper.WriteAttribute(sw, "w:dyaOrig", this.dyaOrig);451sw.Write(">");452if (this.control != null)453this.control.Write(sw, "control");454sw.WriteEndW(nodeName);455}456
457}458
459[Serializable]460
461[XmlType(Namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main")]462[XmlRoot(Namespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main", IsNullable = true)]463public class CT_Control464{465public static CT_Control Parse(XmlNode node, XmlNamespaceManager namespaceManager)466{467if (node == null)468return null;469CT_Control ctObj = new CT_Control();470ctObj.name = XmlHelper.ReadString(node.Attributes["w:name"]);471ctObj.shapeid = XmlHelper.ReadString(node.Attributes["w:shapeid"]);472ctObj.id = XmlHelper.ReadString(node.Attributes["r:id"]);473return ctObj;474}475
476
477
478internal void Write(StreamWriter sw, string nodeName)479{480sw.Write(string.Format("<w:{0}", nodeName));481XmlHelper.WriteAttribute(sw, "w:name", this.name);482XmlHelper.WriteAttribute(sw, "w:shapeid", this.shapeid);483XmlHelper.WriteAttribute(sw, "r:id", this.id);484sw.Write(">");485sw.WriteEndW(nodeName);486}487
488
489private string nameField;490
491private string shapeidField;492
493private string idField;494
495[XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]496public string name497{498get499{500return this.nameField;501}502set503{504this.nameField = value;505}506}507
508[XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]509public string shapeid510{511get512{513return this.shapeidField;514}515set516{517this.shapeidField = value;518}519}520
521[XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.openxmlformats.org/officeDocument/2006/relationships")]522public string id523{524get525{526return this.idField;527}528set529{530this.idField = value;531}532}533}534
535}
536