Search in sources :

Example 46 with PDDocument

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();
}
Also used : Bitmap(android.graphics.Bitmap) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File)

Example 47 with PDDocument

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();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File)

Example 48 with PDDocument

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();
}
Also used : InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File)

Example 49 with PDDocument

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();
        }
    }
}
Also used : PublicKeyProtectionPolicy(com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Example 50 with PDDocument

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();
    }
}
Also used : PublicKeyProtectionPolicy(com.tom_roush.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) AccessPermission(com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission) File(java.io.File) Test(org.junit.Test)

Aggregations

PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)137 File (java.io.File)80 Test (org.junit.Test)69 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)37 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)22 InputStream (java.io.InputStream)21 Bitmap (android.graphics.Bitmap)18 IOException (java.io.IOException)14 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)11 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)11 ArrayList (java.util.ArrayList)11 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileInputStream (java.io.FileInputStream)9 FileOutputStream (java.io.FileOutputStream)9 COSArray (com.tom_roush.pdfbox.cos.COSArray)8 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)8 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)7 Paint (android.graphics.Paint)6