Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.1401
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DocumentEditorLibrary", "DocumentEditorLibrary/DocumentEditorLibrary.csproj", "{483F283D-4345-4070-BFDD-8A885B3C80F8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{483F283D-4345-4070-BFDD-8A885B3C80F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{483F283D-4345-4070-BFDD-8A885B3C80F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{483F283D-4345-4070-BFDD-8A885B3C80F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{483F283D-4345-4070-BFDD-8A885B3C80F8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0FA40C22-755B-4BBF-98B4-C573F728F80D}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="*" />
<PackageReference Include="Syncfusion.EJ2.WordEditor.AspNet.Core" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.");
}
}
}
}
30 changes: 30 additions & 0 deletions Python/Python Webservice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Integrated .Net Core Library in python

We have created Documenteditor utility with following functionalities:

* [Import](../README.md/#import)
* [ExportSfdt](../README.md/#exportsfdt)
* [SystemClipboard](../README.md/#systemclipboard)
* [RestrictEditing](../README.md/#restrictediting)


Document editor does not have native support to open a document using Python. We can use the .NET Core Wrapper as a Python backend.

1. **Install required `dependencies`:**
```
python -m pip install flask
python -m pip install flask-cors
python -m pip install pythonnet
```

2. **Run (buildNetProject.bat) bat file**
3. **Open cmd and run `"python app.py"`**


Run the sample and set the serviceUrl as the running URL.

**For example,**
container.serviceUrl=`'http://127.0.0.1:5000/'`;


>**Note :** This is a development server. It should not be utilized for production deployment. Instead, employ a production-grade WSGI server.
81 changes: 81 additions & 0 deletions Python/Python Webservice/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from flask import Flask, json, request
from flask_cors import CORS #import CORS from flask_cors

import clr #import clr from pythonnet
import os

app = Flask(__name__)
CORS(app) #enable CORS on the app

# get the current working directory
current_working_directory = os.getcwd()

#load our dll file(mine is in my C:/ folder)
clr.AddReference(current_working_directory + "/.NET Standard Wrapper Library/DocumentEditorLibrary/bin/Release/netstandard2.0/publish/DocumentEditorLibrary.dll")
clr.AddReference(current_working_directory + "/.NET Standard Wrapper Library/DocumentEditorLibrary/bin/Release/netstandard2.0/publish/Syncfusion.EJ2.DocumentEditor.dll")
clr.AddReference(current_working_directory + "/.NET Standard Wrapper Library/DocumentEditorLibrary/bin/Release/netstandard2.0/publish/Syncfusion.DocIO.Portable.dll")
clr.AddReference(current_working_directory + "/.NET Standard Wrapper Library/DocumentEditorLibrary/bin/Release/netstandard2.0/publish/Syncfusion.Compression.Portable.dll")
clr.AddReference(current_working_directory + "/.NET Standard Wrapper Library/DocumentEditorLibrary/bin/Release/netstandard2.0/publish/Syncfusion.OfficeChart.Portable.dll")
clr.AddReference(current_working_directory + "/.NET Standard Wrapper Library/DocumentEditorLibrary/bin/Release/netstandard2.0/publish/Syncfusion.Licensing.dll")
clr.AddReference(current_working_directory + "/.NET Standard Wrapper Library/DocumentEditorLibrary/bin/Release/netstandard2.0/publish/Newtonsoft.Json.dll")
clr.AddReference(current_working_directory + "/.NET Standard Wrapper Library/DocumentEditorLibrary/bin/Release/netstandard2.0/publish/System.Text.Encoding.CodePages.dll")

#import our Documenteditor class from our C# namespace DocumentEditorLibrary
from DocumentEditorLibrary import Documenteditor
from Syncfusion.Licensing import SyncfusionLicenseProvider

# Register Syncfusion license
SyncfusionLicenseProvider.RegisterLicense("Enter your license key here")

docEditor = Documenteditor() #create our Documenteditor object

@app.route('/Import', methods=['POST'])
def importDocument():
if 'files' in request.files:
files = request.files['files']
# Get the stream data
stream_data = files.stream.read()
# Get the file name
file_name = files.filename
# Calling our Import method from our Documenteditor class which will return the SFDT string
return docEditor.Import(stream_data, file_name)
else:
return ""

@app.route('/SystemClipboard', methods=['POST'])
def systemClipboard():
# Get the SFDT data from the request
content = request.json['content']
# Get the type from the request
type = request.json['type']
# Calling our SystemClipboard method from our Documenteditor class which will return the SFDT string
return docEditor.SystemClipboard(content, type)

@app.route('/RestrictEditing', methods=['POST'])
def restrictEditing():
passwordBase64 = request.json['passwordBase64']
slatBase64 = request.json['saltBase64']
spinCount = request.json['spinCount']
# Calling our RestrictEditing method from our Documenteditor class which will return the array of System.String represents the password and salt value.
jsonString = docEditor.RestrictEditing(passwordBase64, slatBase64, spinCount)
return json.loads(jsonString)

@app.route('/Save', methods=['POST'])
def save():
# Get the SFDT data from the request
content = request.json['content']
# Get the file name from the request
fileName = request.json['fileName']
# Calling our Save method from our Documenteditor class which will save the document in the given file name.
result = docEditor.Save(content, fileName)
print(result)
return result


@app.route("/")
def home():
return "Flask Web API for DocumentEditor!"

if __name__ == "__main__":
app.run(debug=True) # http://localhost:5000/
# app.run(host='', port=5001, debug=True) # http://localhost:5001/
3 changes: 3 additions & 0 deletions Python/Python Webservice/buildNetProject.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dotnet build ".NET Standard Wrapper Library/DocumentEditorLibrary.sln" -c Release

dotnet publish ".NET Standard Wrapper Library/DocumentEditorLibrary.sln" -c Release
6 changes: 5 additions & 1 deletion Python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ You can make use of this service in order to paste system clipboard data by pres

## ExportSfdt

You can export the SFDT string Doc, DOCX, RTF, Txt, WordML, HTML formats by using this API in server side.
You can export the SFDT string Doc, DOCX, RTF, Txt, WordML, HTML formats by using this API in server side.

## RestrictEditing

You can make use of this service in order to encrypt/decrypt protected content.