forked from EvotecIT/OfficeIMO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordCompatibilitySettings.cs
73 lines (66 loc) · 2.79 KB
/
WordCompatibilitySettings.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
using System;
using DocumentFormat.OpenXml;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Wordprocessing;
namespace OfficeIMO.Word {
public enum CompatibilityMode {
None = 0,
Word2003 = 11,
Word2007 = 12,
Word2010 = 14,
Word2013 = 15
}
public class WordCompatibilitySettings {
private WordprocessingDocument _wordprocessingDocument;
private WordDocument _document;
public WordCompatibilitySettings(WordDocument document) {
_document = document;
_wordprocessingDocument = document._wordprocessingDocument;
document.CompatibilitySettings = this;
}
/// <summary>
/// Gets or sets compatibility mode of a Word Document
/// </summary>
public CompatibilityMode CompatibilityMode {
get {
var settings = _document._wordprocessingDocument.MainDocumentPart.DocumentSettingsPart.Settings;
var compatibility = settings.OfType<Compatibility>().FirstOrDefault();
if (compatibility == null) {
return CompatibilityMode.None;
}
foreach (var setting in compatibility.OfType<CompatibilitySetting>()) {
if (setting.Name == CompatSettingNameValues.CompatibilityMode) {
return (CompatibilityMode)int.Parse(setting.Val);
}
}
return CompatibilityMode.None;
}
set {
var settings = _document._wordprocessingDocument.MainDocumentPart.DocumentSettingsPart.Settings;
var compatibility = settings.OfType<Compatibility>().FirstOrDefault();
if (compatibility == null) {
compatibility = new Compatibility();
settings.Append(compatibility);
}
foreach (var setting in compatibility.OfType<CompatibilitySetting>()) {
if (setting.Name == CompatSettingNameValues.CompatibilityMode) {
if (value == CompatibilityMode.None) {
setting.Remove();
} else {
setting.Val = ((int)value).ToString();
setting.Uri = "http://schemas.microsoft.com/office/word";
}
return;
}
}
compatibility.Append(new CompatibilitySetting() {
Name = CompatSettingNameValues.CompatibilityMode,
Uri = "http://schemas.microsoft.com/office/word",
Val = ((int)value).ToString()
});
}
}
}
}