public interface PdfPasswordHandler
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"; } }
PdfReader
,
PdfEncryption
Modifier and Type | Method and Description |
---|---|
String |
onPassword(PdfDocument d,
boolean[] flags)
Called when a password-protected document is read.
|
String onPassword(PdfDocument d, boolean[] flags)
d
- password-protected document that needs to be readflags
- whether the document needs to be opened with the
specified password