Example usage for com.lowagie.text.pdf PdfReader getStreamBytes

List of usage examples for com.lowagie.text.pdf PdfReader getStreamBytes

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfReader getStreamBytes.

Prototype

public static byte[] getStreamBytes(PRStream stream) throws IOException 

Source Link

Document

Get the content from a stream applying the required filters.

Usage

From source file:net.sqs2.omr.master.sqm.PDFAttachmentExtractor.java

License:Apache License

/**
 * Unpacks a file attachment./*from   ww w.  ja v  a  2 s  .  co m*/
 * 
 * @param reader
 *            The object that reads the PDF document
 * @param filespec
 *            The dictionary containing the file specifications
 * @param outPath
 *            The path where the attachment has to be written
 * @throws IOException
 */
private static byte[] unpackFile(PdfReader reader, PdfDictionary filespec, String suffix) throws IOException {
    if (filespec == null) {
        return null;
    }
    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));
    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type)) {
        return null;
    }
    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null) {
        return null;
    }
    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null) {
        return null;
    }
    File fLast = new File(fn.toUnicodeString());
    String filename = fLast.getName();

    if (!filename.endsWith(suffix)) {
        return null;
    }

    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null) {
        return null;
    }
    return PdfReader.getStreamBytes(prs);
}

From source file:org.kuali.coeus.propdev.impl.budget.subaward.PropDevPropDevBudgetSubAwardServiceImpl.java

License:Open Source License

/**
 * Unpacks a file attachment./*  www. jav a 2 s.c  o  m*/
 * @param reader The object that reads the PDF document
 * @param filespec The dictonary containing the file specifications
 * @throws IOException
 */

protected static Object[] unpackFile(PdfReader reader, PdfDictionary filespec) throws IOException {
    Object arr[] = new Object[2]; //use to store name and file bytes
    if (filespec == null) {
        return null;
    }

    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));
    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type)) {
        return null;
    }

    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null) {
        return null;
    }

    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null) {
        return null;
    }

    File fLast = new File(fn.toUnicodeString());
    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null) {
        return null;
    }

    byte attachmentByte[] = PdfReader.getStreamBytes(prs);
    arr[0] = fLast.getName();
    arr[1] = attachmentByte;

    return arr;

}

From source file:org.kuali.coeus.propdev.impl.s2s.S2sUserAttachedFormServiceImpl.java

License:Open Source License

/**
 * Unpacks a file attachment./*from  www  .  j  a  v  a2  s  .  co m*/
 * @param filespec
 *            The dictonary containing the file specifications
 * @throws IOException
 */
private Object[] unpackFile(PdfDictionary filespec) throws IOException {

    if (filespec == null)
        return null;

    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));

    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type))
        return null;

    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null)
        return null;

    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null)
        return null;

    File fLast = new File(fn.toUnicodeString());

    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null)
        return null;

    byte attachmentByte[] = PdfReader.getStreamBytes(prs);
    Object[] fileInfo = new Object[2];
    fileInfo[0] = fLast.getName();
    fileInfo[1] = attachmentByte;
    return fileInfo;
}

From source file:org.kuali.kra.s2s.service.impl.S2SUserAttachedFormServiceImpl.java

License:Educational Community License

/**
 * Unpacks a file attachment./*  www .  ja v  a  2s  .  co m*/
 *
 * @param reader
 *            The object that reads the PDF document
 * @param filespec
 *            The dictonary containing the file specifications
 * @throws IOException
 */
private Object[] unpackFile(PdfReader reader, PdfDictionary filespec) throws IOException {
    S2sUserAttachedFormAtt userAttachedS2SFormAttachmentBean = new S2sUserAttachedFormAtt();

    if (filespec == null)
        return null;

    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));

    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type))
        return null;

    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null)
        return null;

    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null)
        return null;

    File fLast = new File(fn.toUnicodeString());

    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null)
        return null;

    byte attachmentByte[] = PdfReader.getStreamBytes(prs);
    Object[] fileInfo = new Object[2];
    fileInfo[0] = fLast.getName();
    fileInfo[1] = attachmentByte;
    return fileInfo;
}

