This demo uses the Word Processing Document API to encrypt a document with a password. Enter a password in the Password box, select the encryption type, and save the file to DOCX or DOC format.
@model AspNetCoreDemos.OfficeFileAPI.WordRTFDocumentEncryptionModel
@using DevExtreme.AspNet.Mvc
@{ Html.BeginForm("WordRTFDocumentEncryptionExportTo", "DocumentProtection", FormMethod.Post); }
<div class="demo-view-container">
@await Html.PartialAsync("WordRTFPreviewPartial", Model.PreviewModel)
</div>
<div class="options">
<div class="caption">Options</div>
<div class="option">
<div class="label">Password</div>
@(Html.DevExtreme().TextBoxFor(m => m.Password))
</div>
<div class="option">
<div class="label">Encryption Type</div>
@(Html.DevExtreme().SelectBoxFor(m => m.EncryptionType)
.DataSource(Html.GetEnumSelectList<DevExpress.XtraRichEdit.API.Native.EncryptionType >()
.Select(i => new { Value = int.Parse(i.Value), Text = i.Text }))
.ValueExpr("Value")
.DisplayExpr("Text")
)
</div>
<div class="option">
<div class="label">File Format</div>
@(Html.DevExtreme().SelectBoxFor(m => m.FileFormat)
.DataSource(Html.GetEnumSelectList<AspNetCoreDemos.OfficeFileAPI.RichEditEncryptionFileFormat>()
.Select(i => new { Value = int.Parse(i.Value), Text = i.Text }))
.ValueExpr("Value")
.DisplayExpr("Text")
)
</div>
<div class="option-buttons">
@(Html.DevExtreme().Button()
.Text("Download")
.Type(ButtonType.Default)
.StylingMode(ButtonStylingMode.Contained)
.UseSubmitBehavior(true)
)
</div>
</div>
@{ Html.EndForm(); }
@model AspNetCoreDemos.OfficeFileAPI.WordRTFPreviewModel
<iframe id="previewFrame" src="@Url.Action(Model.PreviewDocumentAction, Model.ControllerName)" height="@Model.IFrameSize" class="demo-preview-border" style="width:100%;box-sizing:border-box"></iframe>
<script type="text/javascript">
WordRTFPreview = {
basePath: '@Url.Action(Model.PreviewDocumentAction, Model.ControllerName)',
Update: function (param) {
var iframeElementName = "previewFrame";
var iframeElement = document.getElementById(iframeElementName);
if (!iframeElement)
return;
var additionalParams = "&" + new Date().valueOf();
if (param)
additionalParams = param;
iframeElement.src = this.basePath + "?" + additionalParams;
}
};
</script>
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
namespace AspNetCoreDemos.OfficeFileAPI {
public partial class DocumentProtectionController : OfficeDemoController {
public DocumentProtectionController(ILogger<DocumentProtectionController> logger, IWebHostEnvironment hostingEnvironment)
: base(logger, hostingEnvironment) {
}
protected const string TsaServerUriInvalidExceptionString = "ERROR: TSA server URI is invalid or server doesn't support SHA-256 hashing algorithm";
}
}
using DevExpress.XtraRichEdit;
using Microsoft.AspNetCore.Mvc;
using System.IO;
namespace AspNetCoreDemos.OfficeFileAPI {
public partial class DocumentProtectionController {
const string wordRTFDocumentEncryptionDefaultFile = "/Documents/DocumentForProtection.docx";
public IActionResult WordRTFDocumentEncryption() {
WordRTFDocumentEncryptionModel model = new WordRTFDocumentEncryptionModel();
return View(model);
}
public IActionResult WordRTFDocumentEncryptionExportTo(WordRTFDocumentEncryptionModel model) {
RichEditFileFormat fileFormat = model.FileFormat;
DocumentFormat format = WordRTFUtils.ConvertToFormat(fileFormat);
Stream stream = CreateDocumentStream(wordRTFDocumentEncryptionDefaultFile, format, (documentServer) => {
documentServer.Document.Encryption.Password = model.Password;
documentServer.Document.Encryption.Type = model.EncryptionType;
});
string contentType = WordRTFUtils.ConvertToContentType(fileFormat);
string fileExtension = WordRTFUtils.ConvertToFileExtension(fileFormat);
return CreateFileStreamResult(stream, contentType, fileExtension);
}
public IActionResult WordRTFDocumentEncryptionPreview(WordRTFDocumentEncryptionModel model) {
Stream stream = CreateDocumentStream(wordRTFDocumentEncryptionDefaultFile, DocumentFormat.Html, (documentServer) => {
documentServer.Document.Encryption.Password = model.Password;
documentServer.Document.Encryption.Type = model.EncryptionType;
});
return CreatePreviewResult(stream);
}
}
}
using DevExpress.XtraRichEdit.API.Native;
namespace AspNetCoreDemos.OfficeFileAPI {
public class WordRTFDocumentEncryptionModel : WordRTFModelBase {
public WordRTFDocumentEncryptionModel() {
PreviewModel.PreviewDocumentAction = "WordRTFDocumentEncryptionPreview";
PreviewModel.ControllerName = "DocumentProtection";
Password = "test";
EncryptionType = EncryptionType.Strong;
FileFormat = RichEditFileFormat.Docx;
}
public string Password { get; set; }
public EncryptionType EncryptionType { get; set; }
}
}
namespace AspNetCoreDemos.OfficeFileAPI {
public class WordRTFModelBase {
public WordRTFModelBase() {
PreviewModel = new WordRTFPreviewModel();
PreviewModel.OwnerPropertyName = "PreviewModel";
FileFormat = RichEditFileFormat.Rtf;
}
public RichEditFileFormat FileFormat { get; set; }
public WordRTFPreviewModel PreviewModel { get; internal set; }
}
public class WordRTFPreviewModel {
public WordRTFPreviewModel() {
}
public string OwnerPropertyName { get; set; }
public string PreviewDocumentAction { get; set; }
public string ControllerName { get; set; }
public int IFrameSize { get; set; } = 452;
}
}