Example usage for com.lowagie.text.pdf PdfEncryptor encrypt

List of usage examples for com.lowagie.text.pdf PdfEncryptor encrypt

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfEncryptor encrypt.

Prototype

public static void encrypt(PdfReader reader, OutputStream os, int type, String userPassword,
        String ownerPassword, int permissions) throws DocumentException, IOException 

Source Link

Document

Entry point to encrypt a PDF document.

Usage

From source file:com.mirth.connect.connectors.doc.DocumentMessageDispatcher.java

License:Open Source License

private void writeDocument(String template, File file, MessageObject messageObject) throws Exception {
    // add tags to the template to create a valid HTML document
    StringBuilder contents = new StringBuilder();
    if (template.lastIndexOf("<html") < 0) {
        contents.append("<html>");
        if (template.lastIndexOf("<body") < 0) {
            contents.append("<body>");
            contents.append(template);/*from   w ww  . j  av  a2  s  .  com*/
            contents.append("</body>");
        } else {
            contents.append(template);
        }
        contents.append("</html>");
    } else {
        contents.append(template);
    }

    if (connector.getDocumentType().toLowerCase().equals("pdf")) {
        FileOutputStream renderFos = null;

        try {
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            org.w3c.dom.Document document = builder
                    .parse(new InputSource(new StringReader(contents.toString())));

            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(document, null);
            renderFos = new FileOutputStream(file);
            renderer.layout();
            renderer.createPDF(renderFos, true);
        } catch (Throwable e) {
            throw new Exception(e);
        } finally {
            if (renderFos != null) {
                renderFos.close();
            }
        }

        if (connector.isEncrypt() && (connector.getPassword() != null)) {
            FileInputStream encryptFis = null;
            FileOutputStream encryptFos = null;

            try {
                encryptFis = new FileInputStream(file);
                PdfReader reader = new PdfReader(encryptFis);
                encryptFos = new FileOutputStream(file);
                PdfEncryptor.encrypt(reader, encryptFos, true,
                        replacer.replaceValues(connector.getPassword(), messageObject), null,
                        PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY);
            } catch (Exception e) {
                throw e;
            } finally {
                if (encryptFis != null) {
                    encryptFis.close();
                }

                if (encryptFos != null) {
                    encryptFos.close();
                }
            }
        }
    } else if (connector.getDocumentType().toLowerCase().equals("rtf")) {
        com.lowagie.text.Document document = null;

        try {
            document = new com.lowagie.text.Document();
            ByteArrayInputStream bais = new ByteArrayInputStream(contents.toString().getBytes());
            RtfWriter2.getInstance(document, new FileOutputStream(file));
            document.open();
            HtmlParser parser = new HtmlParser();
            parser.go(document, bais);
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }
}

From source file:com.orange.atk.atkUI.corecli.utils.PdfUtilities.java

License:Apache License

/**
 * Encrypt (allow only printing permission) the given pdf file with the given passwords.
 * Encryption must be the latest operation on the file.
 * @param pdfFileName//from   w ww . jav  a 2  s  .c o m
 * @throws IOException
 * @throws DocumentException
 */
public void encryptPDF(String pdfFileName) throws IOException, DocumentException {
    // 1. copy
    File tmpPDFFile = new File(tmpDir, "tmpPDF.pdf");
    copyFile(new File(pdfFileName), tmpPDFFile);
    // 2. encrypt
    PdfReader reader = new PdfReader(tmpPDFFile.getAbsolutePath());
    PdfEncryptor.encrypt(reader, new FileOutputStream(pdfFileName), true, pdfEncryptionUserPassword,
            pdfEncryptionOwnerPassword, PdfWriter.AllowPrinting /*| PdfWriter.AllowCopy*/);
}

From source file:mitm.application.djigzo.james.mailets.PDFEncrypt.java

License:Open Source License

private byte[] encryptPDF(byte[] unencryptedPDF, String userPassword, String ownerPassword)
        throws IOException, DocumentException {
    PdfReader pdfReader = new PdfReader(unencryptedPDF);

    ByteArrayOutputStream encryptedPDF = new ByteArrayOutputStream(unencryptedPDF.length + ENCRYPTION_OVERHEAD);

    PdfEncryptor.encrypt(pdfReader, encryptedPDF, pdfEncryptionType, userPassword, ownerPassword,
            getOpenPermissionsIntValue());

    return encryptedPDF.toByteArray();
}

From source file:net.laubenberger.bogatyr.helper.HelperPdf.java

License:Open Source License

/**
 * Encrypt a PDF with a given user and password.
 * //from ww w . j  a  v  a  2 s  . c om
 * @param source
 *           {@link File}
 * @param dest
 *           {@link File} for the encrypted PDF
 * @param user
 *           of the dest {@link File}
 * @param password
 *           of the dest {@link File}
 * @throws DocumentException
 * @throws IOException
 * @since 0.9.4
 */
public static void encrypt(final File source, final File dest, final byte[] user, final byte[] password)
        throws IOException, DocumentException { // $JUnit$
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodStart(source, dest, user, password));
    if (null == source) {
        throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$
    }
    if (null == dest) {
        throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$
    }
    if (HelperObject.isEquals(source, dest)) {
        throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (null == user) {
        throw new RuntimeExceptionIsNull("user"); //$NON-NLS-1$
    }
    if (!HelperArray.isValid(user)) {
        throw new RuntimeExceptionIsEmpty("user"); //$NON-NLS-1$
    }
    if (null == password) {
        throw new RuntimeExceptionIsNull("password"); //$NON-NLS-1$
    }
    if (!HelperArray.isValid(password)) {
        throw new RuntimeExceptionIsEmpty("password"); //$NON-NLS-1$
    }

    PdfReader reader = null;
    OutputStream os = null;

    try {
        reader = new PdfReader(source.getAbsolutePath());
        os = new FileOutputStream(dest);

        PdfEncryptor.encrypt(reader, os, user, password,
                PdfWriter.ALLOW_ASSEMBLY | PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_DEGRADED_PRINTING
                        | PdfWriter.ALLOW_FILL_IN | PdfWriter.ALLOW_MODIFY_ANNOTATIONS
                        | PdfWriter.ALLOW_MODIFY_CONTENTS | PdfWriter.ALLOW_PRINTING
                        | PdfWriter.ALLOW_SCREENREADERS,
                true);
    } finally {
        if (null != os) {
            os.close();
        }
        if (null != reader) {
            reader.close();
        }
    }
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodExit());
}

