Example usage for com.itextpdf.text.pdf PdfWriter DO_NOT_ENCRYPT_METADATA

List of usage examples for com.itextpdf.text.pdf PdfWriter DO_NOT_ENCRYPT_METADATA

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfWriter DO_NOT_ENCRYPT_METADATA.

Prototype

int DO_NOT_ENCRYPT_METADATA

To view the source code for com.itextpdf.text.pdf PdfWriter DO_NOT_ENCRYPT_METADATA.

Click Source Link

Document

Add this to the mode to keep the metadata in clear text

Usage

From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFEncryptionActionExecuter.java

License:Apache License

/**
 * @param reader//ww  w  .j av  a2s.com
 * @param writer
 * @param options
 * @throws Exception
 */
protected final void action(Action ruleAction, NodeRef actionedUponNodeRef,
        ContentReader actionedUponContentReader, Map<String, Object> options) {

    PdfStamper stamp = null;
    File tempDir = null;
    ContentWriter writer = null;

    try {
        // get the parameters
        String userPassword = (String) options.get(PARAM_USER_PASSWORD);
        String ownerPassword = (String) options.get(PARAM_OWNER_PASSWORD);
        int permissions = buildPermissionMask(options);
        int encryptionType = Integer.parseInt((String) options.get(PARAM_ENCRYPTION_LEVEL));

        // if metadata is excluded, alter encryption type
        if ((Boolean) options.get(PARAM_EXCLUDE_METADATA)) {
            encryptionType = encryptionType | PdfWriter.DO_NOT_ENCRYPT_METADATA;
        }

        // get temp file
        File alfTempDir = TempFileProvider.getTempDir();
        tempDir = new File(alfTempDir.getPath() + File.separatorChar + actionedUponNodeRef.getId());
        tempDir.mkdir();
        File file = new File(tempDir,
                serviceRegistry.getFileFolderService().getFileInfo(actionedUponNodeRef).getName());

        // get the PDF input stream and create a reader for iText
        PdfReader reader = new PdfReader(actionedUponContentReader.getContentInputStream());
        stamp = new PdfStamper(reader, new FileOutputStream(file));

        // encrypt PDF
        stamp.setEncryption(userPassword.getBytes(Charset.forName("UTF-8")),
                ownerPassword.getBytes(Charset.forName("UTF-8")), permissions, encryptionType);
        stamp.close();

        // write out to destination
        NodeRef destinationNode = createDestinationNode(file.getName(),
                (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER), actionedUponNodeRef);
        writer = serviceRegistry.getContentService().getWriter(destinationNode, ContentModel.PROP_CONTENT,
                true);

        writer.setEncoding(actionedUponContentReader.getEncoding());
        writer.setMimetype(FILE_MIMETYPE);
        writer.putContent(file);
        file.delete();

        //if useAspect is true, store some additional info about the signature in the props
        if (useAspect) {
            serviceRegistry.getNodeService().addAspect(destinationNode, PDFToolkitModel.ASPECT_ENCRYPTED,
                    new HashMap<QName, Serializable>());
            serviceRegistry.getNodeService().setProperty(destinationNode, PDFToolkitModel.PROP_ENCRYPTIONDATE,
                    new java.util.Date());
            serviceRegistry.getNodeService().setProperty(destinationNode, PDFToolkitModel.PROP_ENCRYPTEDBY,
                    AuthenticationUtil.getRunAsUser());
        }

    } catch (IOException e) {
        throw new AlfrescoRuntimeException(e.getMessage(), e);
    } catch (DocumentException e) {
        throw new AlfrescoRuntimeException(e.getMessage(), e);
    } finally {
        if (tempDir != null) {
            try {
                tempDir.delete();
            } catch (Exception ex) {
                throw new AlfrescoRuntimeException(ex.getMessage(), ex);
            }
        }

        if (stamp != null) {
            try {
                stamp.close();
            } catch (Exception ex) {
                throw new AlfrescoRuntimeException(ex.getMessage(), ex);
            }
        }
    }
}

From source file:org.sejda.impl.itext5.EncryptTask.java

License:Open Source License

public void execute(EncryptParameters parameters) throws TaskException {
    int currentStep = 0;
    for (PdfSource<?> source : parameters.getSourceList()) {
        currentStep++;/*from  w  w w .j av  a  2 s.  co m*/
        LOG.debug("Opening {} ", source);
        reader = source.open(sourceOpener);

        File tmpFile = createTemporaryPdfBuffer();
        LOG.debug("Created output temporary buffer {} ", tmpFile);
        stamperHandler = new PdfStamperHandler(reader, tmpFile, parameters.getVersion());
        stamperHandler.setCompression(parameters.isCompress(), reader);
        stamperHandler.setEncryption(
                getEncryptionAlgorithm(parameters.getEncryptionAlgorithm()) | PdfWriter.DO_NOT_ENCRYPT_METADATA,
                parameters.getUserPassword(), parameters.getOwnerPassword(), permissions);

        nullSafeCloseQuietly(stamperHandler);
        nullSafeClosePdfReader(reader);

        String outName = nameGenerator(parameters.getOutputPrefix())
                .generate(nameRequest().originalName(source.getName()).fileNumber(currentStep));
        outputWriter.addOutput(file(tmpFile).name(outName));

        notifyEvent(getNotifiableTaskMetadata()).stepsCompleted(currentStep).outOf(totalSteps);
    }

    parameters.getOutput().accept(outputWriter);
    LOG.debug("Input documents encrypted and written to {}", parameters.getOutput());
    LOG.debug("Permissions {}", PdfEncryptor.getPermissionsVerbose(permissions));
}

From source file:tutorials.encryptpdf.encryptpdf.java

public static void main(String[] args) {
    try {//from   www  .jav  a  2 s. c  om
        PdfReader reader = new PdfReader("test.pdf");
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("encrypted.pdf"));
        stamper.setEncryption(null, null, PdfWriter.ALLOW_PRINTING,
                PdfWriter.ENCRYPTION_AES_256 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
        stamper.close();
        reader.close();
    } catch (IOException | DocumentException ex) {
        ex.printStackTrace();
    }
}