npoi

Форк
0
535 строк · 15.5 Кб
1
using System;
2
using System.Xml;
3
using System.Xml.Serialization;
4
using System.Text;
5
using System.Collections.Generic;
6
using System.IO;
7
using NPOI.OpenXml4Net.Util;
8
using NPOI.OpenXmlFormats.Vml;
9

10
namespace NPOI.OpenXmlFormats.Wordprocessing
11
{
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)]
19
    public class CT_PictureBase
20
    {
21
        private List<object> itemsField;
22

23
        private List<ItemsChoiceType9> itemsElementNameField;
24

25
        public CT_PictureBase()
26
        {
27
            this.itemsElementNameField = new List<ItemsChoiceType9>();
28
            this.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")]
34
        public List<object> Items
35
        {
36
            get
37
            {
38
                return this.itemsField;
39
            }
40
            set
41
            {
42
                this.itemsField =value;
43
            }
44
        }
45

46
        [XmlElement("ItemsElementName", Order = 1)]
47
        [XmlIgnore]
48
        public List<ItemsChoiceType9> ItemsElementName
49
        {
50
            get
51
            {
52
                return this.itemsElementNameField;
53
            }
54
            set
55
            {
56
               this.itemsElementNameField = value;
57
            }
58
        }
59

60
        public void Set(object obj)
61
        {
62
            if (obj is CT_Group)
63
            {
64
                var group = (CT_Group)obj;
65
                foreach (var item in group.Items)
66
                {
67
                    /*XmlSerializer xmlse = new XmlSerializer(item.GetType());
68
                    StringBuilder output = new StringBuilder();
69
                    XmlWriterSettings settings = new XmlWriterSettings();
70

71
                    settings.Encoding = Encoding.UTF8;
72
                    settings.OmitXmlDeclaration = true;
73
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
74
                    ns.Add("v", "urn:schemas-microsoft-com:vml");
75
                    ns.Add("o", "urn:schemas-microsoft-com:office:office");
76
                    XmlWriter writer = XmlWriter.Create(output, settings);
77
                    xmlse.Serialize(writer, item, ns);
78

79
                    XmlDocument xmlDoc = new XmlDocument();
80

81
                    xmlDoc.LoadXml(output.ToString());*/
82

83
                    lock (this)
84
                    {
85
                        this.itemsField.Add(item);
86
                        this.itemsElementNameField.Add(ItemsChoiceType9.vml);
87
                    }
88
                }
89
            }
90
            else
91
            {
92
                XmlSerializer xmlse = new XmlSerializer(obj.GetType());
93
                StringBuilder output = new StringBuilder();
94
                XmlWriterSettings settings = new XmlWriterSettings();
95

96
                settings.Encoding = Encoding.UTF8;
97
                settings.OmitXmlDeclaration = true;
98
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
99
                ns.Add("v", "urn:schemas-microsoft-com:vml");
100
                ns.Add("o", "urn:schemas-microsoft-com:office:office");
101
                XmlWriter writer = XmlWriter.Create(output, settings);
102
                xmlse.Serialize(writer, obj, ns);
103

104
                XmlDocument xmlDoc = new XmlDocument();
105

106
                xmlDoc.LoadXml(output.ToString());
107

108
                lock (this)
109
                {
110
                    this.itemsField.Add(xmlDoc.DocumentElement.CloneNode(true));
111
                    this.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)]
120
    public class CT_Picture : CT_PictureBase
121
    {
122

123
        private CT_Rel movieField;
124

125
        private CT_Control controlField;
126

127
        public CT_Picture()
128
        {
129
            //this.controlField = new CT_Control();
130
            //this.movieField = new CT_Rel();
131
        }
132

133
        [XmlElement(Order = 0)]
134
        public CT_Rel movie
135
        {
136
            get
137
            {
138
                return this.movieField;
139
            }
140
            set
141
            {
142
                this.movieField = value;
143
            }
144
        }
145

146
        [XmlElement(Order = 1)]
147
        public CT_Control control
148
        {
149
            get
150
            {
151
                return this.controlField;
152
            }
153
            set
154
            {
155
                this.controlField = value;
156
            }
157
        }
158

159
        // added because they are called in XWPFRun - perhaps another CT_Picture must be used instead
160
        public Dml.Picture.CT_PictureNonVisual AddNewNvPicPr()
161
        {
162
            throw new NotImplementedException();
163
        }
164

165
        public Dml.CT_BlipFillProperties AddNewBlipFill()
166
        {
167
            throw new NotImplementedException();
168
        }
169

170
        public Dml.CT_ShapeProperties AddNewSpPr()
171
        {
172
            throw new NotImplementedException();
173
        }
174

175
        public static CT_Picture Parse(XmlNode node, XmlNamespaceManager namespaceManager)
176
        {
177
            if (node == null)
178
                return null;
179
            CT_Picture ctObj = new CT_Picture();
180
            foreach (XmlNode childNode in node.ChildNodes)
181
            {
182
                if (childNode.LocalName == "movie")
183
                    ctObj.movie = CT_Rel.Parse(childNode, namespaceManager);
184
                else if (childNode.LocalName == "control")
185
                    ctObj.control = CT_Control.Parse(childNode, namespaceManager);
186
                else if(childNode.Prefix == "o")
187
                { 
188
                    ctObj.ItemsElementName.Add(ItemsChoiceType9.office);
189
                    ctObj.Items.Add(childNode);
190
                }else if(childNode.Prefix=="v")
191
                {
192
                    ctObj.ItemsElementName.Add(ItemsChoiceType9.vml);
193
                    ctObj.Items.Add(childNode);
194
                }
195
            }
196
            return ctObj;
197
        }
198

199

200

201
        internal void Write(StreamWriter sw, string nodeName)
202
        {
203
            sw.Write(string.Format("<w:{0}>", nodeName));
204
            if (this.movie != null)
205
                this.movie.Write(sw, "movie");
206
            if (this.control != null)
207
                this.control.Write(sw, "control");
208
            foreach (var childnode in Items)
209
            {
210
                if (childnode is XmlNode)
211
                {
212
                    sw.Write(((XmlNode)childnode).OuterXml);
213
                }
214
                else if (childnode is CT_Shape)
215
                {
216
                    ((CT_Shape)childnode).Write(sw, "shape");
217
                }
218
                else if (childnode is CT_Shapetype)
219
                {
220
                    ((CT_Shapetype)childnode).Write(sw, "shapetype");
221
                }
222
            }
223
            sw.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)]
231
    public class CT_Background : CT_PictureBase
232
    {
233

234
        private string colorField;
235

236
        private ST_ThemeColor themeColorField;
237

238
        private bool themeColorFieldSpecified;
239

240
        private byte[] themeTintField;
241

242
        private byte[] themeShadeField;
243

244
        public CT_Background()
245
        {
246
            this.themeColorField = ST_ThemeColor.none;
247
        }
248

249
        public static CT_Background Parse(XmlNode node, XmlNamespaceManager namespaceManager)
250
        {
251
            if (node == null)
252
                return null;
253
            CT_Background ctObj = new CT_Background();
254
            ctObj.color = XmlHelper.ReadString(node.Attributes["w:color"]);
255
            if (node.Attributes["w:themeColor"] != null)
256
                ctObj.themeColor = (ST_ThemeColor)Enum.Parse(typeof(ST_ThemeColor), node.Attributes["w:themeColor"].Value);
257
            ctObj.themeTint = XmlHelper.ReadBytes(node.Attributes["w:themeTint"]);
258
            ctObj.themeShade = XmlHelper.ReadBytes(node.Attributes["w:themeShade"]);
259

260
            return ctObj;
261
        }
262

263

264

265
        internal void Write(StreamWriter sw, string nodeName)
266
        {
267
            sw.Write(string.Format("<w:{0}", nodeName));
268
            if(themeColorField!= ST_ThemeColor.none)
269
                XmlHelper.WriteAttribute(sw, "w:themeColor", this.themeColor.ToString());
270
            XmlHelper.WriteAttribute(sw, "w:themeTint", this.themeTint);
271
            XmlHelper.WriteAttribute(sw, "w:themeShade", this.themeShade);
272
            XmlHelper.WriteAttribute(sw, "w:color", this.color);
273
            sw.Write("/>");
274
        }
275

276
        [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
277
        public string color
278
        {
279
            get
280
            {
281
                return this.colorField;
282
            }
283
            set
284
            {
285
                this.colorField = value;
286
            }
287
        }
288

289
        [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
290
        public ST_ThemeColor themeColor
291
        {
292
            get
293
            {
294
                return this.themeColorField;
295
            }
296
            set
297
            {
298
                this.themeColorField = value;
299
            }
300
        }
301

302
        [XmlIgnore]
303
        public bool themeColorSpecified
304
        {
305
            get
306
            {
307
                return this.themeColorFieldSpecified;
308
            }
309
            set
310
            {
311
                this.themeColorFieldSpecified = value;
312
            }
313
        }
314

315
        [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, DataType = "hexBinary")]
316
        public byte[] themeTint
317
        {
318
            get
319
            {
320
                return this.themeTintField;
321
            }
322
            set
323
            {
324
                this.themeTintField = value;
325
            }
326
        }
327

328
        [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, DataType = "hexBinary")]
329
        public byte[] themeShade
330
        {
331
            get
332
            {
333
                return this.themeShadeField;
334
            }
335
            set
336
            {
337
                this.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)]
347
    public class CT_Object : CT_PictureBase
348
    {
349

350
        private CT_Control controlField;
351

352
        private ulong dxaOrigField;
353

354
        private bool dxaOrigFieldSpecified;
355

356
        private ulong dyaOrigField;
357

358
        private bool dyaOrigFieldSpecified;
359

360
        public CT_Object()
361
        {
362
            //this.controlField = new CT_Control();
363
        }
364

365
        [XmlElement(Order = 0)]
366
        public CT_Control control
367
        {
368
            get
369
            {
370
                return this.controlField;
371
            }
372
            set
373
            {
374
                this.controlField = value;
375
            }
376
        }
377

378
        [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
379
        public ulong dxaOrig
380
        {
381
            get
382
            {
383
                return this.dxaOrigField;
384
            }
385
            set
386
            {
387
                this.dxaOrigField = value;
388
            }
389
        }
390

391
        [XmlIgnore]
392
        public bool dxaOrigSpecified
393
        {
394
            get
395
            {
396
                return this.dxaOrigFieldSpecified;
397
            }
398
            set
399
            {
400
                this.dxaOrigFieldSpecified = value;
401
            }
402
        }
403

404
        [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
405
        public ulong dyaOrig
406
        {
407
            get
408
            {
409
                return this.dyaOrigField;
410
            }
411
            set
412
            {
413
                this.dyaOrigField = value;
414
            }
415
        }
416

417
        [XmlIgnore]
418
        public bool dyaOrigSpecified
419
        {
420
            get
421
            {
422
                return this.dyaOrigFieldSpecified;
423
            }
424
            set
425
            {
426
                this.dyaOrigFieldSpecified = value;
427
            }
428
        }
429
        public static CT_Object Parse(XmlNode node, XmlNamespaceManager namespaceManager)
430
        {
431
            if (node == null)
432
                return null;
433
            CT_Object ctObj = new CT_Object();
434
            ctObj.dxaOrig = XmlHelper.ReadULong(node.Attributes["w:dxaOrig"]);
435
            ctObj.dyaOrig = XmlHelper.ReadULong(node.Attributes["w:dyaOrig"]);
436
            foreach (XmlNode childNode in node.ChildNodes)
437
            {
438
                if (childNode.LocalName == "control")
439
                    ctObj.control = CT_Control.Parse(childNode, namespaceManager);
440
            }
441
            return ctObj;
442
        }
443

444

445

446
        internal void Write(StreamWriter sw, string nodeName)
447
        {
448
            sw.Write(string.Format("<w:{0}", nodeName));
449
            XmlHelper.WriteAttribute(sw, "w:dxaOrig", this.dxaOrig);
450
            XmlHelper.WriteAttribute(sw, "w:dyaOrig", this.dyaOrig);
451
            sw.Write(">");
452
            if (this.control != null)
453
                this.control.Write(sw, "control");
454
            sw.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)]
463
    public class CT_Control
464
    {
465
        public static CT_Control Parse(XmlNode node, XmlNamespaceManager namespaceManager)
466
        {
467
            if (node == null)
468
                return null;
469
            CT_Control ctObj = new CT_Control();
470
            ctObj.name = XmlHelper.ReadString(node.Attributes["w:name"]);
471
            ctObj.shapeid = XmlHelper.ReadString(node.Attributes["w:shapeid"]);
472
            ctObj.id = XmlHelper.ReadString(node.Attributes["r:id"]);
473
            return ctObj;
474
        }
475

476

477

478
        internal void Write(StreamWriter sw, string nodeName)
479
        {
480
            sw.Write(string.Format("<w:{0}", nodeName));
481
            XmlHelper.WriteAttribute(sw, "w:name", this.name);
482
            XmlHelper.WriteAttribute(sw, "w:shapeid", this.shapeid);
483
            XmlHelper.WriteAttribute(sw, "r:id", this.id);
484
            sw.Write(">");
485
            sw.WriteEndW(nodeName);
486
        }
487

488

489
        private string nameField;
490

491
        private string shapeidField;
492

493
        private string idField;
494

495
        [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
496
        public string name
497
        {
498
            get
499
            {
500
                return this.nameField;
501
            }
502
            set
503
            {
504
                this.nameField = value;
505
            }
506
        }
507

508
        [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
509
        public string shapeid
510
        {
511
            get
512
            {
513
                return this.shapeidField;
514
            }
515
            set
516
            {
517
                this.shapeidField = value;
518
            }
519
        }
520

521
        [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.openxmlformats.org/officeDocument/2006/relationships")]
522
        public string id
523
        {
524
            get
525
            {
526
                return this.idField;
527
            }
528
            set
529
            {
530
                this.idField = value;
531
            }
532
        }
533
    }
534

535
}
536

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.