From source file:net.laubenberger.bogatyr.helper.HelperPdf.java

License:Open Source License

/**
 * Adds a all restrictions to a PDF with a given password.
 * /*from  w  ww . j  a v  a  2s  .  co m*/
 * @param source
 *           {@link File}
 * @param dest
 *           {@link File} for the locked PDF
 * @param password
 *           of the dest {@link File}
 * @throws DocumentException
 * @throws IOException
 * @since 0.9.4
 */
public static void lock(final File source, final File dest, final byte[] password)
        throws IOException, DocumentException { // $JUnit$
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodStart(source, dest, password));
    if (null == source) {
        throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$
    }
    if (null == dest) {
        throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$
    }
    if (HelperObject.isEquals(source, dest)) {
        throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (null == password) {
        throw new RuntimeExceptionIsNull("password"); //$NON-NLS-1$
    }
    if (!HelperArray.isValid(password)) {
        throw new RuntimeExceptionIsEmpty("password"); //$NON-NLS-1$
    }

    PdfReader reader = null;
    OutputStream os = null;

    try {
        reader = new PdfReader(source.getAbsolutePath());
        os = new FileOutputStream(dest);

        PdfEncryptor.encrypt(reader, os, null, password, 0, true);
    } finally {
        if (null != os) {
            os.close();
        }
        if (null != reader) {
            reader.close();
        }
    }
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodExit());
}

From source file:net.laubenberger.bogatyr.helper.HelperPdf.java

License:Open Source License

