The Apryse Custom Security Handler provides a means to encrypt files in a way that cannot be decrypted by other applications. This makes it especially suitable for Digital Rights Management (DRM) use cases. This is an alternative to defining your own custom encryption method. In particular using the Apryse Custom Security Handler is much simpler as it comes built-in with Apryse SDK.
Like other encryption methods a password is required to encrypt and decrypt the document. This custom security handler also requires the application choose a unique unsigned integer custom id (typically one per application) of their choice to encrypt and decrypt the document. Using this unique id provides an extra level of security as even other applications based on Apryse SDK will not typically be configured to open files secured with that particular id.
The first step before encrypting is to choose an application custom id. A typical way to create this is to use a random generator to create a 32-bit number. This number would then be used throughout your document workflow.
Once that is done, the document can be encrypted:
C# C++ Java JavaScript Obj-C PHP Python Ruby
1 // Open the document
2 PDFDoc doc = new PDFDoc (filename);
3
4 // Create Apryse custom security handler with a custom id. Replace this with your own integer
5 PDFTronCustomSecurityHandler custom_handler = new PDFTronCustomSecurityHandler (my_custom_id);
6
7 // Set a new password required to open a document
8 custom_handler. ChangeUserPassword (my_password);
9
10 // Note: document takes the ownership of custom_handler.
11 doc. SetSecurityHandler (custom_handler);
12
13 // Save the encrypted document
14 doc. Save (output_filename, SDFDoc.SaveOptions.e_remove_unused);
1 // Open the document
2 PDFDoc doc ( filename );
3
4 // Create Apryse custom security handler with a custom id. Replace this with your own integer
5 SDF :: PDFTronCustomSecurityHandler custom_handler ( my_custom_id );
6
7 // Set a new password required to open a document
8 custom_handler. ChangeUserPassword (my_password);
9
10 // Note: document takes the ownership of custom_handler.
11 doc. SetSecurityHandler (custom_handler);
12
13 // Save the encrypted document
14 doc. Save (output_filename, SDFDoc :: e_remove_unused, 0 );
1 // Open the document
2 PDFDoc doc = new PDFDoc (filename);
3
4 // Create Apryse custom security handler with a custom id. Replace this with your own integer
5 PDFTronCustomSecurityHandler custom_handler = new PDFTronCustomSecurityHandler (my_custom_id);
6
7 // Set a new password required to open a document
8 custom_handler. changeUserPassword (my_password);
9
10 // Note: document takes the ownership of custom_handler.
11 doc. setSecurityHandler (custom_handler);
12
13 // Save the encrypted document
14 doc. save (output_filename, SDFDoc.SaveMode.e_remove_unused);
1 async function main () {
2 // Open the document
3 const doc = await PDFNet.PDFDoc. createFromFilePath (filename);
4
5 // Create Apryse custom security handler with a custom id. Replace this with your own integer
6 const custom_handler = await PDFNet.PDFTronCustomSecurityHandler. create (my_custom_id);
7
8 // Set a new password required to open a document
9 custom_handler. changeUserPasswordUString (my_password);
10
11 // Note: document takes the ownership of custom_handler.
12 doc. setSecurityHandler (custom_handler);
13
14 // Save the encrypted document
15 await doc. save (output_filename, PDFNet.SDFDoc.SaveOptions.e_remove_unused);
16 }
17 PDFNet. runWithCleanup (main);
1 PTPDFDoc * doc = [[PTPDFDoc alloc ] initWithFilepath : filename];
2
3 // Create Apryse custom security handler with a custom id. Replace this with your own integer
4 PTSecurityHandler * custom_handler = [[PTPDFTronCustomSecurityHandler alloc ] initWithCustom_id : my_custom_id];
5
6 // Set a new password required to open a document
7 [custom_handler ChangeUserPassword : my_password];
8
9 // Note: document takes the ownership of custom_handler.
10 [doc SetSecurityHandler : custom_handler];
11
12 // Save the encrypted document
13 [doc SaveToFile : output_filename flags : e_ptremove_unused];
1 // Open the document
2 $doc = new PDFDoc ($filename);
3
4 // Create Apryse custom security handler with a custom id. Replace this with your own integer
5 $custom_handler = new PDFTronCustomSecurityHandler ($my_custom_id);
6
7 // Set a new password required to open a document
8 $custom_handler -> ChangeUserPassword ($my_password);
9
10 // Note: document takes the ownership of custom_handler.
11 $doc -> SetSecurityHandler ($custom_handler);
12
13 // Save the encrypted document
14 $doc -> Save ($output_filename, SDFDoc :: e_remove_unused );
1 # Open the document
2 doc = PDFDoc(filename)
3
4 # Create Apryse custom security handler with a custom id. Replace this with your own integer
5 custom_handler = PDFTronCustomSecurityHandler(my_custom_id)
6
7 # Set a new password required to open a document
8 custom_handler.ChangeUserPassword(my_password)
9
10 # Note: document takes the ownership of custom_handler.
11 doc.SetSecurityHandler(custom_handler)
12
13 # Save the encrypted document
14 doc.Save(output_filename, SDFDoc.e_remove_unused)
1 # Open the document
2 doc = PDFDoc . new (filename)
3
4 # Create Apryse custom security handler with a custom id. Replace this with your own integer
5 custom_handler = PDFTronCustomSecurityHandler . new (my_custom_id)
6
7 # Set a new password required to open a document
8 custom_handler. ChangeUserPassword (my_password)
9
10 # Note: document takes the ownership of custom_handler.
11 doc. SetSecurityHandler (custom_handler)
12
13 # Save the encrypted document
14 doc. Save (output_filename, SDFDoc :: E_remove_unused )
Encrypt and Decrypt PDF Files Full sample code which illustrates some of our encryption support.
The same application custom id and password using during encryption are required for decryption. Failing to provide the correct password or application custom id will prevent opening the encrypted document.
To decrypt a PDF with Apryse Custom security:
C# C++ Java JavaScript Obj-C PHP Python Ruby
1 // Register the Apryse Custom Security handler with the same custom id used in encryption.
2 // Calling this function is a requirement to load files encrypted with PDFTronCustomSecurityHandler.
3 PDFNet. AddPDFTronCustomHandler (my_custom_id);
4
5 PDFDoc doc_enc = new PDFDoc (filename);
6 if (doc_enc. InitStdSecurityHandler (my_password)) {
7 // The password is correct! Document can now be used for reading and editing
8 }
1 // Register the Apryse Custom Security handler with the same custom id used in encryption.
2 // Calling this function is a requirement to load files encrypted with PDFTronCustomSecurityHandler.
3 PDFNet :: AddPDFTronCustomHandler (my_custom_id);
4
5 PDFDoc doc_enc ( filename );
6 if (doc_enc. InitStdSecurityHandler (my_password)) {
7 // The password is correct! Document can now be used for reading and editing
8 }
1 // Register the Apryse Custom Security handler with the same custom id used in encryption.
2 // Calling this function is a requirement to load files encrypted with PDFTronCustomSecurityHandler.
3 PDFNet. addPDFTronCustomHandler (my_custom_id);
4
5 PDFDoc doc_enc = new PDFDoc (filename);
6 if (doc_enc. initStdSecurityHandler (my_password)) {
7 // The password is correct! Document can now be used for reading and editing
8 }
1 async function main () {
2 // Register the Apryse Custom Security handler with the same custom id used in encryption.
3 // Calling this function is a requirement to load files encrypted with PDFTronCustomSecurityHandler.
4 await PDFNet. addPDFTronCustomHandler (my_custom_id);
5
6 const doc_enc = await PDFNet.PDFDoc. createFromFilePath (filename);
7 if ( await doc_enc. initStdSecurityHandlerUString (my_password)) {
8 // The password is correct! Document can now be used for reading and editing
9 }
10 }
11 PDFNet. runWithCleanup (main);
1 // Register the Apryse Custom Security handler with the same custom id used in encryption.
2 // Calling this function is a requirement to load files encrypted with PDFTronCustomSecurityHandler.
3 [PTPDFNet AddPDFTronCustomHandler : my_custom_id];
4
5 PTPDFDoc * doc_enc = [[PTPDFDoc alloc ] initWithFilepath : filename];
6 if ([doc_enc InitStdSecurityHandler : my_password])
7 {
8 // The password is correct! Document can now be used for reading and editing
9 }
1 // Register the Apryse Custom Security handler with the same custom id used in encryption.
2 // Calling this function is a requirement to load files encrypted with PDFTronCustomSecurityHandler.
3 PDFNet :: AddPDFTronCustomHandler ($my_custom_id);
4
5 $doc_enc = new PDFDoc ($filename);
6 if ($doc_enc -> InitStdSecurityHandler ($my_password)) {
7 // The password is correct! Document can now be used for reading and editing
8 }
1 # Register the Apryse Custom Security handler with the same custom id used in encryption.
2 # Calling this function is a requirement to load files encrypted with PDFTronCustomSecurityHandler.
3 PDFNet.AddPDFTronCustomHandler(my_custom_id)
4
5 doc_enc = PDFDoc(filename)
6 if doc_enc.InitStdSecurityHandler(my_password):
7 # The password is correct! Document can now be used for reading and editing
1 # Register the Apryse Custom Security handler with the same custom id used in encryption.
2 # Calling this function is a requirement to load files encrypted with PDFTronCustomSecurityHandler.
3 PDFNet . AddPDFTronCustomHandler (my_custom_id)
4
5 doc_enc = PDFDoc . new (filename)
6 if doc_enc. InitStdSecurityHandler (my_password)
7 # The password is correct! Document can now be used for reading and editing
8 end
Encrypt and Decrypt PDF Files Full sample code which illustrates some of our encryption support.