Example usage for com.lowagie.text.pdf PdfWriter setEncryption

List of usage examples for com.lowagie.text.pdf PdfWriter setEncryption

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter setEncryption.

Prototype

public void setEncryption(int encryptionType, String userPassword, String ownerPassword, int permissions)
        throws DocumentException 

Source Link

Document

Sets the encryption options for this document.

Usage

From source file:questions.encryption.HelloWorldMetadataNotEncrypted.java

public static void main(String[] args) {
    // step 1/*w w  w.  j  a  v a2 s. c  o m*/
    Document document = new Document();
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        writer.setEncryption("hello".getBytes(), "world".getBytes(), 0,
                PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
        writer.createXmpMetadata();
        writer.setPdfVersion(PdfWriter.VERSION_1_5);
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World"));
        writer.addAnnotation(PdfAnnotation.createFileAttachment(writer, new Rectangle(100f, 650f, 150f, 700f),
                "This is some text", "some text".getBytes(), null, "some.txt"));
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
}