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.PdfWriter;

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

    public static void main(String[] args) throws IOException,
        PdfException
    {
        PdfEncryption_Examples obj = new PdfEncryption_Examples();

        obj.SETTERS_Example();
    }

    // This code segment creates an encrypted document
    public void SETTERS_Example() throws IOException, PdfException {

        // Creates a new PdfDocument object
        PdfWriter writer =
           PdfWriter.fileWriter("PdfEncryption_SETTERS_Example.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);
        
        // Sets the file to be opened after it is written to
        document.setOpenAfterSave(true);

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

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