Search in sources :

Example 6 with PDFRenderer

use of com.tom_roush.pdfbox.rendering.PDFRenderer in project PdfBox-Android by TomRoush.

the class PDAcroFormFlattenTest method generateSamples.

/*
    * Generate the sample images to which the PDF will be compared after flatten.
    */
private static void generateSamples(String sourceUrl, String targetFile) throws IOException {
    getFromUrl(sourceUrl, targetFile);
    File file = new File(IN_DIR, targetFile);
    PDDocument document = PDDocument.load(file, (String) null);
    String outputPrefix = file.getName() + "-";
    int numPages = document.getNumberOfPages();
    PDFRenderer renderer = new PDFRenderer(document);
    for (int i = 0; i < numPages; i++) {
        String fileName = outputPrefix + (i + 1) + ".png";
        // Windows native DPI
        Bitmap image = renderer.renderImageWithDPI(i, 96);
        File renderFile = new File(OUT_DIR, fileName);
        FileOutputStream fileOut = new FileOutputStream(renderFile);
        image.compress(Bitmap.CompressFormat.PNG, 100, fileOut);
        fileOut.close();
    }
    document.close();
}
Also used : Bitmap(android.graphics.Bitmap) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) FileOutputStream(java.io.FileOutputStream) File(java.io.File) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer)

Example 7 with PDFRenderer

use of com.tom_roush.pdfbox.rendering.PDFRenderer in project PdfBox-Android by TomRoush.

the class ValidateXImage method doWritePDF.

// write image twice (overlapped) in document, close document and re-read PDF
static void doWritePDF(PDDocument document, PDImageXObject ximage, File testResultsDir, String filename) throws IOException {
    File pdfFile = new File(testResultsDir, filename);
    // This part isn't really needed because this test doesn't break
    // if the mask has the wrong colorspace (PDFBOX-2057), but it is still useful
    // if something goes wrong in the future and we want to have a PDF to open.
    PDPage page = new PDPage();
    document.addPage(page);
    PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, false);
    contentStream.drawImage(ximage, 150, 300);
    contentStream.drawImage(ximage, 200, 350);
    contentStream.close();
    // check that the resource map is up-to-date
    assertEquals(1, count(document.getPage(0).getResources().getXObjectNames()));
    document.save(pdfFile);
    document.close();
    document = PDDocument.load(pdfFile, (String) null);
    assertEquals(1, count(document.getPage(0).getResources().getXObjectNames()));
    new PDFRenderer(document).renderImage(0);
    document.close();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) File(java.io.File) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer)

Example 8 with PDFRenderer

use of com.tom_roush.pdfbox.rendering.PDFRenderer in project PdfBox-Android by TomRoush.

the class TestSymmetricKeyEncryption method testSymmEncrForKeySize.

private void testSymmEncrForKeySize(int keyLength, boolean preferAES, int sizePriorToEncr, byte[] inputFileAsByteArray, String userpassword, String ownerpassword, AccessPermission permission) throws IOException {
    PDDocument document = PDDocument.load(inputFileAsByteArray);
    String prefix = "Simple-";
    int numSrcPages = document.getNumberOfPages();
    PDFRenderer pdfRenderer = new PDFRenderer(document);
    List<Bitmap> srcImgTab = new ArrayList<Bitmap>();
    List<byte[]> srcContentStreamTab = new ArrayList<byte[]>();
    for (int i = 0; i < numSrcPages; ++i) {
        srcImgTab.add(pdfRenderer.renderImage(i));
        InputStream unfilteredStream = document.getPage(i).getContents();
        byte[] bytes = IOUtils.toByteArray(unfilteredStream);
        unfilteredStream.close();
        srcContentStreamTab.add(bytes);
    }
    PDDocument encryptedDoc = encrypt(keyLength, preferAES, sizePriorToEncr, document, prefix, permission, userpassword, ownerpassword);
    assertEquals(numSrcPages, encryptedDoc.getNumberOfPages());
    pdfRenderer = new PDFRenderer(encryptedDoc);
    for (int i = 0; i < encryptedDoc.getNumberOfPages(); ++i) {
        // compare rendering
        Bitmap bim = pdfRenderer.renderImage(i);
        ValidateXImage.checkIdent(bim, srcImgTab.get(i));
        // compare content streams
        InputStream unfilteredStream = encryptedDoc.getPage(i).getContents();
        byte[] bytes = IOUtils.toByteArray(unfilteredStream);
        unfilteredStream.close();
        Assert.assertArrayEquals("content stream of page " + i + " not identical", srcContentStreamTab.get(i), bytes);
    }
    File pdfFile = new File(testResultsDir, prefix + keyLength + "-bit-" + (preferAES ? "AES" : "RC4") + "-decrypted.pdf");
    encryptedDoc.setAllSecurityToBeRemoved(true);
    encryptedDoc.save(pdfFile);
    encryptedDoc.close();
}
Also used : Bitmap(android.graphics.Bitmap) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) ArrayList(java.util.ArrayList) PDEmbeddedFile(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile) File(java.io.File) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer)

