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

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

Introduction

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

Prototype

public void close() 

Source Link

Document

Closes the reader

Usage

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

License:Open Source License

/**
 * Modifies the meta data of given PDF and stores it in a new {@link File}.
 * <strong>Meta data example:"</strong> metadata.put("Title", "Example");
 * metadata.put("Author", "Stefan Laubenberger"); metadata.put("Subject",
 * "Example PDF meta data"); metadata.put("Keywords", "Example, PDF");
 * metadata.put("Creator", "http://www.laubenberger.net/");
 * metadata.put("Producer", "Silvan Spross");
 * /* w w  w.j ava2 s.c  o m*/
 * @param source
 *           {@link File}
 * @param dest
 *           {@link File} for the modified PDF
 * @param metadata
 *           list with the new meta data informations
 * @throws DocumentException
 * @throws IOException
 * @since 0.0.1
 */
@SuppressWarnings("unchecked")
public static void setMetaData(final File source, final File dest, final Map<String, String> metadata)
        throws IOException, DocumentException { // $JUnit$
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodStart(source, dest, metadata));
    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 == metadata) {
        throw new RuntimeExceptionIsNull("metadata"); //$NON-NLS-1$
    }

    try (FileOutputStream fos = new FileOutputStream(dest)) {
        final PdfReader reader = new PdfReader(source.getAbsolutePath());
        final PdfStamper stamper = new PdfStamper(reader, fos);

        final HashMap<String, String> info = reader.getInfo();
        info.putAll(metadata);

        stamper.setMoreInfo(info);

        stamper.close();
        reader.close();
    }
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodExit());
}

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

License:Open Source License

/**
 * Encrypt a PDF with a given user and password.
 * /*from   w  w w.j a  v  a 2s .  c  o  m*/
 * @param source
 *           {@link File}
 * @param dest
 *           {@link File} for the encrypted PDF
 * @param user
 *           of the dest {@link File}
 * @param password
 *           of the dest {@link File}
 * @throws DocumentException
 * @throws IOException
 * @since 0.0.1
 */
public static void encrypt(final File source, final File dest, final byte[] user, final byte... password)
        throws IOException, DocumentException { // $JUnit$
    if (log.isDebugEnabled())
        log.debug(HelperLog.methodStart(source, dest, user, 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 == user) {
        throw new RuntimeExceptionIsNull("user"); //$NON-NLS-1$
    }
    if (!HelperArray.isValid(user)) {
        throw new RuntimeExceptionIsEmpty("user"); //$NON-NLS-1$
    }
    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());

        PdfEncryptor.encrypt(reader, os, user, password,
                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.laubenberger.wichtel.helper.HelperPdf.java

License:Open Source License

/**
 * Adds a all restrictions to a PDF with a given password.
 * //from   ww w .  ja  va 2s .  c  om
 * @param source
 *           {@link File}
 * @param dest
 *           {@link File} for the locked PDF
 * @param password
 *           of the dest {@link File}
 * @throws DocumentException
 * @throws IOException
 * @since 0.0.1
 */
public static void lock(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());

        PdfEncryptor.encrypt(reader, os, null, password, 0, true);
    } finally {
        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.
 * /* w w w .j  a  v  a 2  s.c o m*/
 * @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.mitnet.tools.pdf.book.publisher.BookPublisher.java

License:Open Source License

/**
 * Returns the number of pages in a PDF file.
 * /*from  ww  w .  j a v  a2s  .  c om*/
 * @param pdfFileName
 * @return
 * @throws IOException
 * 
 * @see http://stackoverflow.com/questions/6026971/page-count-of-pdf-with-java
 */
private int countPdfPages(String filename) throws IOException {
    PdfReader reader = new PdfReader(filename);
    int pageCount = reader.getNumberOfPages();
    reader.close();
    return pageCount;
}

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 www. j  a v  a 2s  .c  o  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;
}

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

License:Mozilla Public License

/**
 * Returns page info./*from  ww w  . j  av  a 2 s  .c  o m*/
 * 
 * @param aPage
 *            number of page for which size should be returned
 * @return FloatPoint or null
 */
public PageInfo getPageInfo(int aPage) {
    PageInfo tmpResult = null;
    PdfReader reader = null;
    try {
        reader = PdfUtils.getPdfReader(options.getInFile(), options.getPdfOwnerPwdStrX().getBytes());
        final Rectangle tmpRect = reader.getPageSizeWithRotation(aPage);
        if (tmpRect != null) {
            tmpResult = new PageInfo(tmpRect.getRight(), tmpRect.getTop());
        }
    } catch (Exception e) {
        // nothing to do
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
            }
        }
    }

    return tmpResult;
}

