-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathDocumenteditor.cs
More file actions
151 lines (144 loc) · 5.35 KB
/
Documenteditor.cs
File metadata and controls
151 lines (144 loc) · 5.35 KB
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
using System;
using System.Diagnostics;
using System.IO;
using Syncfusion.EJ2.DocumentEditor;
using WDocument = Syncfusion.DocIO.DLS.WordDocument;
using WFormatType = Syncfusion.DocIO.FormatType;
namespace DocumentEditorLibrary
{
public class Documenteditor
{
public string Import(byte[] byteArr, string fileName)
{
try
{
int index = fileName.LastIndexOf('.');
string type = index > -1 && index < fileName.Length - 1 ? fileName.Substring(index) : ".docx";
MemoryStream stream = new MemoryStream(byteArr);
stream.Position = 0;
WordDocument document = WordDocument.Load(stream, GetFormatType(type.ToLower()));
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
document.Dispose();
stream.Dispose();
return json;
}
catch (Exception ex)
{
Trace.WriteLine($"Error loading Word document: {ex.Message}");
return $"Error loading Word document: {ex.Message}";
}
}
public string SystemClipboard(string Content, string type)
{
if (Content != null && Content != "")
{
try
{
WordDocument document = WordDocument.LoadString(Content, GetFormatType(type.ToLower()));
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
document.Dispose();
return json;
}
catch (Exception ex)
{
return ex.Message;
}
}
return "";
}
public string Save(string content, string fileName)
{
try
{
string name = fileName;
string format = RetrieveFileType(name);
if (string.IsNullOrEmpty(name))
{
name = "Document1.doc";
}
WDocument document = WordDocument.Save(content);
FileStream fileStream = new FileStream(name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
document.Save(fileStream, GetWFormatType(format));
document.Close();
fileStream.Close();
return "Pass";
}
catch (Exception ex)
{
return ex.Message;
}
}
private string RetrieveFileType(string name)
{
int index = name.LastIndexOf('.');
string format = index > -1 && index < name.Length - 1 ?
name.Substring(index) : ".doc";
return format;
}
public string RestrictEditing(string passwordBase64, string saltBase64, int spinCount)
{
if (passwordBase64 == "" && passwordBase64 == null)
return null;
string[] result = WordDocument.ComputeHash(passwordBase64, saltBase64, spinCount);
return Newtonsoft.Json.JsonConvert.SerializeObject(result);
}
internal static FormatType GetFormatType(string format)
{
if (string.IsNullOrEmpty(format))
throw new NotSupportedException("EJ2 DocumentEditor does not support this file format.");
switch (format.ToLower())
{
case ".dotx":
case ".docx":
case ".docm":
case ".dotm":
return FormatType.Docx;
case ".dot":
case ".doc":
return FormatType.Doc;
case ".rtf":
return FormatType.Rtf;
case ".txt":
return FormatType.Txt;
case ".xml":
return FormatType.WordML;
case ".html":
return FormatType.Html;
default:
throw new NotSupportedException("EJ2 DocumentEditor does not support this file format.");
}
}
internal static WFormatType GetWFormatType(string format)
{
if (string.IsNullOrEmpty(format))
throw new NotSupportedException("EJ2 DocumentEditor does not support this file format.");
switch (format.ToLower())
{
case ".dotx":
return WFormatType.Dotx;
case ".docx":
return WFormatType.Docx;
case ".docm":
return WFormatType.Docm;
case ".dotm":
return WFormatType.Dotm;
case ".dot":
return WFormatType.Dot;
case ".doc":
return WFormatType.Doc;
case ".rtf":
return WFormatType.Rtf;
case ".html":
return WFormatType.Html;
case ".txt":
return WFormatType.Txt;
case ".xml":
return WFormatType.WordML;
case ".odt":
return WFormatType.Odt;
default:
throw new NotSupportedException("EJ2 DocumentEditor does not support this file format.");
}
}
}
}