Example usage for com.itextpdf.text.pdf PdfCopy freeReader

List of usage examples for com.itextpdf.text.pdf PdfCopy freeReader

Introduction

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

Prototype

@Override
    public void freeReader(PdfReader reader) throws IOException 

Source Link

Usage

From source file:adams.flow.transformer.PDFExtract.java

License:Open Source License

/**
 * Executes the flow item./*from  www . j  a va2 s. c  o  m*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    File file;
    int i;
    Document document;
    PdfCopy copy;
    PdfReader reader;
    int[] pages;
    int page;
    FileOutputStream fos;

    result = null;

    // get file
    if (m_InputToken.getPayload() instanceof File)
        file = (File) m_InputToken.getPayload();
    else
        file = new PlaceholderFile((String) m_InputToken.getPayload());

    fos = null;
    try {
        if (isLoggingEnabled())
            getLogger().info("Extracting pages from '" + file + "' into '" + m_Output + "'");
        document = new Document();
        fos = new FileOutputStream(m_Output.getAbsolutePath());
        copy = new PdfCopy(document, fos);
        document.open();
        document.addCreationDate();
        document.addCreator(Environment.getInstance().getProject());
        document.addAuthor(System.getProperty("user.name"));
        reader = new PdfReader(file.getAbsolutePath());
        if (isLoggingEnabled())
            getLogger().info("- #pages: " + reader.getNumberOfPages());
        m_Pages.setMax(reader.getNumberOfPages());
        pages = m_Pages.getIntIndices();
        for (i = 0; i < pages.length; i++) {
            page = pages[i] + 1;
            copy.addPage(copy.getImportedPage(reader, page));
            if (isLoggingEnabled())
                getLogger().info("- adding page: " + page);
        }
        copy.freeReader(reader);
        document.close();
    } catch (Exception e) {
        result = handleException("Failed to extract pages: ", e);
    } finally {
        FileUtils.closeQuietly(fos);
    }

    if (result == null)
        m_OutputToken = new Token(m_Output.getAbsolutePath());

    return result;
}

From source file:adams.flow.transformer.PDFMerge.java

License:Open Source License

/**
 * Executes the flow item./*w ww.j  a  v  a2s  .  c om*/
 *
 * @return      null if everything is fine, otherwise error message
 */
@Override
protected String doExecute() {
    String result;
    PlaceholderFile[] files;
    int i;
    int n;
    int pages;
    Document document;
    PdfCopy copy;
    PdfReader reader;
    FileOutputStream fos;

    result = null;

    // get files
    files = FileUtils.toPlaceholderFileArray(m_InputToken.getPayload());

    fos = null;
    try {
        if (isLoggingEnabled())
            getLogger().info("Merging PDFs into: " + m_Output);
        document = new Document();
        fos = new FileOutputStream(m_Output.getAbsolutePath());
        copy = new PdfCopy(document, fos);
        document.open();
        document.addCreationDate();
        document.addCreator(Environment.getInstance().getProject());
        document.addAuthor(System.getProperty("user.name"));
        for (i = 0; i < files.length; i++) {
            if (isLoggingEnabled())
                getLogger().info("Adding file: " + files[i]);
            reader = new PdfReader(files[i].getAbsolutePath());
            // loop over the pages in that document
            pages = reader.getNumberOfPages();
            if (isLoggingEnabled())
                getLogger().info("- #pages: " + pages);
            for (n = 1; n <= pages; n++) {
                copy.addPage(copy.getImportedPage(reader, n));
                if (isLoggingEnabled())
                    getLogger().info("- adding page: " + n);
            }
            copy.freeReader(reader);
        }
        document.close();
    } catch (Exception e) {
        result = handleException("Failed to merge PDF files: ", e);
    } finally {
        FileUtils.closeQuietly(fos);
    }

    if (result == null)
        m_OutputToken = new Token(m_Output.getAbsolutePath());

    return result;
}

From source file:com.github.sgelb.sldownloader.model.Pdf.java

License:Open Source License

