SetProtection: Set Document Protection [ 35 ]
Set whether target PDF should be protected or not. It is recalled that a PDF document may be protected at two distinct levels :
- User Level: upon opening a PDF document, users are required to key a password to visualize it.
- Owner Level: no password is required to open a document for reading. However, it is required to key a password in case a user wishes to modify this PDF document.
- Permissions is an integer the value of which is meaningful for owner level protection. If permissions are set to 0, PDF will not be owner protected. Otherwise, by ORing the following values, a program may further specify which functions are allowed.
- 0x04 → print allowed
- 0x08 → modifications allowed
- 0x10 → copy-paste allowed
- 0x20 → comments allowed
- User level password: this is a character string pointer which specifies a password which will be required for opening the PDF document.
- Owner level password: this is a character string pointer which specifies a password which will be required to modify either the PDF document proper or its properties.
- If permissions parameter value is set to zero, this password parameter will be ignored.
- If permissions parameter value is NOT set to zero, this third parameter will specify a passwsord required to modify the PDF being built.
- If permissions parameter value is NOT set to zero and this third parameter is an empty string, FPDF4ZOS will automatically generate an inner password. This generally means that the PDF being built will NOT be modifiable.
- PDF files may be protected at both user and owner levels.
- Whenever protection is required, either at user or owner level, all streams within PDF are encrypted. Besides, PDF specification supports 2 kinds of encryption:
- 40 bits encryption
- 256 bit encryption.
C calling syntax
void SetProtection (int permissions, char *user_pswd, char *owner_pswd)
Cobol calling syntax
Call "SetProtection" using Perms, user_pswd, owner_pswd.
C sample code
#include "fpdf.h" static char f_ini ??(??) = ??< "ini??/??/demofpdf.ini" ??>; static char f_out ??(??) = ??< "pdf??/??/demo_d.pdf" ??>; static char buf0 ??(128??); /* ----------------------------------------------------------------- */ int main (int argc, char *argv ) ??< int ii, jj; char *ptr; NewPdfy (f_ini, 1); jj = P_MODIFY + P_ANNOT_FORMS; SetProtection (jj, "USER-PSWD", "OWNER-PSWD"); for (ii = 0; ii < 9; ii ++) begin AddPage("L"); jj = PageNo (); sprintf (buf0, "numbers%d", jj); if ((ii & 1) == 0) ptr = ""; else ptr = "http://www.cnn.com"; Image (buf0, 50, 20, 200, 0, "jpg", ptr); sprintf (buf0, "Page %d", jj); Bookmark (buf0, 0, 0); end Output (f_out); ??>
Description of sample code
This piece of code illustrates various functions found in FPDF4ZOS interface. It opens a PDF context and specifies that the document ought to be protected at both levels: - It requires from users to key the string USER-PSWD in order to be able to read the document => encryption at user level. - It further encodes the document at owner level, forbidding Print and copy-paste features. In order to change these protections, a user should key password OWNER-PSWD. Program then performs a loop on 9 pages in each of which a different image is inserted. Note that in the even pages, the image carries a link to web site www.cnn.com. The following functions are used in this piece: - NewPdfy() --> Initialisation of a pdf context - AddPage() --> As it says - SetProtection() --> Instruct that PDF is to be protected at user and owner levels - Image() --> Insert an image into a PDF document - Bookmark() --> Insert an bookmark into a PDF document - PageNo() --> Retrieve current page number while building PDF document - FlushPdf() --> Output PDF file taking into account all the instructions invoked since NewPdfy or previous FlushPdf
See sample source code in 'C' as well as PDF execution result - NB: For encrypted PDFs, user password is USER-PSWD