//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- #include #include #include #include #include #include #include #include using namespace std; using namespace pdftron; using namespace SDF; using namespace PDF; using namespace Filters; //--------------------------------------------------------------------------------------- // This sample shows encryption support in PDFNet. The sample reads an encrypted document and // sets a new SecurityHandler. The sample also illustrates how password protection can // be removed from an existing PDF document. //--------------------------------------------------------------------------------------- int main(int argc, char *argv[]) { int ret = 0; PDFNet::Initialize(); PDFNet::SetResourcesPath("../../../resources"); // Relative path to the folder containing test files. string input_path = "../../TestFiles/"; string output_path = "../../TestFiles/Output/"; // Example 1: // secure a document with password protection and // adjust permissions try { // Open the test file cout << "Securing an existing document ..." << endl; PDFDoc doc((input_path + "fish.pdf").c_str()); // 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) // Optional { cout << "Replacing the content stream, use Flate compression..." << endl; // Get the page dictionary using the following path: trailer/Root/Pages/Kids/0 Obj 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. StdFile embed_file((input_path + "my_stream.txt").c_str(), StdFile::e_read_mode ); FilterReader mystm(embed_file); page_dict.Put("Contents", doc.CreateIndirectStream(mystm, FlateEncode(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'. SecurityHandler new_handler; // Set a new password required to open a document const char* user_password="test"; 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. cout << "Saving modified file..." << endl; doc.Save((output_path + "secured.pdf").c_str(), 0, NULL); } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } // Example 2: // Opens the encrypted document and removes all of // its security. try { PDFDoc doc((output_path + "secured.pdf").c_str()); //If the document is encrypted prompt for the password if (!doc.InitSecurityHandler()) { bool success=false; cout << "The password is: test" << endl; for(int count=0; count<3;count++) { char password[255]; cout << "A password required to open the document." << endl << "Please enter the password:"; cin.getline(password,255); if(doc.InitStdSecurityHandler(password,strlen(password))) { success=true; cout << "The password is correct." << endl; break; } else if(count<3) { cout << "The password is incorrect, please try again" << endl; } } if(!success) { cout << "Document authentication error...." << endl; ret = 1; PDFNet::Terminate(); return ret; } } //remove all security on the document doc.RemoveSecurity(); doc.Save((output_path + "not_secured.pdf").c_str(), 0, NULL); } catch(Common::Exception& e) { cout << e << endl; ret = 1; } catch(...) { cout << "Unknown Exception" << endl; ret = 1; } cout << "Tests completed." << endl; PDFNet::Terminate(); return ret; }