use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class CCITTFactoryTest method testCreateFromBufferedChessImage.
public void testCreateFromBufferedChessImage() throws IOException {
PDDocument document = new PDDocument();
Bitmap bim = Bitmap.createBitmap(343, 287, Bitmap.Config.ALPHA_8);
// not mult of 8
assertTrue((bim.getWidth() / 8) * 8 != bim.getWidth());
int col = 0;
for (int x = 0; x < bim.getWidth(); ++x) {
for (int y = 0; y < bim.getHeight(); ++y) {
bim.setPixel(x, y, col & 0xFFFFFF);
col = ~col;
}
}
PDImageXObject ximage3 = CCITTFactory.createFromImage(document, bim);
validate(ximage3, 1, 343, 287, "tiff", PDDeviceGray.INSTANCE.getName());
// checkIdent(bim, ximage3.getOpaqueImage()); TODO: PdfBox-Android
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
contentStream.drawImage(ximage3, 0, 0, ximage3.getWidth(), ximage3.getHeight());
contentStream.close();
document.save(testResultsDir + "/singletifffromchessbi.pdf");
document.close();
document = PDDocument.load(new File(testResultsDir, "singletifffromchessbi.pdf"));
assertEquals(1, document.getNumberOfPages());
document.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class CCITTFactoryTest method testCreateFromBitmap.
public void testCreateFromBitmap() throws IOException {
String tiffG4Path = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg4.tif";
PDDocument document = new PDDocument();
// Bitmap bim = ImageIO.read(new File(tiffG4Path));
// PDImageXObject ximage3 = CCITTFactory.createFromImage(document, bim);
// validate(ximage3, 1, 344, 287, "tiff", PDDeviceGray.INSTANCE.getName());
// checkIdent(bim, ximage3.getOpaqueImage());
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
// contentStream.drawImage(ximage3, 0, 0, ximage3.getWidth(), ximage3.getHeight());
contentStream.close();
document.save(testResultsDir + "/singletifffrombi.pdf");
document.close();
document = PDDocument.load(new File(testResultsDir, "singletifffrombi.pdf"));
assertEquals(1, document.getNumberOfPages());
document.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class CCITTFactoryTest method testByteShortPaddedWithGarbage.
/**
* Tests that byte/short tag values are read correctly (ignoring possible garbage in remaining
* bytes).
*/
public void testByteShortPaddedWithGarbage() throws IOException {
PDDocument document = new PDDocument();
String basePath = "pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/ccittg3-garbage-padded-fields";
for (String ext : Arrays.asList(".tif", "-bigendian.tif")) {
String tiffPath = basePath + ext;
InputStream sourceTiff = testContext.getAssets().open(tiffPath);
File copiedTiffFile = new File(testContext.getCacheDir(), "ccittg3-ccittg3-garbage-padded-fields" + ext);
copyFile(sourceTiff, copiedTiffFile);
PDImageXObject ximage3 = CCITTFactory.createFromFile(document, copiedTiffFile);
validate(ximage3, 1, 344, 287, "tiff", PDDeviceGray.INSTANCE.getName());
}
document.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class TestPublicKeyEncryption method testProtectionError.
/**
* Protect a document with certificate 1 and try to open it with
* certificate 2 and catch the exception.
*
* @throws Exception If there is an unexpected error during the test.
*/
@Test
public void testProtectionError() throws Exception {
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
policy.addRecipient(recipient1);
policy.setEncryptionKeyLength(keyLength);
document.protect(policy);
PDDocument encryptedDoc = null;
try {
File file = save("testProtectionError");
encryptedDoc = reload(file, password2, getKeyStore(keyStore2));
Assert.assertTrue(encryptedDoc.isEncrypted());
fail("No exception when using an incorrect decryption key");
} catch (IOException ex) {
String msg = ex.getMessage();
Assert.assertTrue("not the expected exception: " + msg, msg.contains("serial-#: rid 2 vs. cert 3"));
} finally {
if (encryptedDoc != null) {
encryptedDoc.close();
}
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class TestPublicKeyEncryption method testProtection.
/**
* Protect a document with a public certificate and try to open it
* with the corresponding private certificate.
*
* @throws Exception If there is an unexpected error during the test.
*/
@Test
public void testProtection() throws Exception {
PublicKeyProtectionPolicy policy = new PublicKeyProtectionPolicy();
policy.addRecipient(recipient1);
policy.setEncryptionKeyLength(keyLength);
document.protect(policy);
File file = save("testProtection");
PDDocument encryptedDoc = reload(file, password1, getKeyStore(keyStore1));
try {
Assert.assertTrue(encryptedDoc.isEncrypted());
AccessPermission permission = encryptedDoc.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 {
encryptedDoc.close();
}
}
Aggregations