From source file:org.pdfsam.console.business.pdf.handlers.UnpackCmdExecutor.java

License:Open Source License

/**
 * Unpack//  w w w  . j a  v a  2s  . c  o m
 * @param filespec the dictionary
 * @param outPath output directory
 * @param overwrite if true overwrite if already exists
 * @return number of unpacked files
 * @throws IOException
 */
private int unpackFile(PdfDictionary filespec, File outPath, boolean overwrite) throws IOException {
    int retVal = 0;
    if (filespec != null) {
        PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));
        if (PdfName.F.equals(type) || PdfName.FILESPEC.equals(type)) {
            PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
            PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
            if (fn != null && ef != null) {
                LOG.debug("Unpacking file " + fn + " to " + outPath);
                File fLast = new File(fn.toUnicodeString());
                File fullPath = new File(outPath, fLast.getName());
                if (fullPath.exists()) {
                    //check if overwrite is allowed
                    if (overwrite) {
                        if (!fullPath.delete()) {
                            LOG.warn("Unable to overwrite " + fullPath.getAbsolutePath()
                                    + ", unable to unpack.");
                        }
                    } else {
                        LOG.warn("Cannot overwrite " + fullPath.getAbsolutePath()
                                + " (overwrite is false), unable to unpack.");
                    }
                } else {
                    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
                    if (prs != null) {
                        byte b[] = PdfReader.getStreamBytes(prs);
                        FileOutputStream fout = new FileOutputStream(fullPath);
                        fout.write(b);
                        fout.close();
                        retVal = 1;
                    }
                }
            }
        }
    }
    return retVal;
}

From source file:org.sejda.impl.itext.component.PdfUnpacker.java

License:Apache License

private File copyToTemporaryFile(PRStream prs) throws TaskIOException {
    File tmpFile = createTemporaryBuffer();
    LOG.debug("Created output temporary buffer {}", tmpFile);

    ByteArrayInputStream inputStream = null;
    try {//  w w  w  .  j  a v  a  2s . co  m
        inputStream = new ByteArrayInputStream(PdfReader.getStreamBytes(prs));
        FileUtils.copyInputStreamToFile(inputStream, tmpFile);
        LOG.debug("Attachment unpacked to temporary buffer");
    } catch (IOException e) {
        throw new TaskIOException("Unable to copy attachment to temporary file.", e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return tmpFile;
}

From source file:questions.forms.ReadXfa.java

public static void main(String[] args) {
    try {/*from   ww  w  . j a v  a 2s.com*/
        PdfReader reader = new PdfReader(RESOURCE);
        FileOutputStream os = new FileOutputStream(RESULT);
        PdfDictionary root = reader.getCatalog();
        PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM);
        PdfArray xfa = acroform.getAsArray(PdfName.XFA);
        for (int i = 0; i < xfa.size(); i += 2) {
            System.out.println("Reading: " + xfa.getAsString(i));
            PRStream s = (PRStream) xfa.getAsStream(i + 1);
            os.write(PdfReader.getStreamBytes(s));
        }
        os.flush();
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:questions.metadata.ReplaceXMP.java

public static void alterXmp1() {
    try {//  w  ww  .  java2  s. c o  m
        PdfReader reader = new PdfReader(ORIGINAL);
        PdfDictionary catalog = reader.getCatalog();
        PdfObject obj = catalog.get(PdfName.METADATA);
        PRStream stream = (PRStream) PdfReader.getPdfObject(obj);
        String metadata = new String(PdfReader.getStreamBytes(stream));
        metadata = metadata.replaceAll("Hello World", "Hello Universe");
        stream.setData(metadata.getBytes(), false);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT1));
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}