/**
 * Removes all restrictions from a PDF with a given password.
 * //w  ww  .j  a v  a  2s .co m
 * @param source
 *           {@link File}
 * @param dest
 *           {@link File} for the unlocked PDF
 * @param password
 *           of the source {@link File}
 * @throws DocumentException
 * @throws IOException
 * @since 0.9.0
 */
public static void unlock(final File source, final File dest, final byte[] password)
        throws IOException, DocumentException { // $JUnit$
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodStart(source, dest, password));
    if (null == source) {
        throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$
    }
    if (null == dest) {
        throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$
    }
    if (HelperObject.isEquals(source, dest)) {
        throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (null == password) {
        throw new RuntimeExceptionIsNull("password"); //$NON-NLS-1$
    }
    if (!HelperArray.isValid(password)) {
        throw new RuntimeExceptionIsEmpty("password"); //$NON-NLS-1$
    }

    PdfReader reader = null;
    OutputStream os = null;

    try {
        reader = new PdfReader(source.getAbsolutePath(), password);
        os = new FileOutputStream(dest);

        PdfEncryptor.encrypt(reader, os, null, null,
                PdfWriter.ALLOW_ASSEMBLY | PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_DEGRADED_PRINTING
                        | PdfWriter.ALLOW_FILL_IN | PdfWriter.ALLOW_MODIFY_ANNOTATIONS
                        | PdfWriter.ALLOW_MODIFY_CONTENTS | PdfWriter.ALLOW_PRINTING
                        | PdfWriter.ALLOW_SCREENREADERS,
                true);
    } finally {
        if (null != os) {
            os.close();
        }
        if (null != reader) {
            reader.close();
        }
    }
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodExit());
}

From source file:net.laubenberger.wichtel.helper.HelperPdf.java

License:Open Source License

/**
 * Encrypt a PDF with a given user and password.
 * /*  w  w  w.j av a2 s  .c  om*/
 * @param source
 *           {@link File}
 * @param dest
 *           {@link File} for the encrypted PDF
 * @param user
 *           of the dest {@link File}
 * @param password
 *           of the dest {@link File}
 * @throws DocumentException
 * @throws IOException
 * @since 0.0.1
 */
public static void encrypt(final File source, final File dest, final byte[] user, final byte... password)
        throws IOException, DocumentException { // $JUnit$
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodStart(source, dest, user, password));
    if (null == source) {
        throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$
    }
    if (null == dest) {
        throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$
    }
    if (HelperObject.isEquals(source, dest)) {
        throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (null == user) {
        throw new RuntimeExceptionIsNull("user"); //$NON-NLS-1$
    }
    if (!HelperArray.isValid(user)) {
        throw new RuntimeExceptionIsEmpty("user"); //$NON-NLS-1$
    }
    if (null == password) {
        throw new RuntimeExceptionIsNull("password"); //$NON-NLS-1$
    }
    if (!HelperArray.isValid(password)) {
        throw new RuntimeExceptionIsEmpty("password"); //$NON-NLS-1$
    }

    PdfReader reader = null;

    try (OutputStream os = new FileOutputStream(dest)) {
        reader = new PdfReader(source.getAbsolutePath());

        PdfEncryptor.encrypt(reader, os, user, password,
                PdfWriter.ALLOW_ASSEMBLY | PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_DEGRADED_PRINTING
                        | PdfWriter.ALLOW_FILL_IN | PdfWriter.ALLOW_MODIFY_ANNOTATIONS
                        | PdfWriter.ALLOW_MODIFY_CONTENTS | PdfWriter.ALLOW_PRINTING
                        | PdfWriter.ALLOW_SCREENREADERS,
                true);
    } finally {
        if (null != reader) {
            reader.close();
        }
    }
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodExit());
}

From source file:net.laubenberger.wichtel.helper.HelperPdf.java

License:Open Source License

/**
 * Adds a all restrictions to a PDF with a given password.
 * //from   w  w  w  .  j  a va  2 s .  c o m
 * @param source
 *           {@link File}
 * @param dest
 *           {@link File} for the locked PDF
 * @param password
 *           of the dest {@link File}
 * @throws DocumentException
 * @throws IOException
 * @since 0.0.1
 */
