Gnostice PDFOne
Pro. Ed. v5.0.0


com.gnostice.pdfone
Interface PdfPasswordHandler


public interface PdfPasswordHandler

This interface provides an event that is raised when a password-protected document is being read.

import java.io.IOException;

import com.gnostice.pdfone.PDFOne;
import com.gnostice.pdfone.PdfDocument;
import com.gnostice.pdfone.PdfEncryption;
import com.gnostice.pdfone.PdfException;
import com.gnostice.pdfone.PdfPasswordHandler;
import com.gnostice.pdfone.PdfReader;
import com.gnostice.pdfone.PdfWriter;

public class PdfPasswordHandler_Example 
  implements PdfPasswordHandler
{
 // Activates the PDFOne.jar component
 static
 {
  PDFOne.activate("T95VZE:W8HBPVA:74VQ8QV:LO4V8",
                  "9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10");
 }

 public static void main(String[] args) 
   throws PdfException, IOException
 {
  createEncryptedDoc();
  openEncryptedDoc();
 }

 // Creates an encrypted document with 
 // owner and user passwords
 private static void createEncryptedDoc() 
  throws PdfException, IOException
 {
  // Creates a new PdfDocument object
  PdfWriter writer = 
     PdfWriter.fileWriter("encrypted_doc.pdf");
  PdfDocument document = new PdfDocument(writer);

  // Creates a PdfEncryption object with current (default)
  // encryption settings of the PdfDocument object
  PdfEncryption crypt = document.getEncryptor();

  // Sets owner and user passwords 
  crypt.setOwnerPwd("gnostice");
  crypt.setUserPwd("pdfone");

  // Sets encryption key length to 128 bits
  crypt.setLevel(PdfEncryption.LEVEL_128_BIT);

  // Set user access permissions
  crypt.setPermissions(
    PdfEncryption.AllowPrinting 
    | PdfEncryption.AllowHighResPrint);

  // Writes some content to the document 
  document.writeText(
    "This document is encrypted. The user password is " 
     + "\"pdfone\" and the owner password is \"gnostice.\"");

  // Specifies new encryption settings for the PdfDocument
  // using the modified PdfEncryption object
  document.setEncryptor(crypt);

  // Writes the document object to file
  document.write();

  // Closes all I/O streams associated with this writer object
  writer.dispose();
 }

 // Opens the encrypted document
 // with user password
 private static void openEncryptedDoc() 
    throws IOException, PdfException
 {
  // Creates a PdfReader object with the String object
  PdfReader reader = 
    PdfReader.fileReader("encrypted_doc.pdf");

  reader.setOnPasswordHandler(
    new PdfPasswordHandler_Example());

  // Creates a PdfDocument object with the PdfReader object
  // and make the file contents available for reading
  PdfDocument document = new PdfDocument(reader);

  // Prints the number of pages in the document
  System.out.println(document.getPageCount());

  // Closes all I/O streams associated with this reader object
  reader.dispose();
 }

 // Returns a password with which the 
 // encrypted document is opened 
 public String onPassword(PdfDocument d, boolean[] flags)
 {
  return "pdfone";      
 }  
}

See Also:
PdfReader, PdfEncryption

Method Summary
 String onPassword(PdfDocument d, boolean[] flags)
          Called when a password-protected document is read.
 

Method Detail

onPassword

public String onPassword(PdfDocument d,
                         boolean[] flags)
Called when a password-protected document is read. User classes handling this event can return the password with which document needs to be opened.

Parameters:
d - password-protected document that needs to be read
flags - whether the document needs to be opened with the specified password
Returns:
password with which the document needs to be read

Gnostice PDFOne
Pro. Ed. v5.0.0

To contact our support team, send an e-mail to support@gnostice.com.
 
© 2010 Gnostice Information Technologies Private Limited. All rights reserved.
www.gnostice.com