document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. import com.pdftron.common.PDFNetException;
  6. import com.pdftron.filters.FilterReader;
  7. import com.pdftron.filters.FlateEncode;
  8. import com.pdftron.filters.MappedFile;
  9. import com.pdftron.pdf.*;
  10. import com.pdftron.sdf.*;
  11.  
  12.  
  13. //---------------------------------------------------------------------------------------
  14. // This sample shows encryption support in PDFNet. The sample reads an encrypted document and
  15. // sets a new SecurityHandler. The sample also illustrates how password protection can
  16. // be removed from an existing PDF document.
  17. //---------------------------------------------------------------------------------------
  18. public class EncTest {
  19.     public static void main(String[] args) {
  20.         PDFNet.initialize();
  21.  
  22.         // Relative path to the folder containing test files.
  23.         String input_path = "../../TestFiles/";
  24.         String output_path = "../../TestFiles/Output/";
  25.  
  26.         // Example 1:
  27.         // secure a document with password protection and
  28.         // adjust permissions
  29.  
  30.         try {
  31.             // Open the test file
  32.             System.out.println("Securing an existing document ...");
  33.             PDFDoc doc = new PDFDoc((input_path + "fish.pdf"));
  34.             doc.initSecurityHandler();
  35.  
  36.             // Perform some operation on the document. In this case we use low level SDF API
  37.             // to replace the content stream of the first page with contents of file \'my_stream.txt\'
  38.             if (true)  // Optional
  39.             {
  40.                 System.out.println("Replacing the content stream, use flate compression...");
  41.  
  42.                 // Get the page dictionary using the following path: trailer/Root/Pages/Kids/0
  43.                 Obj page_dict = doc.getTrailer().get("Root").value()
  44.                         .get("Pages").value()
  45.                         .get("Kids").value()
  46.                         .getAt(0);
  47.  
  48.                 // Embed a custom stream (file mystream.txt) using Flate compression.
  49.                 MappedFile embed_file = new MappedFile((input_path + "my_stream.txt"));
  50.                 FilterReader mystm = new FilterReader(embed_file);
  51.                 page_dict.put("Contents",
  52.                         doc.createIndirectStream(mystm,
  53.                                 new FlateEncode(null)));
  54.             }
  55.  
  56.             //encrypt the document
  57.  
  58.             // Apply a new security handler with given security settings.
  59.             // In order to open saved PDF you will need a user password \'test\'.
  60.             SecurityHandler new_handler = new SecurityHandler();
  61.  
  62.             // Set a new password required to open a document
  63.             String user_password = "test";
  64.             new_handler.changeUserPassword(user_password);
  65.  
  66.             // Set Permissions
  67.             new_handler.setPermission(SecurityHandler.e_print, true);
  68.             new_handler.setPermission(SecurityHandler.e_extract_content, false);
  69.  
  70.             // Note: document takes the ownership of new_handler.
  71.             doc.setSecurityHandler(new_handler);
  72.  
  73.             // Save the changes.
  74.             System.out.println("Saving modified file...");
  75.             doc.save((output_path + "secured.pdf"), SDFDoc.SaveMode.NO_FLAGS, null);
  76.             // output PDF doc
  77.             doc.close();
  78.         } catch (PDFNetException e) {
  79.             e.printStackTrace();
  80.         }
  81.  
  82.         // Example 2:
  83.         // Opens the encrypted document and removes all of
  84.         // its security.
  85.         try {
  86.             PDFDoc doc = new PDFDoc((output_path + "secured.pdf"));
  87.  
  88.             //If the document is encrypted prompt for the password
  89.             if (!doc.initSecurityHandler()) {
  90.                 boolean success = false;
  91.                 System.out.println("The password is: test");
  92.                 for (int count = 0; count < 3; count++) {
  93.                     BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
  94.                     System.out.println("A password required to open the document.");
  95.                     System.out.print("Please enter the password: ");
  96.                     String password = r.readLine();
  97.                     if (doc.initStdSecurityHandler(password)) {
  98.                         success = true;
  99.                         System.out.println("The password is correct.");
  100.                         break;
  101.                     } else if (count < 3) {
  102.                         System.out.println("The password is incorrect, please try again");
  103.                     }
  104.                 }
  105.                 if (!success) {
  106.                     System.out.println("Document authentication error....");
  107.                     PDFNet.terminate();
  108.                 }
  109.             }
  110.  
  111.             //remove all security on the document
  112.             doc.removeSecurity();
  113.             doc.save(output_path + "not_secured.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
  114.             // output PDF doc
  115.             doc.close();
  116.         } catch (Exception e) {
  117.             e.printStackTrace();
  118.         }
  119.  
  120.         System.out.println("Test completed.");
  121.         PDFNet.terminate();
  122.     }
  123. }
  124.  
');