public void mergePdfs() throws DocumentException, IOException {
    String title = book.getPdfTitle() + ".pdf";
    File saveFile = new File(saveFolder, title);

    int count = 1;
    while (saveFile.exists()) {
        title = book.getPdfTitle() + "_" + count++ + ".pdf";
        saveFile = new File(saveFolder, title);
    }//from   www.j a v a  2 s.com
    book.setInfo("saveFile", saveFile.toString());

    Document document = new Document();
    PdfCopy destPdf = new PdfCopy(document, new FileOutputStream(saveFile));
    document.open();
    PdfReader reader;
    int page_offset = 0;
    int n;
    ArrayList<HashMap<String, Object>> bookmarks = new ArrayList<HashMap<String, Object>>();
    List<HashMap<String, Object>> tmp;

    count = 1;
    System.out.println("Start mergin\u2026");
    for (File srcPdf : src) {

        if (Thread.interrupted()) {
            return;
        }

        System.out.print(":: " + count++ + "/" + src.size());
        reader = new PdfReader(srcPdf.toString());

        tmp = SimpleBookmark.getBookmark(reader);
        if (tmp != null) {
            SimpleBookmark.shiftPageNumbers(tmp, page_offset, null);
            bookmarks.addAll(tmp);
        }

        n = reader.getNumberOfPages();
        page_offset += n;
        for (int page = 0; page < n;) {
            destPdf.addPage(destPdf.getImportedPage(reader, ++page));
        }
        destPdf.freeReader(reader);
        reader.close();
        System.out.println(" succeed.");
    }
    if (!bookmarks.isEmpty()) {
        destPdf.setOutlines(bookmarks);
    }

    if (book.getInfo("author") != null)
        document.addAuthor(book.getInfo("author"));
    if (book.getInfo("title") != null)
        document.addTitle(book.getInfo("title"));
    if (book.getInfo("subtitle") != null)
        document.addSubject(book.getInfo("subtitle"));
    document.close();

    System.out.println("Merge complete. Saved to " + saveFile);
}

From source file:com.photon.phresco.framework.docs.impl.DocumentUtil.java

License:Apache License

/**
 * @param titleSection/*from  w ww.ja v a 2 s  . co  m*/
 * @param pdfCopy
 * @throws PhrescoException 
 */
public static void addPages(InputStream titleSection, PdfCopy pdfCopy) throws PhrescoException {
    if (isDebugEnabled) {
        S_LOGGER.debug("Entering Method DocumentUtil.addPages(InputStream titleSection, PdfCopy pdfCopy)");
    }
    try {
        PdfReader reader = new PdfReader(titleSection);
        reader.consolidateNamedDestinations();
        int pages = reader.getNumberOfPages();
        for (int i = 1; i <= pages; i++) {
            PdfImportedPage importedPage = pdfCopy.getImportedPage(reader, i);
            pdfCopy.addPage(importedPage);
        }
        pdfCopy.freeReader(reader);
    } catch (BadPdfFormatException e) {
        e.printStackTrace();
        throw new PhrescoException(e);
    } catch (IOException e) {
        throw new PhrescoException(e);
    }
}

From source file:com.photon.phresco.service.docs.impl.DocumentUtil.java

License:Apache License

/**
 * @param titleSection/*from   ww w  . java  2 s  . com*/
 * @param pdfCopy
 * @throws IOException
 * @throws BadPdfFormatException
 */
public static void addPages(InputStream titleSection, PdfCopy pdfCopy)
        throws IOException, BadPdfFormatException {
    if (isDebugEnabled) {
        S_LOGGER.debug("Entering Method DocumentUtil.addPages(InputStream titleSection, PdfCopy pdfCopy)");
    }
    PdfReader reader = new PdfReader(titleSection);
    reader.consolidateNamedDestinations();
    int pages = reader.getNumberOfPages();
    for (int i = 1; i <= pages; i++) {
        PdfImportedPage importedPage = pdfCopy.getImportedPage(reader, i);
        pdfCopy.addPage(importedPage);
    }
    pdfCopy.freeReader(reader);
}