TX Text Control supports the encryption of Adobe PDF documents with encryption standards. Using the TXTextControl.SaveSettings class , you can define two passwords: The UserPassword to open the document and the MasterPassword for the document's access permissions. These permissions can be set using the SaveSettings.DocumentAccessPermissions property.

This demo uses the MVC version of TXTextControl.Web.MVC.DocumentViewer included in TX Text Control .NET Server for ASP.NET. The functionality is compatible across all supported platforms.

Version 32.0 SP4 (NuGet Version 32.4.0)

<div class="demo">
<div class="row">
<div class="col-4">
<form>
<div class="form-group">
<label for="userpassword">User Password</label>
<input autocomplete="off" value="password" type="text" class="form-control" id="userpassword" aria-describedby="User Password" placeholder="Enter the user password">
</div>
<div class="form-group">
<label for="masterpassword">Master Password</label>
<input autocomplete="off" value="textcontrol" type="text" class="form-control" id="masterpassword" aria-describedby="Master Password" placeholder="Enter the master password">
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="allowAuthoring">
<label class="form-check-label" for="allowAuthoring">
Allow Authoring
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="allowAuthoringFields">
<label class="form-check-label" for="allow>">
Allow AuthoringFields
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="allowContentAccessibility">
<label class="form-check-label" for="allowContentAccessibility>">
Allow Content Accessibility
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="allowDocumentAssembly">
<label class="form-check-label" for="allowDocumentAssembly>">
Allow Document Assembly
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="allowExtractContents">
<label class="form-check-label" for="allowExtractContents>">
Allow Extract Contents
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="allowGeneralEditing">
<label class="form-check-label" for="allowGeneralEditing>">
Allow General Editing
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="allowAllowHighLevelPrinting">
<label class="form-check-label" for="allowAllowHighLevelPrinting>">
Allow High Level Printing
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="allowAllowLowLevelPrinting">
<label class="form-check-label" for="allowAllowLowLevelPrinting>">
Allow Low Level Printing
</label>
</div>
</form>
<button onclick="CreateDocument()" class="btn mt-3 btn-success">Encrypt PDF</button>
<!-- Info area -->
<div class="alert alert-info mt-3" role="alert">
<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
<span class="alert-text"></span>
</div>
<div class="mb-5" id="buttonArea"></div>
</div>
<div class="col-8">
<div class="tx-container">
@Html.TXTextControl().DocumentViewer(settings => {
settings.Dock = DocumentViewerSettings.DockStyle.Fill;
settings.IsSelectionActivated = false;
settings.ShowThumbnailPane = false;
}).Render()
</div>
</div>
</div>
/div>
view raw test.cshtml hosted with ❤ by GitHub
[HttpPost]
public ActionResult PDFEncryptDocument(EncryptionSettings encryptionSettings) {
byte[] output = null;
byte[] outputNotEncrypted = null;
// create temporary ServerTextControl
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
tx.Create();
tx.Selection.Text = "This is a protected PDF with the following settings:\r\n\r\n";
tx.Selection.ListFormat.Type = ListType.Bulleted;
DocumentAccessPermissions permissions = DocumentAccessPermissions.None;
if (encryptionSettings.allowGeneralEditing) {
permissions = DocumentAccessPermissions.AllowGeneralEditing;
tx.Selection.Text = "Allow general editing\r\n";
}
if (encryptionSettings.allowExtractContents) {
permissions = permissions | DocumentAccessPermissions.AllowExtractContents;
tx.Selection.Text = "Allow extract contents\r\n";
}
if (encryptionSettings.allowDocumentAssembly) {
permissions = permissions | DocumentAccessPermissions.AllowDocumentAssembly;
tx.Selection.Text = "Allow document assembly\r\n";
}
if (encryptionSettings.allowContentAccessibility) {
permissions = permissions | DocumentAccessPermissions.AllowContentAccessibility;
tx.Selection.Text = "Allow content accessibility\r\n";
}
if (encryptionSettings.allowAllowHighLevelPrinting) {
permissions = permissions | DocumentAccessPermissions.AllowHighLevelPrinting;
tx.Selection.Text = "Allow high level printing\r\n";
}
if (encryptionSettings.allowAllowLowLevelPrinting) {
permissions = permissions | DocumentAccessPermissions.AllowLowLevelPrinting;
tx.Selection.Text = "Allow low level printing\r\n";
}
if (encryptionSettings.allowAuthoring) {
permissions = permissions | DocumentAccessPermissions.AllowAuthoring;
tx.Selection.Text = "Allow authoring\r\n";
}
if (encryptionSettings.allowAuthoringFields) {
permissions = permissions | DocumentAccessPermissions.AllowAuthoringFields;
tx.Selection.Text = "Allow authoring fields\r\n";
}
tx.Selection.ListFormat.Type = ListType.None;
TXTextControl.SaveSettings saveSettings = new SaveSettings() {
UserPassword = encryptionSettings.userpassword,
MasterPassword = encryptionSettings.masterpassword,
DocumentAccessPermissions = permissions
};
tx.Save(out output, BinaryStreamType.AdobePDF, saveSettings);
tx.Save(out outputNotEncrypted, BinaryStreamType.AdobePDF);
}
// return as Base64 encoded string
return Json(new { Encrypted = Convert.ToBase64String(output), Unencrypted = Convert.ToBase64String(outputNotEncrypted) });
}
view raw test.cs hosted with ❤ by GitHub
function CreateDocument() {
$('.alert .alert-text').text("Creating ZUGFeRD PDF. Please wait...");
$('.alert').show();
var form = {
userpassword: $("#userpassword").val(),
masterpassword: $("#masterpassword").val(),
allowAuthoring: $("#allowAuthoring").is(':checked'),
allowAuthoringFields: $("#allowAuthoringFields").is(':checked'),
allowContentAccessibility: $("#allowContentAccessibility").is(':checked'),
allowDocumentAssembly: $("#allowDocumentAssembly").is(':checked'),
allowExtractContents: $("#allowExtractContents").is(':checked'),
allowGeneralEditing: $("#allowGeneralEditing").is(':checked'),
allowAllowHighLevelPrinting: $("#allowAllowHighLevelPrinting").is(':checked'),
allowAllowLowLevelPrinting: $("#allowAllowLowLevelPrinting").is(':checked')
};
console.log(form);
var serviceURL = "@Url.Action("PDFEncryptDocument", "TX")";
// send document to controller
$.ajax({
type: "POST",
url: serviceURL,
data: {
encryptionSettings: form
},
success: successFunc,
error: errorFunc
});
}
function successFunc(data, status) {
console.log(data);
TXDocumentViewer.loadDocument(data.Unencrypted, "results.pdf");
createDownloadLink("results.pdf", data.Encrypted);
$('.alert').hide();
}
function errorFunc() {
alert("Error");
}
function createDownloadLink(name, content) {
$("#buttonArea").empty();
var dlink = document.createElement("a");
dlink.download = name;
dlink.textContent = "Download PDF";
dlink.classList.add("btn", "mt-3", "btn-primary");
dlink.href = "data:application/pdf;base64," + content;
console.log(dlink.href);
dlink.onclick = function (e) {
var that = this;
setTimeout(function () {
window.URL.revokeObjectURL(that.href);
}, 1500);
};
$("#buttonArea").append(dlink);
}
view raw test.js hosted with ❤ by GitHub

No code required.

Next Sample

© 2025 Copyright Text Control GmbH and Text Control, LLC. All rights reserved. We documents.