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

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

Introduction

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

Prototype

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

Source Link

Document

Reads and parses a pdf document.

Usage

From source file:org.gmdev.pdftrick.engine.MergeFiles.java

License:Open Source License

/**
 * Materially multiple pdf files are written merged file on a disk 
 * @param list//  www  .  j a v  a  2  s.com
 * @param outputStream
 * @throws DocumentException
 * @throws IOException
 */
private void doMerge(List<StreamPwdContainer> list, OutputStream outputStream)
        throws DocumentException, IOException {
    HashMap<Integer, String> rotationFromPages = factory.getRotationFromPages();
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    document.open();
    PdfContentByte cb = writer.getDirectContent();

    int z = 0;
    for (StreamPwdContainer boom : list) {

        InputStream in = boom.getIn();
        PdfReader reader = null;
        if (!boom.getPwd().equalsIgnoreCase("")) {
            reader = new PdfReader(in, boom.getPwd().getBytes());
        } else {
            reader = new PdfReader(in);
        }

        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            z++;
            int rotation = reader.getPageRotation(i);

            //set size
            Rectangle pageSize_ = reader.getPageSize(i);
            Rectangle pageSize = null;
            if (rotation == 270 || rotation == 90) {
                pageSize = new Rectangle(pageSize_.getHeight(), pageSize_.getWidth());
            } else {
                pageSize = pageSize_;
            }

            document.setPageSize(pageSize);
            writer.setCropBoxSize(pageSize);

            document.newPage();

            // import the page from source pdf
            PdfImportedPage page = writer.getImportedPage(reader, i);

            // add the page to the destination pdf
            if (rotation == 270) {
                cb.addTemplate(page, 0, 1.0f, -1.0f, 0, reader.getPageSizeWithRotation(i).getWidth(), 0);
                rotationFromPages.put(z, "" + rotation);
            } else if (rotation == 180) {
                cb.addTemplate(page, -1f, 0, 0, -1f, 0, 0);
                rotationFromPages.put(z, "" + rotation);
            } else if (rotation == 90) {
                cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight());
                rotationFromPages.put(z, "" + rotation);
            } else {
                cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
            }
        }
        in.close();
    }
    outputStream.flush();
    document.close();
    outputStream.close();
}

From source file:org.h819.commons.file.MyPDFUtils.java

/**
 *  PdfReader ? pdf  pdf ???/* w  w  w  .ja  v  a  2  s. c o m*/
 *
 * @param pdfFile ? pdf 
 * @return PdfReader 
 * @throws IOException
 */
public static PdfReader getPdfReader(File pdfFile) throws IOException {

    Document.plainRandomAccess = true;
    FileInputStream fileStream = new FileInputStream(pdfFile);
    return new PdfReader(
            new RandomAccessFileOrArray(new FileChannelRandomAccessSource(fileStream.getChannel())), null);

}

From source file:org.h819.commons.file.MyPDFUtils.java

/**
 *  PdfReader ? pdf  pdf ??/*from w ww . j a  v  a2  s  . c  om*/
 *
 * @param pdfFile       ? pdf 
 * @param ownerPassword ?
 * @return
 * @throws IOException
 */
public static PdfReader getPdfReader(File pdfFile, String ownerPassword) throws IOException {

    FileInputStream fileStream = new FileInputStream(pdfFile);

    if (ownerPassword == null)
        return getPdfReader(pdfFile);

    return new PdfReader(
            new RandomAccessFileOrArray(new FileChannelRandomAccessSource(fileStream.getChannel())),
            ownerPassword.getBytes());
}

From source file:org.sejda.impl.itext5.component.DefaultPdfSourceOpener.java

License:Open Source License

private PdfReader doOpen(RandomAccessSource ras, byte[] pwd) throws TaskIOException, IOException {
    PdfReader reader;/*from  w w w  .  java2s .  c om*/
    try {
        reader = new PdfReader(new RandomAccessFileOrArray(ras), pwd);
    } catch (BadPasswordException bpe) {
        throw new TaskWrongPasswordException("Unable to open the document due to a wrong password.", bpe);
    }

    reader.removeUnusedObjects();
    reader.consolidateNamedDestinations();
    return reader;
}