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

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

Introduction

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

Prototype

public PdfReader(RandomAccessFileOrArray raf, byte ownerPassword[]) throws IOException 

Source Link

Document

Reads and parses a pdf document.

Usage

From source file:it.pdfsam.console.tools.pdf.PdfSplit.java

License:Open Source License

/**
 * Execute the split command. On error an exception is thrown.
 * //from w  w  w. j av a  2  s  .c  o m
 * @throws SplitException
 */
public void execute() throws SplitException {
    try {
        percentageChanged(0);
        out_message = "";
        PdfReader pdf_reader = new PdfReader(new RandomAccessFileOrArray(f_file.getCanonicalPath()), null);
        prefixParser = new PrefixParser(prefix_value, f_file.getName());
        //we retrieve the total number of pages
        n = pdf_reader.getNumberOfPages();
        // apply format for output files name with leading zero(s)
        try {
            file_number_formatter.applyPattern(Integer.toString(n).replaceAll("\\d", "0"));
        } catch (Exception fe) {
            file_number_formatter.applyPattern("00000");
        }
        out_message += LogFormatter.formatMessage("There are " + n + " pages in this document\n");
        // -s ODD EVEN
        if (split_type.equals(CmdParser.S_ODD) || split_type.equals(CmdParser.S_EVEN)) {
            doSplitOddEven(pdf_reader);
        } else
        // -s BURST
        if (split_type.equals(CmdParser.S_BURST)) {
            doSplitBurst(pdf_reader);
        } else
        // -s SPLIT
        if (split_type.equals(CmdParser.S_SPLIT)) {
            doSplitSplit(pdf_reader);
        } else
        // -s NSPLIT
        if (split_type.equals(CmdParser.S_NSPLIT)) {
            doSplitNSplit(pdf_reader);
        }
        pdf_reader.close();
    } catch (Exception e) {
        throw new SplitException(e);
    } finally {
        workCompleted();
    }

}

From source file:it.pdfsam.plugin.coverfooter.GUI.CoverFooterMainGUI.java

License:Open Source License

private void addTableRow(File file_to_add) {
    if (file_to_add != null) {
        boolean encrypt = false;
        String num_pages = "";
        try {//from  w w  w. j  a  v  a 2 s .c om
            //fix 03/07 for memory usage
            PdfReader pdf_reader = new PdfReader(new RandomAccessFileOrArray(file_to_add.getAbsolutePath()),
                    null);
            encrypt = pdf_reader.isEncrypted();
            // we retrieve the total number of pages
            num_pages = Integer.toString(pdf_reader.getNumberOfPages());
            pdf_reader.close();
        } catch (Exception ex) {
            num_pages = ex.getMessage();
        }
        try {
            modello_cover_table.addRow(new CoverFooterItemType(file_to_add.getName(),
                    file_to_add.getAbsolutePath(), num_pages, CoverFooterMainGUI.ALL_STRING, encrypt));
            fireLogPropertyChanged(
                    GettextResource.gettext(i18n_messages, "File selected: ") + file_to_add.getName(),
                    LogPanel.LOG_INFO);
        } catch (Exception ex) {
            fireLogPropertyChanged(GettextResource.gettext(i18n_messages, "Error: ") + ex.getMessage(),
                    LogPanel.LOG_ERROR);

        }
    }
}

From source file:it.pdfsam.plugin.coverfooter.GUI.CoverFooterMainGUI.java

License:Open Source License

private void addTableRowsFromNode(Node file_node) {
    if (file_node != null) {
        boolean encrypt = false;
        String num_pages = "";
        String page_selection = "";
        File file_to_add = null;//  ww  w . j a va2 s  . c om
        try {
            Node file_name = (Node) file_node.selectSingleNode("@name");
            if (file_name != null) {
                file_to_add = new File(file_name.getText());
            }
        } catch (Exception ex) {
            file_to_add = null;
            fireLogPropertyChanged(GettextResource.gettext(i18n_messages, "Error: ") + ex.getMessage(),
                    LogPanel.LOG_ERROR);
        }
        try {
            if (file_to_add != null) {
                PdfReader pdf_reader = new PdfReader(new RandomAccessFileOrArray(file_to_add.getAbsolutePath()),
                        null);
                encrypt = pdf_reader.isEncrypted();
                // we retrieve the total number of pages
                num_pages = Integer.toString(pdf_reader.getNumberOfPages());
                pdf_reader.close();
            }
        } catch (Exception ex) {
            num_pages = ex.getMessage();
        }
        try {
            Node file_pages = (Node) file_node.selectSingleNode("@pageselection");
            if (file_pages != null) {
                page_selection = file_pages.getText();
            } else {
                page_selection = CoverFooterMainGUI.ALL_STRING;
            }
        } catch (Exception ex) {
            page_selection = CoverFooterMainGUI.ALL_STRING;
        }
        try {
            modello_cover_table.addRow(new CoverFooterItemType(file_to_add.getName(),
                    file_to_add.getAbsolutePath(), num_pages, page_selection, encrypt));
            fireLogPropertyChanged(
                    GettextResource.gettext(i18n_messages, "File selected: ") + file_to_add.getName(),
                    LogPanel.LOG_INFO);
        } catch (Exception ex) {
            fireLogPropertyChanged(GettextResource.gettext(i18n_messages, "Error: ") + ex.getMessage(),
                    LogPanel.LOG_ERROR);

        }
    }
}

