Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
||||||||||||||||||||||||||







Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
|
Gnostice Document Studio .NET |
Gnostice Document Studio Java |
Gnostice Document Studio Delphi |
eDocEngine VCL |
PDFtoolkit VCL |
StarDocs Web APIs |
Select the language for the code snippets
PDF files can be password-protected at two levels. One level is the protection put by the owner/author of the file to disallow certain operations on the PDF file (such as content extraction or high resolution printing) while allowing unrestricted opening/viewing of the file. This password is known as the permissions/owner/master password. The second level is the protection put by the owner/author of the file to allow opening/viewing of the PDF file only after supplying the correct password. This password is known as the open/user password. Applying or removing protection is accomplished by encrypting or decrypting the PDF file contents hence the operation is also termed as encryption or decryption.
StarDocs provides an easy-to-use API to apply or remove the protection at both the levels. It also allows setting up of permissions when applying the permissions password. In this article we'll show you how to accomplish all this using a single API. The values of the parameters will determine the operation performed. Passing an empty string for a password will remove the protection while passing a non-empty string will apply protection.
Note that StarDocs being a core PDF library, allows you to override the permissions put by the owner/author of the PDF file or even remove the permissions password just by supplying the user password. And, if the user password has not been set on the file, then even without actually supplying the user password, you can perform any operation on the file. Therefore, we urge you to use this feature responsibly.
After authentication and uploading of the PDF document you need to encrypt/decrypt, you will get the document URL. We pass in this URL to the encrypt API as shown below.
// Set up connection details
var stardocs = new Gnostice.StarDocs(
new Gnostice.ConnectionInfo(
'https://api.gnostice.com/stardocs/v1',
'<API Key>',
'<API Secret>'),
new Preferences(
// Whether to force full permissions on PDF files protected
// with a permissions/owner/master password
new DocPasswordSettings(true))
);
// Authenticate
stardocs.auth.loginApp()
.done(function(response) {
// Upload file
var selectedFile = document.getElementById('input').files[0];
stardocs.storage.upload(selectedFile)
.done(function(response) {
var documentUrl = response.documents[0].url;
// The existing open-password present on the document (if any)
var oldPassword = "oldpassword";
// The encryption level (if encrypting)
// Supported values are AES_128bit(default), RC4_128bit and RC4_40bit
var encryptionLevel = "AES_128bit";
// The new open-password (if encrypting)
// Pass an empty string if you want to remove the protection
var newOpenPassword = "newpassword";
// The new permissions-password (if encrypting)
// Pass an empty string if you want to remove the protection
var newPermissionsPassword = "newpermissionspassword";
// Specify the restrictions to be placed on the document
// Valid only if a non-empty newPermissionsPassword is supplied
var newPermissions = {
// True if allow accessibility. Default is false
allowAccessibility: true,
// True if allow document assembly. Default is false
allowAssembly: true,
// True if allow content copying. Default is false
allowCopy: true,
// True if allow form filling. Default is false
allowFormFill: true,
// True if allow high resolution printing. Default is false
allowHighResPrint: true,
// True if allow modifying annotations. Default is false
allowModifyAnnotations: true,
// True if allow content modification. Default is false
allowModifyContents: true,
// True if allow printing. Default is false
allowPrinting: true,
};
// Encrypt or decrypt the file
stardocs.docOperations.encrypt(documentUrl, oldPassword,
encryptionLevel, newOpenPassword,
newPermissionsPassword, newPermissions)
.done(function(response) {
var newDocUrl = response.documents[0].url;
// Do something with resultant document (newDocUrl)
// ...
});
});
});
// Set up connection details
StarDocs starDocs = new StarDocs(
new ConnectionInfo(
new Uri("https://api.gnostice.com/stardocs/v1"),
"<API Key>",
"<API Secret>"),
new Preferences(
// Force full permissions on PDF files protected
// with an permissions/owner/master password
new DocPasswordSettings(true))
);
// Authenticate
starDocs.Auth.loginApp();
// Input file
FileObject fileObjectInput = new FileObject(@"C:\Documents\Statement.pdf");
// The existing open-password present on the document (if any)
string oldPassword = "oldpassword";
// The encryption level (if encrypting, else set to None)
// Supported values are AES_128bit(default), RC4_128bit and RC4_40bit
PDFEncryptionLevel encryptionLevel = PDFEncryptionLevel.AES_128bit;
// The new open-password (if encrypting)
// Pass an empty string if you want to remove the protection
string newOpenPassword = "newpassword";
// The new permissions-password (if encrypting)
// Pass an empty string if you want to remove the protection
string newPermissionsPassword = "newpermissionspassword";
// Specify the restrictions to be placed on the document
// Valid only if a non-empty newPermissionsPassword is supplied
PDFDocPermissions newPermissions = new PDFDocPermissions();
// True if allow accessibility. Default is false
newPermissions.AllowAccessibility = true;
// True if allow document assembly. Default is false
newPermissions.AllowAssembly = true;
// True if allow content copying. Default is false
newPermissions.AllowCopy = true;
// True if allow form filling. Default is false
newPermissions.AllowFormFill = true;
// True if allow high resolution printing. Default is false
newPermissions.AllowHighResPrint = true;
// True if allow modifying annotations. Default is false
newPermissions.AllowModifyAnnotations = true;
// True if allow content modification. Default is false
newPermissions.AllowModifyContents = true;
// True if allow printing. Default is false
newPermissions.AllowPrinting = true;
// Encrypt or decrypt the file
DocObject docObjectOutput = starDocs.DocOperations.Encrypt(
fileObjectInput,
oldPassword,
encryptionLevel,
newOpenPassword,
newPermissionsPassword,
newPermissions);
// Do something with resultant document (docObjectOutput)
// ...
var
StarDocs: TgtStarDocsSDK;
FileObjectInput: TgtFileObject;
DocObjectOutput: TgtDocObject;
OldPassword: string;
EncryptionLevel: TgtPDFEncryptionLevel;
NewOpenPassword: string;
NewPermissionsPassword: string;
NewPermissions: TgtPDFDocPermissions;
begin
StarDocs := nil;
FileObjectInput := nil;
DocObjectOutput := nil;
try
// Set up connection details
StarDocs := TgtStarDocsSDK.Create(nil);
StarDocs.ConnectionSettings.ApiServerUri :=
'http://api.gnostice.com/stardocs/v1';
StarDocs.ConnectionSettings.ApiKey := '<API Key>';
StarDocs.ConnectionSettings.ApiSecret := '<API Secret>';
// Force full permissions on PDF files protected
// with an permissions/owner/master password
StarDocs.Preferences.DocPasswordSettings.ForceFullPermission := True;
// Authenticate
StarDocs.Auth.loginApp;
// Input file
FileObjectInput := TgtFileObject.Create('C:\Documents\Statement.pdf');
// The existing open-password present on the document (if any)
OldPassword := 'oldpassword';
// The encryption level (if encrypting, else set to pelNone)
// Supported values are pelAES_128bit(default), pelRC4_128bit
// and pelRC4_40bit
EncryptionLevel := TgtPDFEncryptionLevel.pelAES_128bit;
// The new open-password (if encrypting)
// Pass an empty string if you want to remove the protection
NewOpenPassword := 'newpassword';
// The new permissions-password (if encrypting)
// Pass an empty string if you want to remove the protection
NewPermissionsPassword := 'newpermissionspassword';
// Specify the restrictions to be placed on the document
// Valid only if a non-empty NewPermissionsPassword is supplied
NewPermissions := [
// Specify if allow accessibility
pdpAllowAccessibility,
// Specify if allow document assembly
pdpAllowAssembly,
// True if allow content copying
pdpAllowCopy,
// Specify if allow form filling
pdpAllowFormFill,
// Specify if allow high resolution printing
pdpAllowHighResPrint,
// Specify if allow modifying annotations
pdpAllowModifyAnnotations,
// Specify if allow content modification
pdpAllowModifyContents,
// Specify if allow printing
pdpAllowPrinting
];
// Encrypt or decrypt the file
DocObjectOutput := StarDocs.DocOperations.Encrypt(
FileObjectInput,
OldPassword,
EncryptionLevel,
NewOpenPassword,
NewPermissionsPassword,
NewPermissions);
// Do something with resultant document (DocObjectOutput)
// ...
finally
// Free objects
if Assigned(DocObjectOutput) then
FreeAndNil(DocObjectOutput);
if Assigned(FileObjectInput) then
FreeAndNil(FileObjectInput);
if Assigned(StarDocs) then
FreeAndNil(StarDocs);
end;
end;
// Set up connection details
StarDocs starDocs = new StarDocs(
new ConnectionInfo(
new java.net.URI("https://api.gnostice.com/stardocs/v1"),
"<API Key>",
"<API Secret>"),
new Preferences(
// Force full permissions on PDF files protected
// with an permissions/owner/master password
new DocPasswordSettings(true))
);
// Authenticate
starDocs.auth.loginApp();
// Input file
FileObject fileObjectInput = new FileObject("C:\\Documents\\Statement.pdf");
// The existing open-password present on the document (if any)
string oldPassword = "oldpassword";
// The encryption level (if encrypting, else set to None)
// Supported values are AES_128bit(default), RC4_128bit, RC4_40bit and None
PDFEncryptionLevel encryptionLevel = PDFEncryptionLevel.AES_128bit;
// The new open-password (if encrypting)
// Pass an empty string if you want to remove the protection
String newOpenPassword = "newpassword";
// The new permissions-password (if encrypting, else set to None)
// Pass an empty string if you want to remove the protection
String newPermissionsPassword = "newpermissionspassword";
// Specify the restrictions to be placed on the document
// Valid only if a non-empty newPermissionsPassword is supplied
PDFDocPermissions newPermissions = new PDFDocPermissions();
// True if allow accessibility. Default is false
newPermissions.setAllowAccessibility(true);
// True if allow document assembly. Default is false
newPermissions.setAllowAssembly(true);
// True if allow content copying. Default is false
newPermissions.setAllowCopy(true);
// True if allow form filling. Default is false
newPermissions.setAllowFormFill(true);
// True if allow high resolution printing. Default is false
newPermissions.setAllowHighResPrint(true);
// True if allow modifying annotations. Default is false
newPermissions.setAllowModifyAnnotations(true);
// True if allow content modification. Default is false
newPermissions.setAllowModifyContents(true);
// True if allow printing. Default is false
newPermissions.setAllowPrinting(true);
// Encrypt or decrypt the file
DocObject docObjectOutput = starDocs.docOperations.encrypt(
fileObjectInput,
oldPassword,
encryptionLevel,
newOpenPassword,
newPermissionsPassword,
newPermissions);
// Do something with resultant document (docObjectOutput)
// ...
That's it! This article showed how to use the Gnostice StarDocs API to encrypt/decrypt PDF files and how to set permissions on it.
---o0O0o---
| Our .NET Developer Tools | |
|---|---|
Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
| Our Delphi/C++Builder developer tools | |
|---|---|
Gnostice Document Studio DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
|
| Our Java developer tools | |
|---|---|
Gnostice Document Studio JavaMulti-format document-processing component suite for Java developers. |
PDFOne (for Java)A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications. |
| Our Platform-Agnostic Cloud and On-Premises APIs | |
|---|---|
StarDocsCloud-hosted and On-Premises REST-based document-processing and document-viewing APIs |
| Privacy | Legal | Feedback | Newsletter | Blog | Resellers | © 2002-2026 Gnostice Information Technologies Private Limited. All rights reserved. |