All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jpedal.ThumbnailDecoder Maven / Gradle / Ivy

/*
 * ===========================================
 * Java Pdf Extraction Decoding Access Library
 * ===========================================
 *
 * Project Info:  http://www.idrsolutions.com
 * Help section for developers at http://www.idrsolutions.com/support/
 *
 * (C) Copyright 1997-2017 IDRsolutions and Contributors.
 *
 * This file is part of JPedal/JPDF2HTML5
 *
     This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


 *
 * ---------------
 * ThumbnailDecoder.java
 * ---------------
 */
package org.jpedal;


import java.awt.Graphics;
import java.awt.image.BufferedImage;

import org.jpedal.exception.PdfException;
import org.jpedal.objects.acroforms.AcroRenderer;
import org.jpedal.utils.LogWriter;

/**
 * generates the thumbnails of pages for display
 */
public class ThumbnailDecoder {

    private final PdfDecoderInt decode_pdf;

    public ThumbnailDecoder(final PdfDecoderInt decode_pdf) {

        this.decode_pdf = decode_pdf;

    }

    /**
     * get pdf as Image of any page scaling is size (100 = full size)
     */
    public final synchronized BufferedImage getPageAsThumbnail(final int pageNumber, final int height) {

        BufferedImage newImg = null;
        //stopDecoding=false;
        try {

            //this is used in Viewer so we need to over-ride implicit assumption            
            //forms rendered as components just for thumbnail
            final AcroRenderer formRenderer = decode_pdf.getFormRenderer();

            final boolean formsAlreadyDecoded = formRenderer.getCompData().getFormList(true)[pageNumber] != null;

            //Produce image at 50% scaling to prevent massive memory issues at large scalings
            //Also improves thumbnail quality at higher resolutions
            float scaling = decode_pdf.getScaling();
            if (scaling > 0.5f) {
                scaling = 0.5f;
            }
            final BufferedImage pageImage = decode_pdf.getPageAsImage(pageNumber, scaling);

            //If viewer had not decoded forms for that page, reset to null
            if (!formsAlreadyDecoded) {
                formRenderer.getCompData().getFormList(true)[pageNumber] = null;
                formRenderer.getCompData().getFormList(false)[pageNumber] = null;
            }

            final int imgHeight = pageImage.getHeight();
            final double scale = height / (double) imgHeight;
            final int width = (int) (pageImage.getWidth() * scale);

            newImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            final Graphics g = newImg.getGraphics();
            g.drawImage(pageImage, 0, 0, width, height, null);
            g.dispose();

        } catch (final PdfException e) {
            LogWriter.writeLog("Exception: " + e.getMessage());
        }

        return newImg;

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy