Search in sources :

Example 26 with PDDocument

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

the class PDStreamTest method testCreateInputStreamEmptyFilters.

/**
 * Test for empty filter list
 */
@Test
public void testCreateInputStreamEmptyFilters() throws Exception {
    PDDocument doc = new PDDocument();
    InputStream is = new ByteArrayInputStream(new byte[] { 12, 34, 56, 78 });
    PDStream pdStream = new PDStream(doc, is, new COSArray());
    Assert.assertEquals(0, pdStream.getFilters().size());
    List<String> stopFilters = new ArrayList<String>();
    stopFilters.add(COSName.DCT_DECODE.toString());
    stopFilters.add(COSName.DCT_DECODE_ABBREVIATION.toString());
    is = pdStream.createInputStream(stopFilters);
    Assert.assertEquals(12, is.read());
    Assert.assertEquals(34, is.read());
    Assert.assertEquals(56, is.read());
    Assert.assertEquals(78, is.read());
    Assert.assertEquals(-1, is.read());
    doc.close();
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 27 with PDDocument

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

the class PDStreamTest method testCreateInputStreamNullFilters.

/**
 * Test for null filter list (PDFBOX-2948)
 */
@Test
public void testCreateInputStreamNullFilters() throws Exception {
    PDDocument doc = new PDDocument();
    InputStream is = new ByteArrayInputStream(new byte[] { 12, 34, 56, 78 });
    PDStream pdStream = new PDStream(doc, is, (COSArray) null);
    Assert.assertNull(pdStream.getFilters());
    List<String> stopFilters = new ArrayList<String>();
    stopFilters.add(COSName.DCT_DECODE.toString());
    stopFilters.add(COSName.DCT_DECODE_ABBREVIATION.toString());
    is = pdStream.createInputStream(stopFilters);
    Assert.assertEquals(12, is.read());
    Assert.assertEquals(34, is.read());
    Assert.assertEquals(56, is.read());
    Assert.assertEquals(78, is.read());
    Assert.assertEquals(-1, is.read());
    doc.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 28 with PDDocument

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

the class PDStreamTest method testCreateInputStreamNullStopFilters.

/**
 * Test for null stop filters
 */
@Test
public void testCreateInputStreamNullStopFilters() throws Exception {
    PDDocument doc = new PDDocument();
    InputStream is = new ByteArrayInputStream(new byte[] { 12, 34, 56, 78 });
    PDStream pdStream = new PDStream(doc, is, new COSArray());
    Assert.assertEquals(0, pdStream.getFilters().size());
    is = pdStream.createInputStream((List<String>) null);
    Assert.assertEquals(12, is.read());
    Assert.assertEquals(34, is.read());
    Assert.assertEquals(56, is.read());
    Assert.assertEquals(78, is.read());
    Assert.assertEquals(-1, is.read());
    doc.close();
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 29 with PDDocument

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

the class TestEmbeddedFiles method testNullEmbeddedFile.

@Test
public void testNullEmbeddedFile() throws IOException {
    PDEmbeddedFile embeddedFile = null;
    boolean ok = false;
    try {
        PDDocument doc = PDDocument.load(getClass().getResourceAsStream("/pdfbox/com/tom_roush/pdfbox/pdmodel/common/null_PDComplexFileSpecification.pdf"));
        PDDocumentCatalog catalog = doc.getDocumentCatalog();
        PDDocumentNameDictionary names = catalog.getNames();
        assertEquals("expected two files", 2, names.getEmbeddedFiles().getNames().size());
        PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles();
        PDComplexFileSpecification spec = embeddedFiles.getNames().get("non-existent-file.docx");
        if (spec != null) {
            embeddedFile = spec.getEmbeddedFile();
            ok = true;
        }
        // now test for actual attachment
        spec = embeddedFiles.getNames().get("My first attachment");
        assertNotNull("one attachment actually exists", spec);
        assertEquals("existing file length", 17660, spec.getEmbeddedFile().getLength());
        spec = embeddedFiles.getNames().get("non-existent-file.docx");
    } catch (NullPointerException e) {
        assertNotNull("null pointer exception", null);
    }
    assertTrue("Was able to get file without exception", ok);
    assertNull("EmbeddedFile was correctly null", embeddedFile);
}
Also used : PDEmbeddedFilesNameTreeNode(com.tom_roush.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode) PDEmbeddedFile(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDComplexFileSpecification(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog) PDDocumentNameDictionary(com.tom_roush.pdfbox.pdmodel.PDDocumentNameDictionary) Test(org.junit.Test)

Example 30 with PDDocument

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

the class TestOptionalContentGroups method testOCGConsumption.

/**
 * Tests OCG functions on a loaded PDF.
 * @throws Exception if an error occurs
 */
public void testOCGConsumption() throws Exception {
    File pdfFile = new File(testResultsDir, "ocg-generation.pdf");
    if (!pdfFile.exists()) {
        testOCGGeneration();
    }
    PDDocument doc = PDDocument.load(pdfFile);
    try {
        assertEquals(1.5f, doc.getVersion());
        PDDocumentCatalog catalog = doc.getDocumentCatalog();
        PDPage page = doc.getPage(0);
        PDResources resources = page.getResources();
        COSName mc0 = COSName.getPDFName("oc1");
        PDOptionalContentGroup ocg = (PDOptionalContentGroup) resources.getProperties(mc0);
        assertNotNull(ocg);
        assertEquals("background", ocg.getName());
        assertNull(resources.getProperties(COSName.getPDFName("inexistent")));
        PDOptionalContentProperties ocgs = catalog.getOCProperties();
        assertEquals(BaseState.ON, ocgs.getBaseState());
        Set<String> names = new java.util.HashSet<String>(Arrays.asList(ocgs.getGroupNames()));
        assertEquals(3, names.size());
        assertTrue(names.contains("background"));
        assertTrue(ocgs.isGroupEnabled("background"));
        assertTrue(ocgs.isGroupEnabled("enabled"));
        assertFalse(ocgs.isGroupEnabled("disabled"));
        ocgs.setGroupEnabled("background", false);
        assertFalse(ocgs.isGroupEnabled("background"));
        PDOptionalContentGroup background = ocgs.getGroup("background");
        assertEquals(ocg.getName(), background.getName());
        assertNull(ocgs.getGroup("inexistent"));
        Collection<PDOptionalContentGroup> coll = ocgs.getOptionalContentGroups();
        assertEquals(3, coll.size());
        Set<String> nameSet = new HashSet<String>();
        for (PDOptionalContentGroup ocg2 : coll) {
            nameSet.add(ocg2.getName());
        }
        assertTrue(nameSet.contains("background"));
        assertTrue(nameSet.contains("enabled"));
        assertTrue(nameSet.contains("disabled"));
    } finally {
        doc.close();
    }
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog) COSName(com.tom_roush.pdfbox.cos.COSName) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) File(java.io.File) HashSet(java.util.HashSet)

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