use of com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission in project PdfBox-Android by TomRoush.
the class TestPublicKeyEncryption method testMultipleRecipients.
/**
* Protect the document for 2 recipients and try to open it.
*
* @throws Exception If there is an error during the test.
*/
@Test
public void testMultipleRecipients() throws Exception {
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
policy.addRecipient(recipient1);
policy.addRecipient(recipient2);
policy.setEncryptionKeyLength(keyLength);
document.protect(policy);
// open first time
File file = save("testMultipleRecipients");
PDDocument encryptedDoc1 = reload(file, password1, getKeyStore(keyStore1));
try {
AccessPermission permission = encryptedDoc1.getCurrentAccessPermission();
Assert.assertFalse(permission.canAssembleDocument());
Assert.assertFalse(permission.canExtractContent());
Assert.assertTrue(permission.canExtractForAccessibility());
Assert.assertFalse(permission.canFillInForm());
Assert.assertFalse(permission.canModify());
Assert.assertFalse(permission.canModifyAnnotations());
Assert.assertFalse(permission.canPrint());
Assert.assertFalse(permission.canPrintDegraded());
} finally {
encryptedDoc1.close();
}
// open second time
PDDocument encryptedDoc2 = reload(file, password2, getKeyStore(keyStore2));
try {
AccessPermission permission = encryptedDoc2.getCurrentAccessPermission();
Assert.assertFalse(permission.canAssembleDocument());
Assert.assertFalse(permission.canExtractContent());
Assert.assertTrue(permission.canExtractForAccessibility());
Assert.assertFalse(permission.canFillInForm());
Assert.assertFalse(permission.canModify());
Assert.assertFalse(permission.canModifyAnnotations());
Assert.assertTrue(permission.canPrint());
Assert.assertFalse(permission.canPrintDegraded());
} finally {
encryptedDoc2.close();
}
}
use of com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission in project PdfBox-Android by TomRoush.
the class TestPublicKeyEncryption method setUp.
/**
* {@inheritDoc}
*/
@Before
public void setUp() throws Exception {
if (Cipher.getMaxAllowedKeyLength("AES") != Integer.MAX_VALUE) {
// we need strong encryption for these tests
fail("JCE unlimited strength jurisdiction policy files are not installed");
}
testContext = InstrumentationRegistry.getInstrumentation().getContext();
PDFBoxResourceLoader.init(testContext);
testResultsDir = new File(testContext.getCacheDir(), "pdfbox-test-output/crypto");
testResultsDir.mkdirs();
permission1 = new AccessPermission();
permission1.setCanAssembleDocument(false);
permission1.setCanExtractContent(false);
permission1.setCanExtractForAccessibility(true);
permission1.setCanFillInForm(false);
permission1.setCanModify(false);
permission1.setCanModifyAnnotations(false);
permission1.setCanPrint(false);
permission1.setCanPrintDegraded(false);
permission2 = new AccessPermission();
permission2.setCanAssembleDocument(false);
permission2.setCanExtractContent(false);
permission2.setCanExtractForAccessibility(true);
permission2.setCanFillInForm(false);
permission2.setCanModify(false);
permission2.setCanModifyAnnotations(false);
// it is true now !
permission2.setCanPrint(true);
permission2.setCanPrintDegraded(false);
recipient1 = getRecipient("test1.der", permission1);
recipient2 = getRecipient("test2.der", permission2);
password1 = "test1";
password2 = "test2";
keyStore1 = "test1.pfx";
keyStore2 = "test2.pfx";
document = PDDocument.load(testContext.getAssets().open(path + "test.pdf"));
text = new PDFTextStripper().getText(document);
producer = document.getDocumentInformation().getProducer();
document.setVersion(1.7f);
}
use of com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission in project PdfBox-Android by TomRoush.
the class MainActivity method createEncryptedPdf.
/**
* Creates a simple pdf and encrypts it
*/
public void createEncryptedPdf(View v) {
String path = root.getAbsolutePath() + "/crypt.pdf";
// 128 bit is the highest currently supported
int keyLength = 128;
// Limit permissions of those without the password
AccessPermission ap = new AccessPermission();
ap.setCanPrint(false);
// Sets the owner password and user password
StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "hi", ap);
// Setups up the encryption parameters
spp.setEncryptionKeyLength(keyLength);
spp.setPermissions(ap);
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);
PDFont font = PDType1Font.HELVETICA;
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
try {
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Write Hello World in blue text
contentStream.beginText();
contentStream.setNonStrokingColor(15, 38, 192);
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Hello World");
contentStream.endText();
contentStream.close();
// Save the final pdf document to a file
// Apply the protections to the PDF
document.protect(spp);
document.save(path);
document.close();
tv.setText("Successfully wrote PDF to " + path);
} catch (IOException e) {
Log.e("PdfBox-Android-Sample", "Exception thrown while creating PDF for encryption", e);
}
}
Aggregations