Skip to content

Commit 92b1153

Browse files
authored
Merge pull request #1 from SyncfusionExamples/demo
Added core sample
2 parents bf1bf76 + e67e78f commit 92b1153

19 files changed

+3385
-0
lines changed
816 KB
Binary file not shown.
Lines changed: 388 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,388 @@
1+
using DocumentEditorApp.Models;
2+
using Microsoft.AspNetCore.Cors;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Syncfusion.DocIO;
7+
using Syncfusion.DocIO.DLS;
8+
using System;
9+
using System.Data.OleDb;
10+
using System.IO;
11+
using System.Collections.Generic;
12+
using EJ2DocumentEditor = Syncfusion.EJ2.DocumentEditor;
13+
using Syncfusion.DocIORenderer;
14+
using Syncfusion.Pdf;
15+
using DocIOWordDocument = Syncfusion.DocIO.DLS.WordDocument;
16+
using System.Data.SqlClient;
17+
using System.Text;
18+
using System.Data;
19+
20+
namespace DocumentEditorApp.Controllers
21+
{
22+
[Route("api/[controller]")]
23+
[ApiController]
24+
public class DocumenteditorController : ControllerBase
25+
{
26+
private IHostingEnvironment hostEnvironment;
27+
28+
private string dataSourcePath;
29+
30+
private string connectionString;
31+
public DocumenteditorController(IHostingEnvironment environment)
32+
{
33+
this.hostEnvironment = environment;
34+
this.dataSourcePath = Path.Combine(this.hostEnvironment.ContentRootPath,"AppData"+"\\DocumentInfo.accdb");
35+
this.connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + this.dataSourcePath + ";User Id=admin;Password=;";
36+
}
37+
38+
[Route("Import")]
39+
public string Import(IFormCollection data)
40+
{
41+
if (data.Files.Count == 0)
42+
return null;
43+
System.IO.Stream stream = new System.IO.MemoryStream();
44+
Microsoft.AspNetCore.Http.IFormFile file = data.Files[0];
45+
int index = file.FileName.LastIndexOf('.');
46+
string type = index > -1 && index < file.FileName.Length - 1 ?
47+
file.FileName.Substring(index) : ".docx";
48+
file.CopyTo(stream);
49+
stream.Position = 0;
50+
51+
EJ2DocumentEditor.WordDocument document = EJ2DocumentEditor.WordDocument.Load(stream, GetFormatType(type.ToLower()));
52+
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
53+
document.Dispose();
54+
return json;
55+
56+
}
57+
public class CustomClipboarParameter
58+
{
59+
public string content { get; set; }
60+
public string type { get; set; }
61+
}
62+
63+
64+
[Route("SystemClipboard")]
65+
public string SystemClipboard([FromBody]CustomClipboarParameter param)
66+
{
67+
if (param.content != null && param.content != "")
68+
{
69+
try
70+
{
71+
Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.LoadString(param.content, GetFormatType(param.type.ToLower()));
72+
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
73+
document.Dispose();
74+
return json;
75+
}
76+
catch (Exception)
77+
{
78+
return "";
79+
}
80+
}
81+
return "";
82+
}
83+
84+
public class CustomRestrictParameter
85+
{
86+
public string passwordBase64 { get; set; }
87+
public string saltBase64 { get; set; }
88+
public int spinCount { get; set; }
89+
}
90+
91+
[Route("RestrictEditing")]
92+
public string[] RestrictEditing([FromBody]CustomRestrictParameter param)
93+
{
94+
if (param.passwordBase64 == "" && param.passwordBase64 == null)
95+
return null;
96+
return Syncfusion.EJ2.DocumentEditor.WordDocument.ComputeHash(param.passwordBase64, param.saltBase64, param.spinCount);
97+
}
98+
99+
100+
[Route("ImportFile")]
101+
public string ImportFile([FromBody]Documentdetails param)
102+
{
103+
string path = this.hostEnvironment.WebRootPath + "\\Files\\" + param.FileName;
104+
try
105+
{
106+
string query = "SELECT EditorName FROM DocumentInfo WHERE DocumentName = '"+param.FileName+"'";
107+
string dataSourcePath = Path.Combine(hostEnvironment.ContentRootPath,"AppData"+"\\DocumentInfo.accdb");
108+
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dataSourcePath + ";User Id=admin;Password=;";
109+
DataTable table = getDatabaseData(connectionString,query);
110+
string Result = Newtonsoft.Json.JsonConvert.SerializeObject(table);
111+
string status = (string)table.Rows[0][0];
112+
if(status != ""){
113+
return Result;
114+
}
115+
DateTime date = DateTime.Now;
116+
query = "UPDATE DocumentInfo SET EditorName = '"+param.userName+"', LastModifiedBy= '"+param.userName+"', LastModifiedTime =#"+date+"# WHERE DocumentName = '"+param.FileName+"'";
117+
performCRUD(connectionString,query);
118+
Stream stream = System.IO.File.Open(path, FileMode.Open, FileAccess.ReadWrite);
119+
Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(stream, GetFormatType(path));
120+
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
121+
document.Dispose();
122+
stream.Dispose();
123+
return json;
124+
}
125+
catch(Exception ex)
126+
{
127+
return ex.Message;
128+
}
129+
}
130+
131+
[Route("LogOut")]
132+
133+
public void LogOut([FromBody]Documentdetails param){
134+
string SQLquery = "UPDATE DocumentInfo SET EditorName = '' WHERE DocumentName = '"+param.FileName+"'";
135+
performCRUD(connectionString,SQLquery);
136+
}
137+
138+
[AcceptVerbs("Post")]
139+
[HttpPost]
140+
[Route("ExportPdf")]
141+
public FileStreamResult ExportPdf([FromBody] SaveParameter data)
142+
{
143+
// Converts the sfdt to stream
144+
Stream document = EJ2DocumentEditor.WordDocument.Save(data.Content, EJ2DocumentEditor.FormatType.Docx);
145+
Syncfusion.DocIO.DLS.WordDocument doc = new Syncfusion.DocIO.DLS.WordDocument(document, Syncfusion.DocIO.FormatType.Docx);
146+
//Instantiation of DocIORenderer for Word to PDF conversion
147+
DocIORenderer render = new DocIORenderer();
148+
//Converts Word document into PDF document
149+
PdfDocument pdfDocument = render.ConvertToPDF(doc);
150+
Stream stream = new MemoryStream();
151+
152+
//Saves the PDF file
153+
pdfDocument.Save(stream);
154+
stream.Position = 0;
155+
pdfDocument.Close();
156+
document.Close();
157+
return new FileStreamResult(stream, "application/pdf")
158+
{
159+
FileDownloadName = data.FileName
160+
};
161+
}
162+
public class SaveParameter
163+
{
164+
public string Content { get; set; }
165+
public string FileName { get; set; }
166+
public string UserName { get; set; }
167+
}
168+
169+
170+
[Route("ExportSFDT")]
171+
public void ExportSFDT([FromBody]Saveparameter data)
172+
{
173+
string name = data.FileName;
174+
string path = this.hostEnvironment.WebRootPath + "\\Files\\" + data.FileName;
175+
EJ2DocumentEditor.FormatType format = GetFormatType(data.FileName);
176+
if (string.IsNullOrEmpty(name))
177+
{
178+
name = "Document1.doc";
179+
}
180+
DocIOWordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.Save(data.content);
181+
FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
182+
document.Save(fileStream, GetDocIOFomatType(format));
183+
document.Close();
184+
fileStream.Close();
185+
}
186+
187+
[Route("InsertRow")]
188+
189+
public int InsertRow([FromBody]Documentdetails param)
190+
{
191+
192+
string SQLquery = "SELECT COUNT(DocumentName) FROM DocumentInfo WHERE DocumentName = '"+param.FileName+"';";
193+
using (OleDbConnection connection = new OleDbConnection(connectionString))
194+
{
195+
OleDbCommand command = new OleDbCommand(SQLquery);
196+
197+
command.Connection = connection;
198+
199+
try
200+
{
201+
connection.Open();
202+
int count = (int)command.ExecuteScalar();
203+
connection.Close();
204+
if(count != 0){
205+
return 0;
206+
}
207+
else {
208+
DateTime date = DateTime.Now;
209+
210+
string insertSQL = "INSERT INTO DocumentInfo (DocumentName, AuthorName, LastModifiedTime, LastModifiedBy, EditorName) " +
211+
"VALUES ('"+param.FileName+"', '"+param.userName+"', #"+date+"#, '"+param.userName+"', '"+param.userName+"')";
212+
213+
performCRUD(connectionString,insertSQL);
214+
}
215+
}
216+
catch (Exception ex)
217+
{
218+
Console.WriteLine(ex.Message);
219+
}
220+
}
221+
222+
223+
return 1;
224+
}
225+
226+
[Route("RetriveDataSource")]
227+
228+
public string RetriveDataSource()
229+
{
230+
231+
var JSONString = "";
232+
string dataSourcePath = Path.Combine(hostEnvironment.ContentRootPath,"AppData"+"\\DocumentInfo.accdb");
233+
string SQLquery = "SELECT DocumentName,AuthorName,LastModifiedTime,LastModifiedBy FROM DocumentInfo";
234+
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dataSourcePath + ";User Id=admin;Password=;";
235+
DataTable table = getDatabaseData(connectionString,SQLquery);
236+
JSONString = Newtonsoft.Json.JsonConvert.SerializeObject(table);
237+
return JSONString;
238+
}
239+
240+
[Route("DeleteRecords")]
241+
242+
public void DeleteRecords([FromBody]CustomParams param){
243+
244+
string SQLquery = "DELETE FROM DocumentInfo WHERE DocumentName = '"+param.fileName+"'";
245+
performCRUD(connectionString,SQLquery);
246+
}
247+
public void performCRUD(string connectionString, string SQLquery){
248+
249+
using (OleDbConnection connection = new OleDbConnection(connectionString))
250+
{
251+
OleDbCommand command = new OleDbCommand(SQLquery);
252+
253+
command.Connection = connection;
254+
255+
try
256+
{
257+
connection.Open();
258+
command.ExecuteNonQuery();
259+
connection.Close();
260+
}
261+
catch (Exception ex)
262+
{
263+
Console.WriteLine(ex.Message);
264+
}
265+
}
266+
}
267+
268+
public DataTable getDatabaseData(string connectionString, string SQLquery){
269+
270+
DataTable table = new DataTable();
271+
try {
272+
using(OleDbConnection con = new OleDbConnection(connectionString))
273+
{
274+
using(OleDbCommand command = new OleDbCommand(SQLquery,con))
275+
{
276+
con.Open();
277+
OleDbDataReader reader = command.ExecuteReader();
278+
279+
280+
table.Load(reader);
281+
con.Close();
282+
}
283+
}
284+
}
285+
catch (Exception ex){
286+
Console.WriteLine(ex.Message);
287+
}
288+
return table;
289+
}
290+
291+
public class Documentdetails
292+
{
293+
public string userName { get; set; }
294+
public string FileName { get; set; }
295+
296+
}
297+
public class Saveparameter
298+
{
299+
public string content { get; set; }
300+
public string FileName { get; set; }
301+
302+
}
303+
304+
//Save document in web server.
305+
[Route("Save")]
306+
public string Save([FromBody]CustomParameter param)
307+
{
308+
string path = this.hostEnvironment.WebRootPath + "\\Files\\" + param.fileName;
309+
Byte[] byteArray = Convert.FromBase64String(param.documentData);
310+
Stream stream = new MemoryStream(byteArray);
311+
EJ2DocumentEditor.FormatType type = GetFormatType(path);
312+
try
313+
{
314+
FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
315+
316+
if (type != EJ2DocumentEditor.FormatType.Docx)
317+
{
318+
Syncfusion.DocIO.DLS.WordDocument document = new Syncfusion.DocIO.DLS.WordDocument(stream, Syncfusion.DocIO.FormatType.Docx);
319+
document.Save(fileStream, GetDocIOFomatType(type));
320+
document.Close();
321+
}
322+
else
323+
{
324+
stream.Position = 0;
325+
stream.CopyTo(fileStream);
326+
}
327+
stream.Dispose();
328+
fileStream.Dispose();
329+
return "Sucess";
330+
}
331+
catch
332+
{
333+
Console.WriteLine("err");
334+
return "Failure";
335+
}
336+
}
337+
338+
internal static EJ2DocumentEditor.FormatType GetFormatType(string fileName)
339+
{
340+
int index = fileName.LastIndexOf('.');
341+
string format = index > -1 && index < fileName.Length - 1 ? fileName.Substring(index + 1) : "";
342+
343+
if (string.IsNullOrEmpty(format))
344+
throw new NotSupportedException("EJ2 Document editor does not support this file format.");
345+
switch (format.ToLower())
346+
{
347+
case "dotx":
348+
case "docx":
349+
case "docm":
350+
case "dotm":
351+
return EJ2DocumentEditor.FormatType.Docx;
352+
case "dot":
353+
case "doc":
354+
return EJ2DocumentEditor.FormatType.Doc;
355+
case "rtf":
356+
return EJ2DocumentEditor.FormatType.Rtf;
357+
case "txt":
358+
return EJ2DocumentEditor.FormatType.Txt;
359+
case "xml":
360+
return EJ2DocumentEditor.FormatType.WordML;
361+
default:
362+
throw new NotSupportedException("EJ2 Document editor does not support this file format.");
363+
}
364+
}
365+
366+
internal static Syncfusion.DocIO.FormatType GetDocIOFomatType(EJ2DocumentEditor.FormatType type)
367+
{
368+
switch (type)
369+
{
370+
case EJ2DocumentEditor.FormatType.Docx:
371+
return FormatType.Docx;
372+
case EJ2DocumentEditor.FormatType.Doc:
373+
return FormatType.Doc;
374+
case EJ2DocumentEditor.FormatType.Rtf:
375+
return FormatType.Rtf;
376+
case EJ2DocumentEditor.FormatType.Txt:
377+
return FormatType.Txt;
378+
case EJ2DocumentEditor.FormatType.WordML:
379+
return FormatType.WordML;
380+
default:
381+
throw new NotSupportedException("DocIO does not support this file format.");
382+
}
383+
}
384+
385+
}
386+
387+
388+
}

0 commit comments

Comments
 (0)