Example #1
0
// Example 1:
// secure a PDF document with password protection and adjust permissions
// Open the test file
echo "Securing an existing document ...\n";
$doc = new PDFDoc($input_path . "fish.pdf");
$doc->InitSecurityHandler();
// Perform some operation on the document. In this case we use low level SDF API
// to replace the content stream of the first page with contents of file 'my_stream.txt'
if (true) {
    echo "Replacing the content stream, use Flate compression...\n";
    // Get the page dictionary using the following path: trailer/Root/Pages/Kids/0
    $page_dict = $doc->GetTrailer()->Get("Root")->Value()->Get("Pages")->Value()->Get("Kids")->Value()->GetAt(0);
    // Embed a custom stream (file mystream.txt) using Flate compression.
    $embed_file = new MappedFile($input_path . "my_stream.txt");
    $mystm = new FilterReader($embed_file);
    $page_dict->Put("Contents", $doc->CreateIndirectStream($mystm, new FlateEncode(new Filter())));
}
//encrypt the document
// Apply a new security handler with given security settings.
// In order to open saved PDF you will need a user password 'test'.
$new_handler = new SecurityHandler();
// Set a new password required to open a document
$user_password = "******";
$new_handler->ChangeUserPassword($user_password);
// Set Permissions
$new_handler->SetPermission(SecurityHandler::e_print, true);
$new_handler->SetPermission(SecurityHandler::e_extract_content, false);
// Note: document takes the ownership of new_handler.
$doc->SetSecurityHandler($new_handler);
// Save the changes.
echo "Saving modified file...\n";