Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] DLPJTS-130 Sample to add password protection to a document #125

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2018 Datalogics, Inc.
*/

package com.datalogics.pdf.samples.security;

import com.adobe.pdfjt.core.license.LicenseManager;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFSaveFullOptions;
import com.adobe.pdfjt.services.security.SecurityLockPassword;
import com.adobe.pdfjt.services.security.UnicodePasswordUtil;

import com.datalogics.pdf.document.DocumentHelper;
import com.datalogics.pdf.samples.signature.SignDocument;
import com.datalogics.pdf.samples.util.DocumentUtils;
import com.datalogics.pdf.samples.util.IoUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandles;
import java.net.URL;

public class PasswordProtect {
public static final String INPUT_UNPROTECTED_PDF_PATH = "/com/datalogics/pdf/samples/manipulation/pdfjavatoolkit-ds.pdf";
public static final String OUTPUT_PASSWORD_PROTECTED_PDF_PATH = "PasswordProtected.pdf";
private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

/**
* This is a utility class, and won't be instantiated.
*/
private PasswordProtect() {}

/**
* Main program.
*
* @param args command line arguments. Only one is expected in order to specify the output path. If no arguments are
* given, the sample will output to the root of the samples directory by default.
* @throws Exception a general exception was thrown
*/
public static void main(final String... args) throws Exception {
// If you are using an evaluation version of the product (License Managed, or LM), set the path to where PDFJT
// can find the license file.
//
// If you are not using an evaluation version of the product you can ignore or remove this code.
LicenseManager.setLicensePath(".");

URL outputUrl = null;
if (args.length > 0) {
outputUrl = IoUtils.createUrlFromPath(args[0]);
} else {
outputUrl = IoUtils.createUrlFromPath(OUTPUT_PASSWORD_PROTECTED_PDF_PATH);
}

final URL inputUrl = SignDocument.class.getResource(INPUT_UNPROTECTED_PDF_PATH);

// Query and sign all permissible signature fields.
passwordProtectDocument(inputUrl, outputUrl, "ownerPassword", "userPassword");
}

public static void passwordProtectDocument(final URL inputUrl, final URL outputUrl, final String ownerPassword,
final String userPassword)
throws Exception {
final PDFDocument pdfDocument = DocumentUtils.openPdfDocument(inputUrl);
final UnicodePasswordUtil unicodePasswordUtil = new UnicodePasswordUtil();
final byte[] ownerPW = unicodePasswordUtil.getPasswordFromUnicode(ownerPassword);
final byte[] userPW = unicodePasswordUtil.getPasswordFromUnicode(userPassword);

// Metadata should *not* be encrypted
final boolean encryptedMetadata = false;
final SecurityLockPassword securityLockPassword = SecurityLockPassword.newAES_128bit(pdfDocument,
ownerPW,
userPW,
encryptedMetadata);

final PDFSaveFullOptions pdfSaveFullOptions = PDFSaveFullOptions.newInstance(securityLockPassword);
DocumentHelper.saveAndClose(pdfDocument, outputUrl.toURI().getPath(), pdfSaveFullOptions);
}


}