-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PasswordProtect: Sample to add password protection to a document.
Basic proof-of-concept. Needs: Command-line options, varying permissions.
- Loading branch information
1 parent
8d13af1
commit 1d98167
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/main/java/com/datalogics/pdf/samples/security/PasswordProtect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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); | ||
final SecurityLockPassword securityLockPassword = SecurityLockPassword.newRC4_128bit(pdfDocument, | ||
ownerPW, | ||
userPW); | ||
final PDFSaveFullOptions pdfSaveFullOptions = PDFSaveFullOptions.newInstance(securityLockPassword); | ||
DocumentHelper.saveAndClose(pdfDocument, outputUrl.toURI().getPath(), pdfSaveFullOptions); | ||
} | ||
|
||
|
||
} |