/
PresentationDocument.cs
822 lines (709 loc) · 35.8 KB
/
PresentationDocument.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using DocumentFormat.OpenXml.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
using System.Reflection;
namespace DocumentFormat.OpenXml.Packaging
{
/// <summary>
/// Defines PresentationDocument - an OpenXmlPackage represents a Presentation document
/// </summary>
[PartConstraint(typeof(PresentationPart), true, false)]
[PartConstraint(typeof(CoreFilePropertiesPart), false, false)]
[PartConstraint(typeof(ExtendedFilePropertiesPart), false, false)]
[PartConstraint(typeof(CustomFilePropertiesPart), false, false)]
[PartConstraint(typeof(ThumbnailPart), false, false)]
[PartConstraint(typeof(DigitalSignatureOriginPart), false, false)]
[PartConstraint(typeof(QuickAccessToolbarCustomizationsPart), false, false)]
[PartConstraint(typeof(RibbonExtensibilityPart), false, false)]
[PartConstraint(typeof(RibbonAndBackstageCustomizationsPart), false, false)]
[PartConstraint(typeof(WebExTaskpanesPart), false, false)]
public partial class PresentationDocument : OpenXmlPackage
{
/// <summary>
/// Gets the relationship type of the main part.
/// </summary>
internal sealed override string MainPartRelationshipType => PresentationPart.RelationshipTypeConstant;
private static Dictionary<PresentationDocumentType, string> _validMainPartContentType;
private static Dictionary<PresentationDocumentType, string> MainPartContentTypes
{
get
{
if (_validMainPartContentType == null)
{
_validMainPartContentType = new Dictionary<PresentationDocumentType, string>
{
{ PresentationDocumentType.Presentation, @"application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml" },
{ PresentationDocumentType.Template, @"application/vnd.openxmlformats-officedocument.presentationml.template.main+xml" },
{ PresentationDocumentType.Slideshow, @"application/vnd.openxmlformats-officedocument.presentationml.slideshow.main+xml" },
{ PresentationDocumentType.MacroEnabledPresentation, @"application/vnd.ms-powerpoint.presentation.macroEnabled.main+xml" },
{ PresentationDocumentType.MacroEnabledTemplate, @"application/vnd.ms-powerpoint.template.macroEnabled.main+xml" },
{ PresentationDocumentType.MacroEnabledSlideshow, @"application/vnd.ms-powerpoint.slideshow.macroEnabled.main+xml" },
{ PresentationDocumentType.AddIn, @"application/vnd.ms-powerpoint.addin.macroEnabled.main+xml" },
};
}
return _validMainPartContentType;
}
}
/// <summary>
/// Gets the list of valid content type for main part.
/// </summary>
internal sealed override ICollection<string> ValidMainPartContentTypes => MainPartContentTypes.Values;
/// <summary>
/// Creates a PresentationDocument.
/// </summary>
protected PresentationDocument()
: base()
{
}
private PresentationDocumentType _documentType;
/// <summary>
/// Gets the type of the PresentationDocument.
/// </summary>
public PresentationDocumentType DocumentType
{
get
{
ThrowIfObjectDisposed();
return _documentType;
}
private set
{
ThrowIfObjectDisposed();
_documentType = value;
}
}
internal override ApplicationType ApplicationType => ApplicationType.PowerPoint;
private void UpdateDocumentTypeFromContentType()
{
if (MainPartContentType == null)
{
throw new InvalidOperationException();
}
foreach (KeyValuePair<PresentationDocumentType, string> types in MainPartContentTypes)
{
if (types.Value == MainPartContentType)
{
DocumentType = types.Key;
}
}
}
/// <summary>
/// Creates a new instance of the PresentationDocument class from the specified file.
/// </summary>
/// <param name="path">The path and file name of the target PresentationDocument.</param>
/// <param name="type">The type of the PresentationDocument.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when "path" is null reference.</exception>
public static PresentationDocument Create(string path, PresentationDocumentType type)
{
return Create(path, type, true);
}
/// <summary>
/// Created a new instance of the PresentationDocument class from the IO stream.
/// </summary>
/// <param name="stream">The IO stream on which to create the PresentationDocument.</param>
/// <param name="type">The type of the PresentationDocument.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when "stream" is null reference.</exception>
/// <exception cref="IOException">Thrown when "stream" is not opened with Write access.</exception>
public static PresentationDocument Create(Stream stream, PresentationDocumentType type)
{
return Create(stream, type, true);
}
/// <summary>
/// Created a new instance of the PresentationDocument class from the specified package.
/// </summary>
/// <param name="package">The specified OpenXml package.</param>
/// <param name="type">The type of the PresentationDocument.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when "package" is null reference.</exception>
/// <exception cref="IOException">Thrown when "package" is not opened with Write access.</exception>
public static PresentationDocument Create(Package package, PresentationDocumentType type)
{
return Create(package, type, true);
}
/// <summary>
/// Created a new instance of the PresentationDocument class from the specified file.
/// </summary>
/// <param name="path">The path and file name of the target PresentationDocument.</param>
/// <param name="type">The type of the PresentationDocument.</param>
/// <param name="autoSave">Whether to auto save the created document.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when "path" is null reference.</exception>
public static PresentationDocument Create(string path, PresentationDocumentType type, bool autoSave)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(path);
}
var doc = new PresentationDocument
{
DocumentType = type,
OpenSettings = new OpenSettings { AutoSave = autoSave },
MainPartContentType = MainPartContentTypes[type],
};
doc.CreateCore(path);
return doc;
}
/// <summary>
/// Creates a new instance of the PresentationDocument class from the IO stream.
/// </summary>
/// <param name="stream">The IO stream on which to create the PresentationDocument.</param>
/// <param name="type">The type of the PresentationDocument.</param>
/// <param name="autoSave">Whether to auto save the created document.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when "stream" is null reference.</exception>
/// <exception cref="IOException">Thrown when "stream" is not opened with Write access.</exception>
public static PresentationDocument Create(Stream stream, PresentationDocumentType type, bool autoSave)
{
var doc = new PresentationDocument
{
DocumentType = type,
OpenSettings = new OpenSettings { AutoSave = autoSave },
MainPartContentType = MainPartContentTypes[type],
};
doc.CreateCore(stream);
return doc;
}
/// <summary>
/// Creates a new instance of the PresentationDocument class from the specified package.
/// </summary>
/// <param name="package">The specified OpenXml package.</param>
/// <param name="type">The type of the PresentationDocument.</param>
/// <param name="autoSave">Whether to auto save the created document.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when "package" is null reference.</exception>
/// <exception cref="IOException">Thrown when "package" is not opened with Write access.</exception>
public static PresentationDocument Create(Package package, PresentationDocumentType type, bool autoSave)
{
var doc = new PresentationDocument
{
DocumentType = type,
OpenSettings = new OpenSettings { AutoSave = autoSave },
MainPartContentType = MainPartContentTypes[type],
};
doc.CreateCore(package);
return doc;
}
/// <summary>
/// Creates an editable PresentationDocument from a template, opened on
/// a MemoryStream with expandable capacity.
/// </summary>
/// <param name="path">The path and file name of the template.</param>
/// <returns>The new PresentationDocument based on the template.</returns>
public static PresentationDocument CreateFromTemplate(string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
// Check extensions as the template must have a valid Word Open XML extension.
string extension = Path.GetExtension(path);
if (extension != ".pptx" && extension != ".pptm" && extension != ".potx" && extension != ".potm")
{
throw new ArgumentException("Illegal template file: " + path, nameof(path));
}
using (PresentationDocument template = PresentationDocument.Open(path, false))
{
// We've opened the template in read-only mode to let multiple processes or
// threads open it without running into problems.
PresentationDocument document = (PresentationDocument)template.Clone();
// If the template is a document rather than a template, we are done.
if (extension == ".xlsx" || extension == ".xlsm")
{
return document;
}
// Otherwise, we'll have to do some more work.
document.ChangeDocumentType(PresentationDocumentType.Presentation);
// We are done, so save and return.
// TODO: Check whether it would be safe to return without saving.
document.Save();
return document;
}
}
/// <summary>
/// Creates a new instance of the PresentationDocument class from the specified file.
/// </summary>
/// <param name="path">The path and file name of the target PresentationDocument.</param>
/// <param name="isEditable">In ReadWrite mode. False for Read only mode.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when "path" is null reference.</exception>
/// <exception cref="OpenXmlPackageException">Thrown when the package is not valid Open XML PresentationDocument.</exception>
public static PresentationDocument Open(string path, bool isEditable)
{
return PresentationDocument.Open(path, isEditable, new OpenSettings());
}
/// <summary>
/// Creates a new instance of the PresentationDocument class from the IO stream.
/// </summary>
/// <param name="stream">The IO stream on which to open the PresentationDocument.</param>
/// <param name="isEditable">In ReadWrite mode. False for Read only mode.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when "stream" is null reference.</exception>
/// <exception cref="IOException">Thrown when "stream" is not opened with Read (ReadWrite) access.</exception>
/// <exception cref="OpenXmlPackageException">Thrown when the package is not valid Open XML PresentationDocument.</exception>
public static PresentationDocument Open(System.IO.Stream stream, bool isEditable)
{
return PresentationDocument.Open(stream, isEditable, new OpenSettings());
}
/// <summary>
/// Creates a new instance of the PresentationDocument class from the specified package.
/// </summary>
/// <param name="package">The specified OpenXml package.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when "package" is null reference.</exception>
/// <exception cref="IOException">Thrown when "package" is not opened with Read (ReadWrite) access.</exception>
/// <exception cref="OpenXmlPackageException">Thrown when the package is not valid Open XML PresentationDocument.</exception>
public static PresentationDocument Open(System.IO.Packaging.Package package)
{
return PresentationDocument.Open(package, new OpenSettings());
}
/// <summary>
/// Creates a new instance of the PresentationDocument class from the specified file.
/// </summary>
/// <param name="path">The path and file name of the target PresentationDocument.</param>
/// <param name="isEditable">In ReadWrite mode. False for Read only mode.</param>
/// <param name="openSettings">The advanced settings for opening a document.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when "path" is null reference.</exception>
/// <exception cref="OpenXmlPackageException">Thrown when the package is not valid Open XML PresentationDocument.</exception>
/// <exception cref="ArgumentException">Thrown when specified to process the markup compatibility but the given target FileFormatVersion is incorrect.</exception>
public static PresentationDocument Open(string path, bool isEditable, OpenSettings openSettings)
{
if (openSettings is null)
{
throw new ArgumentNullException(nameof(openSettings));
}
if (openSettings.MarkupCompatibilityProcessSettings.ProcessMode != MarkupCompatibilityProcessMode.NoProcess
&& !openSettings.MarkupCompatibilityProcessSettings.TargetFileFormatVersions.Any())
{
throw new ArgumentException(ExceptionMessages.InvalidMCMode);
}
var doc = new PresentationDocument
{
OpenSettings = new OpenSettings(openSettings),
};
doc.OpenCore(path, isEditable);
if (MainPartContentTypes[doc.DocumentType] != doc.MainPartContentType)
{
doc.UpdateDocumentTypeFromContentType();
}
return doc;
}
/// <summary>
/// Creates a new instance of the PresentationDocument class from the IO stream.
/// </summary>
/// <param name="stream">The IO stream on which to open the PresentationDocument.</param>
/// <param name="isEditable">In ReadWrite mode. False for Read only mode.</param>
/// <param name="openSettings">The advanced settings for opening a document.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when "stream" is null reference.</exception>
/// <exception cref="IOException">Thrown when "stream" is not opened with Read (ReadWrite) access.</exception>
/// <exception cref="OpenXmlPackageException">Thrown when the package is not valid Open XML PresentationDocument.</exception>
/// <exception cref="ArgumentException">Thrown when specified to process the markup compatibility but the given target FileFormatVersion is incorrect.</exception>
public static PresentationDocument Open(Stream stream, bool isEditable, OpenSettings openSettings)
{
if (openSettings is null)
{
throw new ArgumentNullException(nameof(openSettings));
}
if (openSettings.MarkupCompatibilityProcessSettings.ProcessMode != MarkupCompatibilityProcessMode.NoProcess
&& !openSettings.MarkupCompatibilityProcessSettings.TargetFileFormatVersions.Any())
{
throw new ArgumentException(ExceptionMessages.InvalidMCMode);
}
var doc = new PresentationDocument
{
OpenSettings = new OpenSettings(openSettings),
};
doc.OpenCore(stream, isEditable);
if (MainPartContentTypes[doc.DocumentType] != doc.MainPartContentType)
{
doc.UpdateDocumentTypeFromContentType();
}
return doc;
}
/// <summary>
/// Creates a new instance of the PresentationDocument class from the specified package.
/// </summary>
/// <param name="package">The specified OpenXml package.</param>
/// <param name="openSettings">The advanced settings for opening a document.</param>
/// <returns>A new instance of PresentationDocument.</returns>
/// <exception cref="ArgumentNullException">Thrown when package is a null reference.</exception>
/// <exception cref="OpenXmlPackageException">Thrown when package is not opened with read access.</exception>
/// <exception cref="OpenXmlPackageException">Thrown when the package is not a valid Open XML document.</exception>
/// <exception cref="ArgumentException">Thrown when specified to process the markup compatibility but the given target FileFormatVersion is incorrect.</exception>
public static PresentationDocument Open(Package package, OpenSettings openSettings)
{
if (openSettings is null)
{
throw new ArgumentNullException(nameof(openSettings));
}
if (openSettings.MarkupCompatibilityProcessSettings.ProcessMode != MarkupCompatibilityProcessMode.NoProcess
&& !openSettings.MarkupCompatibilityProcessSettings.TargetFileFormatVersions.Any())
{
throw new ArgumentException(ExceptionMessages.InvalidMCMode);
}
var doc = new PresentationDocument
{
OpenSettings = new OpenSettings(openSettings),
};
doc.OpenCore(package);
if (MainPartContentTypes[doc.DocumentType] != doc.MainPartContentType)
{
doc.UpdateDocumentTypeFromContentType();
}
return doc;
}
/// <summary>
/// Changes the document type.
/// </summary>
/// <param name="newType">The new type of the document.</param>
/// <remarks>The PresentationPart will be changed.</remarks>
public void ChangeDocumentType(PresentationDocumentType newType)
{
ThrowIfObjectDisposed();
if (newType == DocumentType)
{
// same type, just return
return;
}
if (FileOpenAccess == FileAccess.Read)
{
throw new IOException(ExceptionMessages.PackageAccessModeIsReadonly);
}
PresentationDocumentType oldType = DocumentType;
DocumentType = newType;
MainPartContentType = MainPartContentTypes[newType];
if (PresentationPart == null)
{
return;
}
try
{
ChangeDocumentTypeInternal<PresentationPart>();
}
catch (OpenXmlPackageException e)
{
if (e.Message == ExceptionMessages.CannotChangeDocumentType)
{
DocumentType = oldType;
MainPartContentType = MainPartContentTypes[oldType];
}
throw;
}
}
/// <summary>
/// Creates an instance of OpenXmlPart according to the given relationship type.
/// </summary>
/// <param name="relationshipType">Relationship type.</param>
/// <returns>An instance of OpenXmlPart.</returns>
internal sealed override OpenXmlPart CreatePartCore(string relationshipType)
{
ThrowIfObjectDisposed();
if (relationshipType == null)
{
throw new ArgumentNullException(nameof(relationshipType));
}
switch (relationshipType)
{
case PresentationPart.RelationshipTypeConstant:
return new PresentationPart();
case CoreFilePropertiesPart.RelationshipTypeConstant:
return new CoreFilePropertiesPart();
case ExtendedFilePropertiesPart.RelationshipTypeConstant:
return new ExtendedFilePropertiesPart();
case CustomFilePropertiesPart.RelationshipTypeConstant:
return new CustomFilePropertiesPart();
case ThumbnailPart.RelationshipTypeConstant:
return new ThumbnailPart();
case DigitalSignatureOriginPart.RelationshipTypeConstant:
return new DigitalSignatureOriginPart();
case QuickAccessToolbarCustomizationsPart.RelationshipTypeConstant:
return new QuickAccessToolbarCustomizationsPart();
case RibbonExtensibilityPart.RelationshipTypeConstant:
return new RibbonExtensibilityPart();
case RibbonAndBackstageCustomizationsPart.RelationshipTypeConstant:
return new RibbonAndBackstageCustomizationsPart();
case WebExTaskpanesPart.RelationshipTypeConstant:
return new WebExTaskpanesPart();
}
throw new ArgumentOutOfRangeException(nameof(relationshipType));
}
/// <summary>
/// Creates the PresentationPart and add it to this document.
/// </summary>
/// <returns>The newly added PresentationPart.</returns>
public PresentationPart AddPresentationPart()
{
PresentationPart childPart = new PresentationPart();
InitPart(childPart, MainPartContentType);
return childPart;
}
/// <summary>
/// Adds a CoreFilePropertiesPart to the PresentationDocument.
/// </summary>
/// <returns>The newly added CoreFilePropertiesPart.</returns>
public CoreFilePropertiesPart AddCoreFilePropertiesPart()
{
CoreFilePropertiesPart childPart = new CoreFilePropertiesPart();
InitPart(childPart, CoreFilePropertiesPart.ContentTypeConstant);
return childPart;
}
/// <summary>
/// Adds a ExtendedFilePropertiesPart to the PresentationDocument.
/// </summary>
/// <returns>The newly added ExtendedFilePropertiesPart.</returns>
public ExtendedFilePropertiesPart AddExtendedFilePropertiesPart()
{
ExtendedFilePropertiesPart childPart = new ExtendedFilePropertiesPart();
InitPart(childPart, ExtendedFilePropertiesPart.ContentTypeConstant);
return childPart;
}
/// <summary>
/// Adds a CustomFilePropertiesPart to the PresentationDocument.
/// </summary>
/// <returns>The newly added CustomFilePropertiesPart.</returns>
public CustomFilePropertiesPart AddCustomFilePropertiesPart()
{
CustomFilePropertiesPart childPart = new CustomFilePropertiesPart();
InitPart(childPart, CustomFilePropertiesPart.ContentTypeConstant);
return childPart;
}
/// <summary>
/// Adds a DigitalSignatureOriginPart to the PresentationDocuments.
/// </summary>
/// <returns>The newly added DigitalSignatureOriginPart.</returns>
public DigitalSignatureOriginPart AddDigitalSignatureOriginPart()
{
DigitalSignatureOriginPart childPart = new DigitalSignatureOriginPart();
InitPart(childPart, DigitalSignatureOriginPart.ContentTypeConstant);
return childPart;
}
/// <summary>
/// Adds a ThumbnailPart to the PresentationDocument.
/// </summary>
/// <param name="contentType">The content type of the ThumbnailPart.</param>
/// <returns>The newly added ThumbnailPart.</returns>
public ThumbnailPart AddThumbnailPart(string contentType)
{
ThumbnailPart childPart = new ThumbnailPart();
InitPart(childPart, contentType);
return childPart;
}
/// <summary>
/// Adds a ThumbnailPart to the PresentationDocument.
/// </summary>
/// <param name="partType">The type of the ThumbnailPart.</param>
/// <returns>The newly added ThumbnailPart.</returns>
public ThumbnailPart AddThumbnailPart(ThumbnailPartType partType)
{
string contentType = ThumbnailPartTypeInfo.GetContentType(partType);
string partExtension = ThumbnailPartTypeInfo.GetTargetExtension(partType);
PartExtensionProvider.MakeSurePartExtensionExist(contentType, partExtension);
return AddThumbnailPart(contentType);
}
/// <summary>
/// Adds a new part of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The class of the part.</typeparam>
/// <param name="contentType">The content type of the part. Must match the defined content type if the part is fixed content type.</param>
/// <param name="id">The relationship id. The id will be automatically generated if this param is null.</param>
/// <returns>The added part.</returns>
/// <exception cref="OpenXmlPackageException">When the part is not allowed to be referenced by this part.</exception>
/// <exception cref="ArgumentOutOfRangeException">When the part is fixed content type and the passed in contentType does not match the defined content type.</exception>
/// <exception cref="ArgumentNullException">Thrown when "contentType" is null reference.</exception>
/// <remarks>Mainly used for adding not-fixed content type part - ImagePart, etc.</remarks>
public override T AddNewPart<T>(string contentType, string id)
{
if (contentType == null)
{
throw new ArgumentNullException(nameof(contentType));
}
if (typeof(PresentationPart).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo()) && contentType != PresentationDocument.MainPartContentTypes[_documentType])
{
throw new OpenXmlPackageException(ExceptionMessages.ErrorContentType);
}
return base.AddNewPart<T>(contentType, id);
}
/// <summary>
/// Add a QuickAccessToolbarCustomizationsPart to the PresentationDocument.
/// </summary>
/// <returns>The newly added QuickAccessToolbarCustomizationsPart.</returns>
public QuickAccessToolbarCustomizationsPart AddQuickAccessToolbarCustomizationsPart()
{
QuickAccessToolbarCustomizationsPart childPart = new QuickAccessToolbarCustomizationsPart();
InitPart(childPart, QuickAccessToolbarCustomizationsPart.ContentTypeConstant);
return childPart;
}
/// <summary>
/// Add a RibbonExtensibilityPart to the PresentationDocument.
/// </summary>
/// <returns>The newly added RibbonExtensibilityPart.</returns>
public RibbonExtensibilityPart AddRibbonExtensibilityPart()
{
RibbonExtensibilityPart childPart = new RibbonExtensibilityPart();
InitPart(childPart, RibbonExtensibilityPart.ContentTypeConstant);
return childPart;
}
/// <summary>
/// Add a RibbonAndBackstageCustomizationsPart to the PresentationDocument, this part is only available in Office2010.
/// </summary>
/// <returns>The newly added RibbonExtensibilityPart.</returns>
public RibbonAndBackstageCustomizationsPart AddRibbonAndBackstageCustomizationsPart()
{
RibbonAndBackstageCustomizationsPart childPart = new RibbonAndBackstageCustomizationsPart();
InitPart(childPart, RibbonAndBackstageCustomizationsPart.ContentTypeConstant);
return childPart;
}
/// <summary>
/// Adds a WebExTaskpanesPart to the PresentationDocument, this part is only available in Office2013.
/// </summary>
/// <returns>The newly added WebExTaskpanesPart.</returns>
public WebExTaskpanesPart AddWebExTaskpanesPart()
{
WebExTaskpanesPart childPart = new WebExTaskpanesPart();
InitPart(childPart, WebExTaskpanesPart.ContentTypeConstant);
return childPart;
}
/// <inheritdoc />
public override OpenXmlPart RootPart => PresentationPart;
/// <summary>
/// Gets the PresentationPart of the PresentationDocument.
/// </summary>
public PresentationPart PresentationPart
{
get { return GetSubPartOfType<PresentationPart>(); }
}
/// <summary>
/// Gets the CoreFilePropertiesPart of the PresentationDocument.
/// </summary>
public CoreFilePropertiesPart CoreFilePropertiesPart
{
get { return GetSubPartOfType<CoreFilePropertiesPart>(); }
}
/// <summary>
/// Gets the ExtendedFilePropertiesPart of the PresentationDocument.
/// </summary>
public ExtendedFilePropertiesPart ExtendedFilePropertiesPart
{
get { return GetSubPartOfType<ExtendedFilePropertiesPart>(); }
}
/// <summary>
/// Gets the CustomFilePropertiesPart of the PresentationDocument.
/// </summary>
public CustomFilePropertiesPart CustomFilePropertiesPart
{
get { return GetSubPartOfType<CustomFilePropertiesPart>(); }
}
/// <summary>
/// Gets the ThumbnailPart of the PresentationDocument.
/// </summary>
public ThumbnailPart ThumbnailPart
{
get
{
return GetSubPartOfType<ThumbnailPart>();
}
}
/// <summary>
/// Gets the DigitalSignatureOriginPart of the PresentationDocument.
/// </summary>
public DigitalSignatureOriginPart DigitalSignatureOriginPart
{
get { return GetSubPartOfType<DigitalSignatureOriginPart>(); }
}
/// <summary>
/// Gets the RibbonExtensibilityPart of the PresentationDocument.
/// </summary>
public RibbonExtensibilityPart RibbonExtensibilityPart
{
get { return GetSubPartOfType<RibbonExtensibilityPart>(); }
}
/// <summary>
/// Gets the QuickAccessToolbarCustomizationsPart of the PresentationDocument.
/// </summary>
public QuickAccessToolbarCustomizationsPart QuickAccessToolbarCustomizationsPart
{
get { return GetSubPartOfType<QuickAccessToolbarCustomizationsPart>(); }
}
/// <summary>
/// Gets the RibbonAndBackstageCustomizationsPart of the PresentationDocument, only available in Office2010.
/// </summary>
[OfficeAvailability(FileFormatVersions.Office2010)]
public RibbonAndBackstageCustomizationsPart RibbonAndBackstageCustomizationsPart
{
get { return GetSubPartOfType<RibbonAndBackstageCustomizationsPart>(); }
}
/// <summary>
/// Gets the WebExTaskpanesPart of the PresentationDocument, only available in Office2013.
/// </summary>
[OfficeAvailability(FileFormatVersions.Office2013)]
public WebExTaskpanesPart WebExTaskpanesPart
{
get { return GetSubPartOfType<WebExTaskpanesPart>(); }
}
#region cloning
#region Stream-based cloning
/// <summary>
/// Creates a new OpenXmlPackage on the given stream.
/// </summary>
/// <param name="stream">The stream on which the concrete OpenXml package will be created.</param>
/// <returns>A new instance of OpenXmlPackage.</returns>
protected override OpenXmlPackage CreateClone(Stream stream)
{
return PresentationDocument.Create(stream, DocumentType, OpenSettings.AutoSave);
}
/// <summary>
/// Opens the cloned OpenXml package on the given stream.
/// </summary>
/// <param name="stream">The stream on which the cloned OpenXml package will be opened.</param>
/// <param name="isEditable">In ReadWrite mode. False for Read only mode.</param>
/// <param name="openSettings">The advanced settings for opening a document.</param>
/// <returns>A new instance of OpenXmlPackage.</returns>
protected override OpenXmlPackage OpenClone(Stream stream, bool isEditable, OpenSettings openSettings)
{
return PresentationDocument.Open(stream, isEditable, openSettings);
}
#endregion Stream-based cloning
#region File-based cloning
/// <summary>
/// Creates a new OpenXml package on the given file.
/// </summary>
/// <param name="path">The path and file name of the target OpenXml package.</param>
/// <returns>A new instance of OpenXmlPackage.</returns>
protected override OpenXmlPackage CreateClone(string path)
{
return PresentationDocument.Create(path, DocumentType, OpenSettings.AutoSave);
}
/// <summary>
/// Opens the cloned OpenXml package on the given file.
/// </summary>
/// <param name="path">The path and file name of the target OpenXml package.</param>
/// <param name="isEditable">In ReadWrite mode. False for Read only mode.</param>
/// <param name="openSettings">The advanced settings for opening a document.</param>
/// <returns>A new instance of OpenXmlPackage.</returns>
protected override OpenXmlPackage OpenClone(string path, bool isEditable, OpenSettings openSettings)
{
return PresentationDocument.Open(path, isEditable, openSettings);
}
#endregion File-based cloning
#region Package-based cloning
/// <summary>
/// Creates a new instance of OpenXmlPackage on the specified instance
/// of Package.
/// </summary>
/// <param name="package">The specified instance of Package.</param>
/// <returns>A new instance of OpenXmlPackage.</returns>
protected override OpenXmlPackage CreateClone(Package package)
{
return PresentationDocument.Create(package, DocumentType, OpenSettings.AutoSave);
}
#endregion Package-based cloning
#endregion cloning
}
}