public static void lock(final File source, final File dest, final byte... password)
        throws IOException, DocumentException { // $JUnit$
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodStart(source, dest, password));
    if (null == source) {
        throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$
    }
    if (null == dest) {
        throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$
    }
    if (HelperObject.isEquals(source, dest)) {
        throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (null == password) {
        throw new RuntimeExceptionIsNull("password"); //$NON-NLS-1$
    }
    if (!HelperArray.isValid(password)) {
        throw new RuntimeExceptionIsEmpty("password"); //$NON-NLS-1$
    }

    PdfReader reader = null;

    try (OutputStream os = new FileOutputStream(dest)) {
        reader = new PdfReader(source.getAbsolutePath());

        PdfEncryptor.encrypt(reader, os, null, password, 0, true);
    } finally {
        if (null != reader) {
            reader.close();
        }
    }
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodExit());
}

From source file:net.laubenberger.wichtel.helper.HelperPdf.java

License:Open Source License

/**
 * Removes all restrictions from a PDF with a given password.
 * //from  w  ww.j ava  2 s  .  c  om
 * @param source
 *           {@link File}
 * @param dest
 *           {@link File} for the unlocked PDF
 * @param password
 *           of the source {@link File}
 * @throws DocumentException
 * @throws IOException
 * @since 0.0.1
 */
public static void unlock(final File source, final File dest, final byte... password)
        throws IOException, DocumentException { // $JUnit$
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodStart(source, dest, password));
    if (null == source) {
        throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$
    }
    if (null == dest) {
        throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$
    }
    if (HelperObject.isEquals(source, dest)) {
        throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (null == password) {
        throw new RuntimeExceptionIsNull("password"); //$NON-NLS-1$
    }
    if (!HelperArray.isValid(password)) {
        throw new RuntimeExceptionIsEmpty("password"); //$NON-NLS-1$
    }

    PdfReader reader = null;

    try (OutputStream os = new FileOutputStream(dest)) {
        reader = new PdfReader(source.getAbsolutePath(), password);

        PdfEncryptor.encrypt(reader, os, null, null,
                PdfWriter.ALLOW_ASSEMBLY | PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_DEGRADED_PRINTING
                        | PdfWriter.ALLOW_FILL_IN | PdfWriter.ALLOW_MODIFY_ANNOTATIONS
                        | PdfWriter.ALLOW_MODIFY_CONTENTS | PdfWriter.ALLOW_PRINTING
                        | PdfWriter.ALLOW_SCREENREADERS,
                true);
    } finally {
        if (null != reader) {
            reader.close();
        }
    }
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodExit());
}

From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java

License:Open Source License

public void encrypt(int pageCount, PdfPageData currentPageData, EncryptPDFDocument encryptPage) {
    String p = encryptPage.getPermissions();
    int encryptionLevel = encryptPage.getEncryptionLevel();
    String userPassword = encryptPage.getUserPassword();
    String masterPassword = encryptPage.getMasterPassword();

    int permit[] = { PdfWriter.AllowPrinting, PdfWriter.AllowModifyContents, PdfWriter.AllowCopy,
            PdfWriter.AllowModifyAnnotations, PdfWriter.AllowFillIn };

    int permissions = 0;
    for (int i = 0; i < p.length(); ++i) {
        permissions |= (p.charAt(i) == '0' ? 0 : permit[i]);
    }//from  w  ww .  j a  va 2 s  .  c  o m

    File tempFile = null;

    try {
        tempFile = File.createTempFile("temp", null);

        ObjectStore.copy(selectedFile, tempFile.getAbsolutePath());
    } catch (Exception e) {
        return;
    }

    try {
        PdfReader reader = new PdfReader(tempFile.getAbsolutePath());

        PdfEncryptor.encrypt(reader, new FileOutputStream(selectedFile), userPassword.getBytes(),
                masterPassword.getBytes(), permissions, encryptionLevel == 0);

    } catch (Exception e) {

        ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile);

        e.printStackTrace();

    } finally {
        tempFile.delete();
    }
}