Search in sources :

Example 1 with PDComplexFileSpecification

use of com.tom_roush.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification 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 2 with PDComplexFileSpecification

use of com.tom_roush.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification in project PdfBox-Android by TomRoush.

the class TestSymmetricKeyEncryption method extractEmbeddedFile.

// extract the embedded file, saves it, and return the extracted saved file
private File extractEmbeddedFile(InputStream pdfInputStream, String name) throws IOException {
    PDDocument docWithEmbeddedFile;
    docWithEmbeddedFile = PDDocument.load(pdfInputStream);
    PDDocumentCatalog catalog = docWithEmbeddedFile.getDocumentCatalog();
    PDDocumentNameDictionary names = catalog.getNames();
    PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles();
    Map<String, PDComplexFileSpecification> embeddedFileNames = embeddedFiles.getNames();
    assertEquals(1, embeddedFileNames.size());
    Map.Entry<String, PDComplexFileSpecification> entry = embeddedFileNames.entrySet().iterator().next();
    Log.i("PdfBox-Android", "Processing embedded file " + entry.getKey() + ":");
    PDComplexFileSpecification complexFileSpec = entry.getValue();
    PDEmbeddedFile embeddedFile = complexFileSpec.getEmbeddedFile();
    File resultFile = new File(testResultsDir, name);
    FileOutputStream fos = new FileOutputStream(resultFile);
    InputStream is = embeddedFile.createInputStream();
    IOUtils.copy(is, fos);
    fos.close();
    is.close();
    Log.i("PdfBox-Android", "  size: " + embeddedFile.getSize());
    assertEquals(embeddedFile.getSize(), resultFile.length());
    return resultFile;
}
Also used : PDEmbeddedFilesNameTreeNode(com.tom_roush.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode) PDEmbeddedFile(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PDComplexFileSpecification(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog) PDDocumentNameDictionary(com.tom_roush.pdfbox.pdmodel.PDDocumentNameDictionary) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) FileOutputStream(java.io.FileOutputStream) Map(java.util.Map) PDEmbeddedFile(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile) File(java.io.File)

Example 3 with PDComplexFileSpecification

use of com.tom_roush.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification in project PdfBox-Android by TomRoush.

the class EndstreamOutputStreamTest method testPDFBox2079EmbeddedFile.

@Test
public void testPDFBox2079EmbeddedFile() throws IOException {
    // there should be 17660 bytes in the zip file.
    // in PDFBox 1.8.5, windows newline is appended to the byte stream
    // yielding 17662 bytes, which causes a problem for ZipFile in Java 1.6
    // Modification of embedded_zip.pdf for 2.0:
    // /Length entry removed to force usage of EndstreamOutputStream
    PDDocument doc = PDDocument.load(new File("src/test/resources/pdfbox/com/tom_roush/pdfbox/pdfparser", "embedded_zip.pdf"));
    PDDocumentCatalog catalog = doc.getDocumentCatalog();
    PDDocumentNameDictionary names = catalog.getNames();
    PDEmbeddedFilesNameTreeNode node = names.getEmbeddedFiles();
    Map<String, PDComplexFileSpecification> map = node.getNames();
    Assert.assertEquals(1, map.size());
    PDComplexFileSpecification spec = map.get("My first attachment");
    PDEmbeddedFile file = spec.getEmbeddedFile();
    InputStream input = file.createInputStream();
    File d = new File("target/test-output");
    d.mkdirs();
    File f = new File(d, spec.getFile());
    OutputStream os = new FileOutputStream(f);
    IOUtils.copy(input, os);
    os.close();
    Assert.assertEquals(17660, f.length());
    doc.close();
}
Also used : PDEmbeddedFilesNameTreeNode(com.tom_roush.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode) PDEmbeddedFile(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) PDEmbeddedFile(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile) 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 4 with PDComplexFileSpecification

use of com.tom_roush.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification in project PdfBox-Android by TomRoush.

the class TestEmbeddedFiles method testOSSpecificAttachments.

@Test
public void testOSSpecificAttachments() throws IOException {
    PDEmbeddedFile nonOSFile = null;
    PDEmbeddedFile macFile = null;
    PDEmbeddedFile dosFile = null;
    PDEmbeddedFile unixFile = null;
    PDDocument doc = PDDocument.load(TestEmbeddedFiles.class.getResourceAsStream("/pdfbox/com/tom_roush/pdfbox/pdmodel/common/testPDF_multiFormatEmbFiles.pdf"));
    PDDocumentCatalog catalog = doc.getDocumentCatalog();
    PDDocumentNameDictionary names = catalog.getNames();
    PDEmbeddedFilesNameTreeNode treeNode = names.getEmbeddedFiles();
    List<PDNameTreeNode<PDComplexFileSpecification>> kids = treeNode.getKids();
    for (PDNameTreeNode<PDComplexFileSpecification> kid : kids) {
        Map<String, PDComplexFileSpecification> tmpNames = kid.getNames();
        COSObjectable obj = tmpNames.get("My first attachment");
        PDComplexFileSpecification spec = (PDComplexFileSpecification) obj;
        nonOSFile = spec.getEmbeddedFile();
        macFile = spec.getEmbeddedFileMac();
        dosFile = spec.getEmbeddedFileDos();
        unixFile = spec.getEmbeddedFileUnix();
    }
    assertTrue("non os specific", byteArrayContainsLC("non os specific", nonOSFile.toByteArray(), "ISO-8859-1"));
    assertTrue("mac", byteArrayContainsLC("mac embedded", macFile.toByteArray(), "ISO-8859-1"));
    assertTrue("dos", byteArrayContainsLC("dos embedded", dosFile.toByteArray(), "ISO-8859-1"));
    assertTrue("unix", byteArrayContainsLC("unix embedded", unixFile.toByteArray(), "ISO-8859-1"));
}
Also used : PDEmbeddedFilesNameTreeNode(com.tom_roush.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode) PDEmbeddedFile(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile) PDComplexFileSpecification(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog) PDDocumentNameDictionary(com.tom_roush.pdfbox.pdmodel.PDDocumentNameDictionary) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) Test(org.junit.Test)

Aggregations

PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)4 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)4 PDDocumentNameDictionary (com.tom_roush.pdfbox.pdmodel.PDDocumentNameDictionary)4 PDEmbeddedFilesNameTreeNode (com.tom_roush.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode)4 PDComplexFileSpecification (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification)4 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)4 Test (org.junit.Test)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 Map (java.util.Map)1