001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.text.pdf;
020
021import java.awt.Color;
022import java.awt.Graphics;
023import java.awt.image.BufferedImage;
024import java.io.IOException;
025import java.util.Set;
026
027import lucee.commons.io.res.Resource;
028import lucee.runtime.exp.ExpressionException;
029import lucee.runtime.exp.PageException;
030import lucee.runtime.img.Image;
031import lucee.runtime.op.Caster;
032
033import org.icepdf.core.pobjects.Catalog;
034import org.icepdf.core.pobjects.Document;
035import org.icepdf.core.pobjects.PDimension;
036import org.icepdf.core.pobjects.PRectangle;
037import org.icepdf.core.pobjects.Page;
038import org.icepdf.core.util.GraphicsRenderingHints;
039
040public class PDF2ImageICEpdf extends PDF2Image {
041        
042        public PDF2ImageICEpdf(){
043                Document.class.getName();// this is needed, that the class throws a error when the PDFRenderer.jar is not in the enviroment
044        }
045
046        @Override
047        public Image toImage(byte[] input, int pageNumber) throws PageException {
048                return toImage(input, pageNumber, 100,false);
049        }
050        
051        public Image toImage(byte[] input, int pageNumber, int scale, boolean transparent) throws PageException {
052                Document document = toDocument(input);
053        BufferedImage bi=toBufferedImage(document,pageNumber,scale/100f,transparent);
054                document.dispose();
055                return new Image(bi);
056        }
057
058        @Override
059        public void writeImages(byte[] input, Set pages, Resource outputDirectory,
060                        String prefix, String format, int scale, boolean overwrite,
061                        boolean goodQuality, boolean transparent) throws PageException,
062                        IOException {
063                if(scale<1) 
064                        throw new ExpressionException("invalid scale definition ["+Caster.toString(scale)+"], value should be in range from 1 to n");
065        
066                Document document = toDocument(input);
067        try{    
068                        Resource res;
069                        int count = document.getNumberOfPages();
070                        for(int page=1;page<=count;page++) {
071                                if(pages!=null && !pages.contains(Integer.valueOf(page)))continue;
072                                res=createDestinationResource(outputDirectory,prefix,page,format,overwrite);
073                                //res=outputDirectory.getRealResource(prefix+"_page_"+page+"."+format);
074                                writeImage(document,page,res,format,scale,overwrite,goodQuality, transparent);
075                        }
076                }
077                finally{
078                        
079                }
080        document.dispose();
081                
082        }
083        
084        private static void writeImage(Document document, int page, Resource destination,String format, int scale,
085                        boolean overwrite, boolean goodQuality, boolean transparent) throws PageException, IOException {
086                
087                BufferedImage bi=toBufferedImage(document,page,scale/100f,transparent);
088                Image img = new Image(bi);
089                img.writeOut(destination,format, overwrite, goodQuality?1f:0.5f);
090        }
091        
092
093
094        private Document toDocument(byte[] input) throws PageException {
095                Document document = new Document();
096        try {
097                        document.setByteArray(input, 0, input.length, null);
098                } catch (Throwable t) {
099                        throw Caster.toPageException(t);
100                }
101                return document;
102        }
103
104        private static BufferedImage toBufferedImage(Document document, int pageNumber,float scale,boolean transparent) {
105                System.getProperties().put("org.icepdf.core.screen.background", "VALUE_DRAW_NO_BACKGROUND");
106                
107                Catalog cat = document.getCatalog();
108                Page page = cat.getPageTree().getPage(pageNumber-1, document);
109        PDimension sz = page.getSize(Page.BOUNDARY_CROPBOX, 0f, scale);
110
111        int pageWidth = (int) sz.getWidth();
112        int pageHeight = (int) sz.getHeight();
113
114        BufferedImage image = new BufferedImage(pageWidth,
115                pageHeight,
116                transparent?BufferedImage.TYPE_INT_ARGB:BufferedImage.TYPE_INT_RGB);
117        Graphics g = image.createGraphics();
118        if (!transparent) {
119                PRectangle pageBoundary = page.getPageBoundary(Page.BOUNDARY_CROPBOX);
120                float x = 0 - pageBoundary.x;
121            float y = 0 - (pageBoundary.y - pageBoundary.height);
122
123            g.setColor(Color.WHITE);
124            g.fillRect((int) (0 - x),
125                    (int) (0 - y),
126                    (int) pageBoundary.width,
127                    (int) pageBoundary.height);
128        }
129        
130        page.paint(g, GraphicsRenderingHints.SCREEN,
131                        Page.BOUNDARY_CROPBOX, 0f, scale);
132        
133        
134        
135        g.dispose();
136        cat.getPageTree().releasePage(page, document);
137
138        return image;
139
140        }
141
142        
143        
144        
145}