Example 9 with PDFRenderer

use of com.tom_roush.pdfbox.rendering.PDFRenderer in project PdfBox-Android by TomRoush.

the class PDFMergerUtilityTest method checkMergeIdentical.

// checks that the result file of a merge has the same rendering as the two
// source files
private void checkMergeIdentical(String filename1, String filename2, String mergeFilename, MemoryUsageSetting memUsageSetting) throws IOException {
    PDDocument srcDoc1 = PDDocument.load(testContext.getAssets().open(SRCDIR + "/" + filename1), (String) null);
    int src1PageCount = srcDoc1.getNumberOfPages();
    PDFRenderer src1PdfRenderer = new PDFRenderer(srcDoc1);
    Bitmap[] src1ImageTab = new Bitmap[src1PageCount];
    for (int page = 0; page < src1PageCount; ++page) {
        src1ImageTab[page] = src1PdfRenderer.renderImageWithDPI(page, DPI);
    }
    srcDoc1.close();
    PDDocument srcDoc2 = PDDocument.load(testContext.getAssets().open(SRCDIR + "/" + filename2), (String) null);
    int src2PageCount = srcDoc2.getNumberOfPages();
    PDFRenderer src2PdfRenderer = new PDFRenderer(srcDoc2);
    Bitmap[] src2ImageTab = new Bitmap[src2PageCount];
    for (int page = 0; page < src2PageCount; ++page) {
        src2ImageTab[page] = src2PdfRenderer.renderImageWithDPI(page, DPI);
    }
    srcDoc2.close();
    PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
    pdfMergerUtility.addSource(testContext.getAssets().open(SRCDIR + "/" + filename1));
    pdfMergerUtility.addSource(testContext.getAssets().open(SRCDIR + "/" + filename2));
    pdfMergerUtility.setDestinationFileName(TARGETTESTDIR + mergeFilename);
    pdfMergerUtility.mergeDocuments(memUsageSetting);
    PDDocument mergedDoc = PDDocument.load(new File(TARGETTESTDIR, mergeFilename), (String) null);
    PDFRenderer mergePdfRenderer = new PDFRenderer(mergedDoc);
    int mergePageCount = mergedDoc.getNumberOfPages();
    assertEquals(src1PageCount + src2PageCount, mergePageCount);
    for (int page = 0; page < src1PageCount; ++page) {
        Bitmap bim = mergePdfRenderer.renderImageWithDPI(page, DPI);
        checkImagesIdentical(bim, src1ImageTab[page]);
    }
    for (int page = 0; page < src2PageCount; ++page) {
        int mergePage = page + src1PageCount;
        Bitmap bim = mergePdfRenderer.renderImageWithDPI(mergePage, DPI);
        checkImagesIdentical(bim, src2ImageTab[page]);
    }
    mergedDoc.close();
}
Also used : Bitmap(android.graphics.Bitmap) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer)

Example 10 with PDFRenderer

use of com.tom_roush.pdfbox.rendering.PDFRenderer in project PdfBox-Android by TomRoush.

the class PDFontTest method testPDFBox988.

/**
 * Test of the error reported in PDFBOX-988
 *
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void testPDFBox988() throws IOException, URISyntaxException {
    PDDocument doc = null;
    try {
        File pdf = new File(testContext.getCacheDir(), "F001u_3_7j.pdf");
        OutputStream os = new FileOutputStream(pdf);
        IOUtils.copy(testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/font/F001u_3_7j.pdf"), os);
        os.close();
        doc = PDDocument.load(pdf);
        PDFRenderer renderer = new PDFRenderer(doc);
        renderer.renderImage(0);
    // the allegation is that renderImage() will crash the JVM or hang
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) PDFRenderer(com.tom_roush.pdfbox.rendering.PDFRenderer) Test(org.junit.Test)

Aggregations

PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)12 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)11 File (java.io.File)10 Bitmap (android.graphics.Bitmap)6 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)5 FileOutputStream (java.io.FileOutputStream)5 Test (org.junit.Test)5 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)4 Paint (android.graphics.Paint)2 IOException (java.io.IOException)2 Canvas (android.graphics.Canvas)1 FlakyTest (androidx.test.filters.FlakyTest)1 COSArray (com.tom_roush.pdfbox.cos.COSArray)1 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)1 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)1 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)1 AccessPermission (com.tom_roush.pdfbox.pdmodel.encryption.AccessPermission)1 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)1 PDFTextStripper (com.tom_roush.pdfbox.text.PDFTextStripper)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1