Cookbook

Protecting DOCX and PDF documents

You can protect documents against unauthorized accesses through many ways: from a basic restriction that allows just reading it but not changing it to more advanced ones like encrypting with passwords the content.

javadocx provides the necessary methods to add the kind of protection you may want for DOCX and PDF documents.

Basic protection

The simplest kind of protection of a DOCX lets you block a document against changes, setting it as a “read-only” file. The setMarkAsFinal method activates the restriction in any DOCX, being either documents made from scratch or templates. E.g.: to add Basic protection to a new document, execute this code:

This is not an effective protection because when Word opens the document, it shows this message: ‘MARKED AS FINAL An author has marked this document as final to discourage editing’. Unfortunately, any user can modify the content simply by pressing the button ‘Edit anyway’ that appears beside that warning. LibreOffice and OpenOffice applications do not even support this protection, so the document could be edited straightaway.

Protecting the document with a password (Premium licenses only)

A step forward in DOCX security is to run the protectDocx method, available in the CryptoPHPDOCX class. This functionality lets you limit the documents to read-only, comment-only, revision-only or form-only modes. Access to full edition would be granted by a password.

E.g.: To protect a document as read-only (the default choice) with 'javadocx' as password, run the following code:

This type of protection is safer than the basic one. Nevertheless, a user with enough technical skills could break it, and applications like LibreOffice and OpenOffice don't support it.

To remove this protection you just need to extract the content of the DOCX (it's a ZIP) and edit the word/setting.xml file by deleting the w:documentProtection tag. Then, just by compressing the files and folders in a new ZIP with the DOCX extension is all that it takes to have the document ready for changes.

Protecting the content PDF files is quite a different matter. The protectPDF method allows to block the access by password with the 'print', 'modify', 'copy', 'annot-forms', 'fill-forms', 'extract', 'assemble' and 'print-high' accesses.

This way, you can forbid the access to print with the code:

Warning

This PDF protection only works if the document is opened with a compatible reader. Other way, the protection is not applied.

Encryption (Premium licenses only)

Encrypting is the only way to achieve complete and safe protection for documents. javadocx allows to encrypt DOCX files with the encryptDocx method and PDFs with encryptPDF. Both methods allow to block access to documents, and only permit to read it if the user types the password that was used for the encryption.

LibreOffice and OpenOffice are compatible with DOCX files encryption. Users may read the content only if they know the password.

To encrypt a DOCX with a password, apply this code:

To encrypt a PDF, execute: