org.ghost4j.document.PDFDocument.java Source code

Java tutorial

Introduction

Here is the source code for org.ghost4j.document.PDFDocument.java

Source

/*
 * Ghost4J: a Java wrapper for Ghostscript API.
 *
 * Distributable under LGPL license.
 * See terms of license at http://www.gnu.org/licenses/lgpl.html.
 */

package org.ghost4j.document;

import org.apache.commons.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author ggrousset
 */
public class PDFDocument extends AbstractDocument {

    /**
     * Serial version UID.
     */
    private static final long serialVersionUID = 6331191005700202153L;

    @Override
    public void load(InputStream inputStream) throws IOException {
        super.load(inputStream);

        // check that the file is a PDF
        ByteArrayInputStream bais = null;
        PDDocument document = null;

        try {

            bais = new ByteArrayInputStream(content);
            document = PDDocument.load(bais);

        } catch (Exception e) {
            throw new IOException("PDF document is not valid");
        } finally {
            if (document != null)
                document.close();
            IOUtils.closeQuietly(bais);
        }
    }

    public int getPageCount() throws DocumentException {

        int pageCount = 0;

        if (content == null) {
            return pageCount;
        }

        ByteArrayInputStream bais = null;
        PDDocument document = null;

        try {

            bais = new ByteArrayInputStream(content);
            document = PDDocument.load(bais);
            pageCount = document.getNumberOfPages();
        } catch (Exception e) {
            throw new DocumentException(e);
        } finally {
            if (document != null)
                try {
                    document.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            IOUtils.closeQuietly(bais);
        }

        return pageCount;

    }

    public Document extract(int begin, int end) throws DocumentException {

        this.assertValidPageRange(begin, end);

        PDFDocument result = new PDFDocument();

        ByteArrayInputStream bais = null;
        ByteArrayOutputStream baos = null;

        if (content != null) {

            PDDocument document = new PDDocument();

            try {

                bais = new ByteArrayInputStream(content);
                baos = new ByteArrayOutputStream();
                PDDocument inputPDF = PDDocument.load(bais);
                while (begin <= end) {
                    document.addPage((PDPage) inputPDF.getDocumentCatalog().getAllPages().get(begin - 1));
                    begin++;
                }
                document.save(baos);
                document.close();
                result.load(new ByteArrayInputStream(baos.toByteArray()));

            } catch (Exception e) {
                throw new DocumentException(e);
            } finally {
                IOUtils.closeQuietly(bais);
                IOUtils.closeQuietly(baos);
            }

        }

        return result;
    }

    @Override
    public void append(Document document) throws DocumentException {

        super.append(document);

        ByteArrayOutputStream baos = null;
        PDDocument mergedDocument = new PDDocument();

        try {

            baos = new ByteArrayOutputStream();
            ByteArrayInputStream bais = new ByteArrayInputStream(content);
            PDDocument pDocument = PDDocument.load(bais);
            int pageCount = pDocument.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                mergedDocument.addPage((PDPage) pDocument.getDocumentCatalog().getAllPages().get(i));
            }

            // copy new document
            ByteArrayInputStream baisNewDoc = new ByteArrayInputStream(document.getContent());
            PDDocument pNewDocument = PDDocument.load(baisNewDoc);
            pageCount = pNewDocument.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                mergedDocument.addPage((PDPage) pNewDocument.getDocumentCatalog().getAllPages().get(i));
            }
            mergedDocument.save(baos);
            mergedDocument.close();
            // replace content with new content
            content = baos.toByteArray();

        } catch (Exception e) {
            throw new DocumentException(e);
        } finally {
            IOUtils.closeQuietly(baos);
        }

    }

    public String getType() {
        return TYPE_PDF;
    }
}