From source file:net.sf.jsignpdf.preview.Pdf2Image.java

License:Mozilla Public License

/**
 * Returns image (or null if failed) generated from given page in PDF using
 * JPedal LGPL./*from  w ww.ja va  2 s  . c  om*/
 * 
 * @param aPage
 *            page in PDF (1 based)
 * @return image or null
 */
public BufferedImage getImageUsingJPedal(final int aPage) {
    BufferedImage tmpResult = null;
    PdfReader reader = null;
    PdfDecoder pdfDecoder = null;
    try {

        reader = PdfUtils.getPdfReader(options.getInFile(), options.getPdfOwnerPwdStrX().getBytes());
        if (JPEDAL_MAX_IMAGE_RENDER_SIZE > reader.getPageSize(aPage).getWidth()
                * reader.getPageSize(aPage).getHeight()) {
            pdfDecoder = new PdfDecoder();
            try {
                pdfDecoder.openPdfFile(options.getInFile(), options.getPdfOwnerPwdStrX());
            } catch (PdfException e) {
                try {
                    // try to read PDF with empty password
                    pdfDecoder.openPdfFile(options.getInFile(), "");
                } catch (PdfException e1) {
                    // try to read PDF without password
                    pdfDecoder.openPdfFile(options.getInFile());
                }
            }
            tmpResult = pdfDecoder.getPageAsImage(aPage);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (pdfDecoder != null) {
            pdfDecoder.closePdfFile();
        }
    }
    return tmpResult;
}

From source file:net.sourceforge.fenixedu.util.report.ReportsUtils.java

License:Open Source License

static public byte[] stampPdfAt(byte[] originalPdf, byte[] toStampPdf, int positionX, int positionY) {
    try {/* w  w w .j a v  a  2  s . com*/
        PdfReader originalPdfReader = new PdfReader(originalPdf);
        PdfReader toStampPdfReader = new PdfReader(toStampPdf);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(originalPdfReader, stream);

        PdfImportedPage importedPage = stamper.getImportedPage(toStampPdfReader, 1);

        PdfContentByte overContent = stamper.getOverContent(1);

        Rectangle pageSizeWithRotation = originalPdfReader.getPageSizeWithRotation(1);
        Rectangle pageSizeWithRotationStamper = toStampPdfReader.getPageSizeWithRotation(1);

        logger.info(
                String.format("[ %s, %s]", pageSizeWithRotation.getWidth(), pageSizeWithRotation.getHeight()));
        logger.info(String.format("[ %s, %s]", pageSizeWithRotationStamper.getWidth(),
                pageSizeWithRotationStamper.getHeight()));

        Image image = Image.getInstance(importedPage);

        overContent.addImage(image, image.getWidth(), 0f, 0f, image.getHeight(), positionX, positionY);

        stamper.close();

        originalPdfReader.close();
        toStampPdfReader.close();

        return stream.toByteArray();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}

From source file:net.sqs2.omr.master.pdfbookmark.PDFtoBookmarkTranslator.java

License:Apache License

@Override
public void execute(InputStream sourceInputStream, String systemId, OutputStream bookmarkOutputStream,
        URIResolver uriResolver) throws InvalidPageMasterException {
    try {/*  w w  w  .j  av  a  2  s  . c o m*/
        PdfReader reader = new PdfReader(sourceInputStream);
        this.numPages = reader.getNumberOfPages();
        SimpleBookmark.exportToXML(SimpleBookmark.getBookmark(reader), bookmarkOutputStream, "UTF-8", false);
        reader.close();
    } catch (IOException ex) {
        throw new InvalidPageMasterException();
    } finally {
    }
}