Search in sources :

Example 51 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class TestPublicKeyEncryption method reload.

/**
 * Reloads the given document from a file and check some contents.
 *
 * @param file input file
 * @param decryptionPassword password to be used to decrypt the doc
 * @param keyStore password to be used to decrypt the doc
 * @return reloaded document
 * @throws Exception if
 */
private PDDocument reload(File file, String decryptionPassword, InputStream keyStore) throws IOException, NoSuchAlgorithmException {
    PDDocument doc2 = PDDocument.load(file, decryptionPassword, keyStore, null, MemoryUsageSetting.setupMainMemoryOnly());
    Assert.assertEquals("Extracted text is different", text, new PDFTextStripper().getText(doc2));
    Assert.assertEquals("Producer is different", producer, doc2.getDocumentInformation().getProducer());
    return doc2;
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDFTextStripper(com.tom_roush.pdfbox.text.PDFTextStripper)

Example 52 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class JPEGFactoryTest method testCreateFromImageRGB.

/**
 * Tests RGB JPEGFactory#createFromImage(PDDocument document, BufferedImage
 * image) with color JPEG image
 */
@Test
public void testCreateFromImageRGB() throws IOException {
    PDDocument document = new PDDocument();
    Bitmap image = BitmapFactory.decodeStream(testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/jpeg.jpg"));
    // assertEquals(3, image.getColorModel().getNumComponents()); TODO: PdfBox-Android
    PDImageXObject ximage = JPEGFactory.createFromImage(document, image);
    validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName());
    doWritePDF(document, ximage, testResultsDir, "jpegrgb.pdf");
}
Also used : Bitmap(android.graphics.Bitmap) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) Test(org.junit.Test)

Example 53 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class JPEGFactoryTest method testCreateFromImageUSHORT_555_RGB.

/**
 * Tests USHORT_555_RGB JPEGFactory#createFromImage(PDDocument document, BufferedImage
 * image), see also PDFBOX-4674.
 * @throws java.io.IOException
 */
@Test
public void testCreateFromImageUSHORT_555_RGB() throws IOException {
    // http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7044758
    if (System.getProperty("java.runtime.name").equals("OpenJDK Runtime Environment") && (System.getProperty("java.specification.version").equals("1.6") || System.getProperty("java.specification.version").equals("1.7") || System.getProperty("java.specification.version").equals("1.8"))) {
        return;
    }
    PDDocument document = new PDDocument();
    Bitmap image = BitmapFactory.decodeStream(testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/jpeg.jpg"));
    // create an USHORT_555_RGB image
    int width = image.getWidth();
    int height = image.getHeight();
    Bitmap rgbImage = image.copy(Bitmap.Config.RGB_565, true);
    for (int x = 0; x < rgbImage.getWidth(); ++x) {
        for (int y = 0; y < rgbImage.getHeight(); ++y) {
            rgbImage.setPixel(x, y, (rgbImage.getPixel(x, y) & 0xFFFFFF) | ((y / 10 * 10) << 24));
        }
    }
    PDImageXObject ximage = JPEGFactory.createFromImage(document, rgbImage);
    validate(ximage, 8, width, height, "jpg", PDDeviceRGB.INSTANCE.getName());
    assertNull(ximage.getSoftMask());
    doWritePDF(document, ximage, testResultsDir, "jpeg-ushort555rgb.pdf");
}
Also used : Bitmap(android.graphics.Bitmap) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) Test(org.junit.Test)

Example 54 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class JPEGFactoryTest method testCreateFromStream256.

/**
 * Tests JPEGFactory#createFromStream(PDDocument document, InputStream
 * stream) with gray JPEG file
 */
@Test
public void testCreateFromStream256() throws IOException {
    PDDocument document = new PDDocument();
    InputStream stream = testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/jpeg256.jpg");
    PDImageXObject ximage = JPEGFactory.createFromStream(document, stream);
    // TODO: PdfBox-Android
    validate(ximage, 8, 344, 287, "jpg", PDDeviceRGB.INSTANCE.getName());
    doWritePDF(document, ximage, testResultsDir, "jpeg256stream.pdf");
    checkJpegStream(testResultsDir, "jpeg256stream.pdf", testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/jpeg256.jpg"));
}
Also used : InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) Test(org.junit.Test)

Example 55 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.

the class LosslessFactoryTest method testCreateLosslessFromImage4BYTE_ABGR.

// testCreateLosslessFromImageBITMASK_INT_ARGB: Android does not have bitmask transparency
// testCreateLosslessFromImageBITMASK4BYTE_ABGR: Android does not have bitmask transparency
/**
 * Tests 4BYTE_ABGR LosslessFactoryTest#createFromImage(PDDocument document,
 * BufferedImage image)
 *
 * @throws java.io.IOException
 */
@Test
public void testCreateLosslessFromImage4BYTE_ABGR() throws IOException {
    PDDocument document = new PDDocument();
    Bitmap image = BitmapFactory.decodeStream(testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/png.png"));
    // create an ARGB image
    int w = image.getWidth();
    int h = image.getHeight();
    Bitmap argbImage = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas();
    canvas.setBitmap(argbImage);
    Paint paint = new Paint();
    canvas.drawBitmap(image, 0, 0, paint);
    int[] argbPixels = new int[w * h];
    argbImage.getPixels(argbPixels, 0, w, 0, 0, w, h);
    for (int x = 0; x < argbImage.getWidth(); ++x) {
        for (int y = 0; y < argbImage.getHeight(); ++y) {
            argbPixels[x + w * y] = (argbPixels[x + w * y] & 0xFFFFFF) | ((y / 10 * 10) << 24);
        }
    }
    argbImage.setPixels(argbPixels, 0, w, 0, 0, w, h);
    PDImageXObject ximage = LosslessFactory.createFromImage(document, argbImage);
    validate(ximage, 8, w, h, "png", PDDeviceRGB.INSTANCE.getName());
    checkIdent(argbImage, ximage.getImage());
    checkIdentRGB(argbImage, ximage.getOpaqueImage());
    assertNotNull(ximage.getSoftMask());
    validate(ximage.getSoftMask(), 8, w, h, "png", PDDeviceGray.INSTANCE.getName());
    assertTrue(colorCount(ximage.getSoftMask().getImage()) > image.getHeight() / 10);
    doWritePDF(document, ximage, testResultsDir, "4babgr.pdf");
}
Also used : Bitmap(android.graphics.Bitmap) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint) Paint(android.graphics.Paint) FlakyTest(androidx.test.filters.FlakyTest) 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