Skip to content

Commit 1479bba

Browse files
931795: Sample on How to Load PDF to Server Side PDF Viewer
1 parent 497adc8 commit 1479bba

File tree

84 files changed

+74937
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+74937
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.4.33403.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreSample", "CoreSample\CoreSample.csproj", "{6732037C-54F8-407D-9651-8B9A16F08A7F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{6732037C-54F8-407D-9651-8B9A16F08A7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{6732037C-54F8-407D-9651-8B9A16F08A7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{6732037C-54F8-407D-9651-8B9A16F08A7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{6732037C-54F8-407D-9651-8B9A16F08A7F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {E1D8E12F-9CC3-44F5-B57F-184524C265B7}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Cors;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.Http;
5+
using Syncfusion.DocIO;
6+
using System;
7+
using System.IO;
8+
using EJ2DocumentEditor = Syncfusion.EJ2.DocumentEditor;
9+
namespace DocumentEditor.Controllers;
10+
11+
[ApiController]
12+
[Route("api/[controller]")]
13+
public class DocumenteditorController : ControllerBase
14+
{
15+
private IHostEnvironment hostEnvironment;
16+
public DocumenteditorController(IHostEnvironment environment)
17+
{
18+
this.hostEnvironment = environment;
19+
}
20+
//Import file from client side.
21+
[Route("Import")]
22+
public string Import(IFormCollection data)
23+
{
24+
if (data.Files.Count == 0)
25+
return null;
26+
Stream stream = new MemoryStream();
27+
IFormFile file = data.Files[0];
28+
int index = file.FileName.LastIndexOf('.');
29+
string type = index > -1 && index < file.FileName.Length - 1 ?
30+
file.FileName.Substring(index) : ".docx";
31+
file.CopyTo(stream);
32+
stream.Position = 0;
33+
34+
EJ2DocumentEditor.WordDocument document = EJ2DocumentEditor.WordDocument.Load(stream, GetFormatType(type.ToLower()));
35+
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
36+
document.Dispose();
37+
return json;
38+
}
39+
//Import documents from web server.
40+
[Route("ImportFile")]
41+
public string ImportFile([FromBody]CustomParams param)
42+
{
43+
string path = this.hostEnvironment.ContentRootPath + "\\Files\\" + param.fileName;
44+
try
45+
{
46+
Stream stream = System.IO.File.Open(path, FileMode.Open, FileAccess.ReadWrite);
47+
Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(stream, GetFormatType(path));
48+
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
49+
document.Dispose();
50+
stream.Dispose();
51+
return json;
52+
}
53+
catch
54+
{
55+
return "Failure";
56+
}
57+
}
58+
public class CustomParams
59+
{
60+
public string fileName
61+
{
62+
get;
63+
set;
64+
}
65+
}
66+
67+
[Route("LoadDefault")]
68+
public string LoadDefault()
69+
{
70+
string path = this.hostEnvironment.ContentRootPath + "\\Files\\" + "sample.docx";
71+
Stream stream = System.IO.File.OpenRead(path);
72+
stream.Position = 0;
73+
74+
Syncfusion.EJ2.DocumentEditor.WordDocument document = Syncfusion.EJ2.DocumentEditor.WordDocument.Load(stream, EJ2DocumentEditor.FormatType.Docx);
75+
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
76+
document.Dispose();
77+
return json;
78+
}
79+
80+
//Save document in web server.
81+
[Route("Save")]
82+
public string Save([FromBody]CustomParameter param)
83+
{
84+
string path = this.hostEnvironment.ContentRootPath + "\\Files\\" + param.fileName;
85+
Byte[] byteArray = Convert.FromBase64String(param.documentData);
86+
Stream stream = new MemoryStream(byteArray);
87+
EJ2DocumentEditor.FormatType type = GetFormatType(path);
88+
try
89+
{
90+
FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
91+
92+
if (type != EJ2DocumentEditor.FormatType.Docx)
93+
{
94+
Syncfusion.DocIO.DLS.WordDocument document = new Syncfusion.DocIO.DLS.WordDocument(stream, Syncfusion.DocIO.FormatType.Docx);
95+
document.Save(fileStream, GetDocIOFomatType(type));
96+
document.Close();
97+
}
98+
else
99+
{
100+
stream.Position = 0;
101+
stream.CopyTo(fileStream);
102+
}
103+
stream.Dispose();
104+
fileStream.Dispose();
105+
return "Sucess";
106+
}
107+
catch
108+
{
109+
Console.WriteLine("err");
110+
return "Failure";
111+
}
112+
}
113+
public class CustomParameter
114+
{
115+
public string fileName
116+
{
117+
get;
118+
set;
119+
}
120+
public string documentData
121+
{
122+
get;
123+
set;
124+
}
125+
}
126+
127+
internal static EJ2DocumentEditor.FormatType GetFormatType(string fileName)
128+
{
129+
int index = fileName.LastIndexOf('.');
130+
string format = index > -1 && index < fileName.Length - 1 ? fileName.Substring(index + 1) : "";
131+
132+
if (string.IsNullOrEmpty(format))
133+
throw new NotSupportedException("EJ2 Document editor does not support this file format.");
134+
switch (format.ToLower())
135+
{
136+
case "dotx":
137+
case "docx":
138+
case "docm":
139+
case "dotm":
140+
return EJ2DocumentEditor.FormatType.Docx;
141+
case "dot":
142+
case "doc":
143+
return EJ2DocumentEditor.FormatType.Doc;
144+
case "rtf":
145+
return EJ2DocumentEditor.FormatType.Rtf;
146+
case "txt":
147+
return EJ2DocumentEditor.FormatType.Txt;
148+
case "xml":
149+
return EJ2DocumentEditor.FormatType.WordML;
150+
default:
151+
throw new NotSupportedException("EJ2 Document editor does not support this file format.");
152+
}
153+
}
154+
155+
internal static Syncfusion.DocIO.FormatType GetDocIOFomatType(EJ2DocumentEditor.FormatType type)
156+
{
157+
switch (type)
158+
{
159+
case EJ2DocumentEditor.FormatType.Docx:
160+
return FormatType.Docx;
161+
case EJ2DocumentEditor.FormatType.Doc:
162+
return FormatType.Doc;
163+
case EJ2DocumentEditor.FormatType.Rtf:
164+
return FormatType.Rtf;
165+
case EJ2DocumentEditor.FormatType.Txt:
166+
return FormatType.Txt;
167+
case EJ2DocumentEditor.FormatType.WordML:
168+
return FormatType.WordML;
169+
default:
170+
throw new NotSupportedException("DocIO does not support this file format.");
171+
}
172+
}
173+
174+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using CoreSample.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.Diagnostics;
4+
5+
namespace CoreSample.Controllers
6+
{
7+
public class HomeController : Controller
8+
{
9+
private readonly ILogger<HomeController> _logger;
10+
11+
public HomeController(ILogger<HomeController> logger)
12+
{
13+
_logger = logger;
14+
}
15+
16+
public IActionResult Index()
17+
{
18+
return View();
19+
}
20+
21+
public IActionResult Privacy()
22+
{
23+
return View();
24+
}
25+
26+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
27+
public IActionResult Error()
28+
{
29+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)