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

org.jpedal.ThumbnailDecoder Maven / Gradle / Ivy

There is a newer version: 7.15.25
Show newest version
/*
 * ===========================================
 * 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
 *
 @LICENSE@
 *
 * ---------------
 * 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 - 2025 Weber Informatics LLC | Privacy Policy