From source file:it.pdfsam.plugin.merge.GUI.MergeMainGUI.java

License:Open Source License

private void addTableRow(File file_to_add) {
    if (file_to_add != null) {
        boolean encrypt = false;
        String num_pages = "";
        try {//from  w  w w .j  a  v  a  2 s  . co  m
            //fix 03/07 for memory usage
            PdfReader pdf_reader = new PdfReader(new RandomAccessFileOrArray(file_to_add.getAbsolutePath()),
                    null);
            encrypt = pdf_reader.isEncrypted();
            // we retrieve the total number of pages
            num_pages = Integer.toString(pdf_reader.getNumberOfPages());
            pdf_reader.close();
        } catch (Exception ex) {
            num_pages = ex.getMessage();
        }
        try {
            modello_merge_table.addRow(new MergeItemType(file_to_add.getName(), file_to_add.getAbsolutePath(),
                    num_pages, MergeMainGUI.ALL_STRING, encrypt));
            fireLogPropertyChanged(
                    GettextResource.gettext(i18n_messages, "File selected: ") + file_to_add.getName(),
                    LogPanel.LOG_INFO);
        } catch (Exception ex) {
            fireLogPropertyChanged(GettextResource.gettext(i18n_messages, "Error: ") + ex.getMessage(),
                    LogPanel.LOG_ERROR);

        }
    }
}

From source file:it.pdfsam.plugin.merge.GUI.MergeMainGUI.java

License:Open Source License

private void addTableRowsFromNode(Node file_node) {
    if (file_node != null) {
        boolean encrypt = false;
        String num_pages = "";
        String page_selection = "";
        File file_to_add = null;/*from   ww  w  . j  a v  a  2  s.  co  m*/
        try {
            Node file_name = (Node) file_node.selectSingleNode("@name");
            if (file_name != null) {
                file_to_add = new File(file_name.getText());
            }
        } catch (Exception ex) {
            file_to_add = null;
            fireLogPropertyChanged(GettextResource.gettext(i18n_messages, "Error: ") + ex.getMessage(),
                    LogPanel.LOG_ERROR);
        }
        try {
            if (file_to_add != null) {
                PdfReader pdf_reader = new PdfReader(new RandomAccessFileOrArray(file_to_add.getAbsolutePath()),
                        null);
                encrypt = pdf_reader.isEncrypted();
                // we retrieve the total number of pages
                num_pages = Integer.toString(pdf_reader.getNumberOfPages());
                pdf_reader.close();
            }
        } catch (Exception ex) {
            num_pages = ex.getMessage();
        }
        try {
            Node file_pages = (Node) file_node.selectSingleNode("@pageselection");
            if (file_pages != null) {
                page_selection = file_pages.getText();
            } else {
                page_selection = MergeMainGUI.ALL_STRING;
            }
        } catch (Exception ex) {
            page_selection = MergeMainGUI.ALL_STRING;
        }
        try {
            modello_merge_table.addRow(new MergeItemType(file_to_add.getName(), file_to_add.getAbsolutePath(),
                    num_pages, page_selection, encrypt));
            fireLogPropertyChanged(
                    GettextResource.gettext(i18n_messages, "File selected: ") + file_to_add.getName(),
                    LogPanel.LOG_INFO);
        } catch (Exception ex) {
            fireLogPropertyChanged(GettextResource.gettext(i18n_messages, "Error: ") + ex.getMessage(),
                    LogPanel.LOG_ERROR);

        }
    }
}

From source file:lucee.runtime.text.pdf.PDFDocument.java

License:Open Source License

public PdfReader getPdfReader() throws ApplicationException {
    try {//  ww  w.  jav a 2s .  com
        if (barr != null) {
            if (password != null)
                return new PdfReader(barr, password.getBytes());
            return new PdfReader(barr);
        }
        if (password != null)
            return new PdfReader(IOUtil.toBytes(resource), password.getBytes());
        return new PdfReader(IOUtil.toBytes(resource));
    } catch (IOException ioe) {
        throw new ApplicationException("can not load file [" + resource + "]", ioe.getMessage());
    }
}

From source file:lucee.runtime.text.pdf.PDFUtil.java

License:Open Source License

