Search in sources :

Example 61 with PDDocument

use of com.tom_roush.pdfbox.pdmodel.PDDocument 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 62 with PDDocument

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

the class MergeAcroFormsTest method testLegacyModeMerge.

/*
     * Test LegacyMode merge
     */
@Test
public void testLegacyModeMerge() throws IOException {
    PDFMergerUtility merger = new PDFMergerUtility();
    File toBeMerged = new File(IN_DIR, "AcroFormForMerge.pdf");
    File pdfOutput = new File(OUT_DIR, "PDFBoxLegacyMerge-SameMerged.pdf");
    merger.setDestinationFileName(pdfOutput.getAbsolutePath());
    merger.addSource(toBeMerged);
    merger.addSource(toBeMerged);
    merger.mergeDocuments(null);
    merger.setAcroFormMergeMode(AcroFormMergeMode.PDFBOX_LEGACY_MODE);
    PDDocument compliantDocument = null;
    PDDocument toBeCompared = null;
    try {
        compliantDocument = PDDocument.load(new File(IN_DIR, "PDFBoxLegacyMerge-SameMerged.pdf"));
        toBeCompared = PDDocument.load(new File(OUT_DIR, "PDFBoxLegacyMerge-SameMerged.pdf"));
        PDAcroForm compliantAcroForm = compliantDocument.getDocumentCatalog().getAcroForm();
        PDAcroForm toBeComparedAcroForm = toBeCompared.getDocumentCatalog().getAcroForm();
        assertEquals("There shall be the same number of root fields", compliantAcroForm.getFields().size(), toBeComparedAcroForm.getFields().size());
        for (PDField compliantField : compliantAcroForm.getFieldTree()) {
            assertNotNull("There shall be a field with the same FQN", toBeComparedAcroForm.getField(compliantField.getFullyQualifiedName()));
            PDField toBeComparedField = toBeComparedAcroForm.getField(compliantField.getFullyQualifiedName());
            compareFieldProperties(compliantField, toBeComparedField);
        }
        for (PDField toBeComparedField : toBeComparedAcroForm.getFieldTree()) {
            assertNotNull("There shall be a field with the same FQN", compliantAcroForm.getField(toBeComparedField.getFullyQualifiedName()));
            PDField compliantField = compliantAcroForm.getField(toBeComparedField.getFullyQualifiedName());
            compareFieldProperties(toBeComparedField, compliantField);
        }
    } finally {
        IOUtils.closeQuietly(compliantDocument);
        IOUtils.closeQuietly(toBeCompared);
    }
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) File(java.io.File) Test(org.junit.Test)

Example 63 with PDDocument

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

the class MergeAcroFormsTest method testAPEntry.

/*
     * PDFBOX-1100 Ensure that after merging the PDFs there is an AP and V entry.
     */
@Test
public void testAPEntry() throws IOException {
    InputStream is1 = null;
    InputStream is2 = null;
    // Merge the PDFs form PDFBOX-1100
    PDFMergerUtility merger = new PDFMergerUtility();
    try {
        File file1 = new File(TARGET_PDF_DIR, "PDFBOX-1100-1.pdf");
        assumeTrue(file1.exists());
        is1 = new FileInputStream(file1);
        File file2 = new File(TARGET_PDF_DIR, "PDFBOX-1100-2.pdf");
        assumeTrue(file2.exists());
        is2 = new FileInputStream(file2);
        File pdfOutput = new File(OUT_DIR, "PDFBOX-1100.pdf");
        merger.setDestinationFileName(pdfOutput.getAbsolutePath());
        merger.addSource(is1);
        merger.addSource(is2);
        merger.mergeDocuments(null);
        // Test merge result
        PDDocument mergedPDF = PDDocument.load(pdfOutput);
        assertEquals("There shall be 2 pages", 2, mergedPDF.getNumberOfPages());
        PDAcroForm acroForm = mergedPDF.getDocumentCatalog().getAcroForm();
        PDField formField = acroForm.getField("Testfeld");
        assertNotNull("There shall be an /AP entry for the field", formField.getCOSObject().getDictionaryObject(COSName.AP));
        assertNotNull("There shall be a /V entry for the field", formField.getCOSObject().getDictionaryObject(COSName.V));
        formField = acroForm.getField("Testfeld2");
        assertNotNull("There shall be an /AP entry for the field", formField.getCOSObject().getDictionaryObject(COSName.AP));
        assertNotNull("There shall be a /V entry for the field", formField.getCOSObject().getDictionaryObject(COSName.V));
        mergedPDF.close();
    } finally {
        IOUtils.closeQuietly(is1);
        IOUtils.closeQuietly(is2);
    }
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 64 with PDDocument

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

the class MergeAcroFormsTest method testAnnotsEntry.

/*
     * PDFBOX-1031 Ensure that after merging the PDFs there is an Annots entry per page.
     */
@Test
public void testAnnotsEntry() throws IOException {
    InputStream s1 = null;
    InputStream s2 = null;
    // Merge the PDFs form PDFBOX-1031
    PDFMergerUtility merger = new PDFMergerUtility();
    try {
        File f1 = new File(TARGET_PDF_DIR, "PDFBOX-1031-1.pdf");
        assumeTrue(f1.exists());
        s1 = new FileInputStream(f1);
        File f2 = new File(TARGET_PDF_DIR, "PDFBOX-1031-2.pdf");
        assumeTrue(f2.exists());
        s2 = new FileInputStream(f2);
        File pdfOutput = new File(OUT_DIR, "PDFBOX-1031.pdf");
        merger.setDestinationFileName(pdfOutput.getAbsolutePath());
        merger.addSource(s1);
        merger.addSource(s2);
        merger.mergeDocuments(null);
        // Test merge result
        PDDocument mergedPDF = PDDocument.load(pdfOutput);
        assertEquals("There shall be 2 pages", 2, mergedPDF.getNumberOfPages());
        assertNotNull("There shall be an /Annots entry for the first page", mergedPDF.getPage(0).getCOSObject().getDictionaryObject(COSName.ANNOTS));
        assertEquals("There shall be 1 annotation for the first page", 1, mergedPDF.getPage(0).getAnnotations().size());
        assertNotNull("There shall be an /Annots entry for the second page", mergedPDF.getPage(1).getCOSObject().getDictionaryObject(COSName.ANNOTS));
        assertEquals("There shall be 1 annotation for the second page", 1, mergedPDF.getPage(0).getAnnotations().size());
        mergedPDF.close();
    } finally {
        IOUtils.closeQuietly(s1);
        IOUtils.closeQuietly(s2);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 65 with PDDocument

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

the class PDFCloneUtilityTest method testClonePDFWithCosArrayStream.

/**
 * original (minimal) test from PDFBOX-2052.
 *
 * @throws IOException
 */
public void testClonePDFWithCosArrayStream() throws IOException {
    PDDocument srcDoc = new PDDocument();
    PDDocument dstDoc = new PDDocument();
    PDPage pdPage = new PDPage();
    srcDoc.addPage(pdPage);
    new PDPageContentStream(srcDoc, pdPage, AppendMode.APPEND, true).close();
    new PDPageContentStream(srcDoc, pdPage, AppendMode.APPEND, true).close();
    new PDFCloneUtility(dstDoc).cloneForNewDocument(pdPage.getCOSObject());
    srcDoc.close();
    dstDoc.close();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream)

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