Some test text!

PDFTron Custom Security Handlerkeyboard_arrow_down

PDFTron Custom Security Handler in Ruby

The PDFTron 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 PDFTron Custom Security Handler is much simpler as it comes built-in with PDFTron 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 PDFTron SDK will not typically be configured to open files secured with that particular id.

linkEncrypting a PDFTron Custom secured document using the password and application custom 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:

# Open the document
doc =

# Create PDFTron custom security handler with a custom id. Replace this with your own integer
custom_handler =

# Set a new password required to open a document

# Note: document takes the ownership of custom_handler.

# Save the encrypted document
doc.Save(output_filename, SDFDoc::E_remove_unused)

linkDecrypting a PDFTron Custom secured document using the password and application custom id

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 PDFTron Custom security:

# Register the PDFTron Custom Security handler with the same custom id used in encryption.
# Calling this function is a requirement to load files encrypted with PDFTronCustomSecurityHandler.

doc_enc =
if doc_enc.InitStdSecurityHandler(my_password)
  # The password is correct! Document can now be used for reading and editing

For more details, please refer to Example 3 in Encryption or EncTest in Samples folder in download packages.

Get the answers you need: Support


Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.