public static PdfReader toPdfReader(PageContext pc, Object value, String password)
        throws IOException, PageException {
    if (value instanceof PdfReader)
        return (PdfReader) value;
    if (value instanceof PDFDocument)
        return ((PDFDocument) value).getPdfReader();
    if (Decision.isBinary(value)) {
        if (password != null)
            return new PdfReader(Caster.toBinary(value), password.getBytes());
        return new PdfReader(Caster.toBinary(value));
    }/*from  w w w .ja  va  2s .  c  o  m*/
    if (value instanceof Resource) {
        if (password != null)
            return new PdfReader(IOUtil.toBytes((Resource) value), password.getBytes());
        return new PdfReader(IOUtil.toBytes((Resource) value));
    }
    if (value instanceof String) {
        if (password != null)
            return new PdfReader(IOUtil.toBytes(Caster.toResource(pc, value, true)), password.getBytes());
        return new PdfReader(IOUtil.toBytes((Resource) value));
    }
    throw new CasterException(value, PdfReader.class);
}

From source file:net.laubenberger.bogatyr.helper.HelperPdf.java

License:Open Source License

/**
 * Removes all restrictions from a PDF with a given password.
 * /*ww w. j  a v  a  2 s  .c  om*/
 * @param source
 *           {@link File}
 * @param dest
 *           {@link File} for the unlocked PDF
 * @param password
 *           of the source {@link File}
 * @throws DocumentException
 * @throws IOException
 * @since 0.9.0
 */
public static void unlock(final File source, final File dest, final byte[] password)
        throws IOException, DocumentException { // $JUnit$
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodStart(source, dest, password));
    if (null == source) {
        throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$
    }
    if (null == dest) {
        throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$
    }
    if (HelperObject.isEquals(source, dest)) {
        throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (null == password) {
        throw new RuntimeExceptionIsNull("password"); //$NON-NLS-1$
    }
    if (!HelperArray.isValid(password)) {
        throw new RuntimeExceptionIsEmpty("password"); //$NON-NLS-1$
    }

    PdfReader reader = null;
    OutputStream os = null;

    try {
        reader = new PdfReader(source.getAbsolutePath(), password);
        os = new FileOutputStream(dest);

        PdfEncryptor.encrypt(reader, os, null, null,
                PdfWriter.ALLOW_ASSEMBLY | PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_DEGRADED_PRINTING
                        | PdfWriter.ALLOW_FILL_IN | PdfWriter.ALLOW_MODIFY_ANNOTATIONS
                        | PdfWriter.ALLOW_MODIFY_CONTENTS | PdfWriter.ALLOW_PRINTING
                        | PdfWriter.ALLOW_SCREENREADERS,
                true);
    } finally {
        if (null != os) {
            os.close();
        }
        if (null != reader) {
            reader.close();
        }
    }
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodExit());
}

From source file:net.laubenberger.wichtel.helper.HelperPdf.java

License:Open Source License

/**
 * Removes all restrictions from a PDF with a given password.
 * /*from   w  w  w.  ja  va 2 s  .c  om*/
 * @param source
 *           {@link File}
 * @param dest
 *           {@link File} for the unlocked PDF
 * @param password
 *           of the source {@link File}
 * @throws DocumentException
 * @throws IOException
 * @since 0.0.1
 */
public static void unlock(final File source, final File dest, final byte... password)
        throws IOException, DocumentException { // $JUnit$
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodStart(source, dest, password));
    if (null == source) {
        throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$
    }
    if (null == dest) {
        throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$
    }
    if (HelperObject.isEquals(source, dest)) {
        throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (null == password) {
        throw new RuntimeExceptionIsNull("password"); //$NON-NLS-1$
    }
    if (!HelperArray.isValid(password)) {
        throw new RuntimeExceptionIsEmpty("password"); //$NON-NLS-1$
    }

    PdfReader reader = null;

    try (OutputStream os = new FileOutputStream(dest)) {
        reader = new PdfReader(source.getAbsolutePath(), password);

        PdfEncryptor.encrypt(reader, os, null, null,
                PdfWriter.ALLOW_ASSEMBLY | PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_DEGRADED_PRINTING
                        | PdfWriter.ALLOW_FILL_IN | PdfWriter.ALLOW_MODIFY_ANNOTATIONS
                        | PdfWriter.ALLOW_MODIFY_CONTENTS | PdfWriter.ALLOW_PRINTING
                        | PdfWriter.ALLOW_SCREENREADERS,
                true);
    } finally {
        if (null != reader) {
            reader.close();
        }
    }
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodExit());
}

From source file:net.sf.jsignpdf.PdfExtraInfo.java

License:Mozilla Public License

/**
 * Returns number of pages in PDF document. If error occures (file not found
 * or sth. similar) -1 is returned./*from w  ww.  j a  v  a 2s  .co m*/
 * 
 * @return number of pages (or -1 if error occures)
 */
public int getNumberOfPages() {
    int tmpResult = 0;
    PdfReader reader = null;
    try {
        try {
            reader = new PdfReader(options.getInFile(), options.getPdfOwnerPwdStrX().getBytes());
        } catch (Exception e) {
            try {
                reader = new PdfReader(options.getInFile(), new byte[0]);
            } catch (Exception e2) {
                // try to read without password
                reader = new PdfReader(options.getInFile());
            }
        }
        tmpResult = reader.getNumberOfPages();
    } catch (Exception e) {
        tmpResult = -1;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
            }
        }
    }

    return tmpResult;
}