001    package railo.runtime.text.pdf;
002    
003    import java.awt.Color;
004    import java.awt.Graphics;
005    import java.awt.image.BufferedImage;
006    import java.io.IOException;
007    import java.util.Set;
008    
009    import org.icepdf.core.pobjects.Catalog;
010    import org.icepdf.core.pobjects.Document;
011    import org.icepdf.core.pobjects.PDimension;
012    import org.icepdf.core.pobjects.PRectangle;
013    import org.icepdf.core.pobjects.Page;
014    import org.icepdf.core.util.GraphicsRenderingHints;
015    
016    import railo.commons.io.res.Resource;
017    import railo.runtime.exp.ExpressionException;
018    import railo.runtime.exp.PageException;
019    import railo.runtime.img.Image;
020    import railo.runtime.op.Caster;
021    
022    public class PDF2ImageICEpdf extends PDF2Image {
023            
024            public PDF2ImageICEpdf(){
025                    Document.class.getName();// this is needed, that the class throws a error when the PDFRenderer.jar is not in the enviroment
026            }
027    
028            /**
029             * @see railo.runtime.text.pdf.PDF2Image#toImage(byte[], int)
030             */
031            public Image toImage(byte[] input, int pageNumber) throws PageException {
032                    return toImage(input, pageNumber, 100,false);
033            }
034            
035            public Image toImage(byte[] input, int pageNumber, int scale, boolean transparent) throws PageException {
036                    Document document = toDocument(input);
037            BufferedImage bi=toBufferedImage(document,pageNumber,scale/100f,transparent);
038                    document.dispose();
039                    return new Image(bi);
040            }
041    
042            /**
043             * @see railo.runtime.text.pdf.PDF2Image#writeImages(byte[], java.util.Set, railo.commons.io.res.Resource, java.lang.String, java.lang.String, int, boolean, boolean, boolean)
044             */
045            public void writeImages(byte[] input, Set pages, Resource outputDirectory,
046                            String prefix, String format, int scale, boolean overwrite,
047                            boolean goodQuality, boolean transparent) throws PageException,
048                            IOException {
049                    if(scale<1) 
050                            throw new ExpressionException("invalid scale definition ["+Caster.toString(scale)+"], value should be in range from 1 to n");
051            
052                    Document document = toDocument(input);
053            try{    
054                            Resource res;
055                            int count = document.getNumberOfPages();
056                            for(int page=1;page<=count;page++) {
057                                    if(pages!=null && !pages.contains(Integer.valueOf(page)))continue;
058                                    res=createDestinationResource(outputDirectory,prefix,page,format,overwrite);
059                                    //res=outputDirectory.getRealResource(prefix+"_page_"+page+"."+format);
060                                    writeImage(document,page,res,format,scale,overwrite,goodQuality, transparent);
061                            }
062                    }
063                    finally{
064                            
065                    }
066            document.dispose();
067                    
068            }
069            
070            private static void writeImage(Document document, int page, Resource destination,String format, int scale,
071                            boolean overwrite, boolean goodQuality, boolean transparent) throws PageException, IOException {
072                    
073                    BufferedImage bi=toBufferedImage(document,page,scale/100f,transparent);
074                    Image img = new Image(bi);
075                    img.writeOut(destination,format, overwrite, goodQuality?1f:0.5f);
076            }
077            
078    
079    
080            private Document toDocument(byte[] input) throws PageException {
081                    Document document = new Document();
082            try {
083                            document.setByteArray(input, 0, input.length, null);
084                    } catch (Throwable t) {
085                            throw Caster.toPageException(t);
086                    }
087                    return document;
088            }
089    
090            private static BufferedImage toBufferedImage(Document document, int pageNumber,float scale,boolean transparent) {
091                    System.getProperties().put("org.icepdf.core.screen.background", "VALUE_DRAW_NO_BACKGROUND");
092                    
093                    Catalog cat = document.getCatalog();
094                    Page page = cat.getPageTree().getPage(pageNumber-1, document);
095            PDimension sz = page.getSize(Page.BOUNDARY_CROPBOX, 0f, scale);
096    
097            int pageWidth = (int) sz.getWidth();
098            int pageHeight = (int) sz.getHeight();
099    
100            BufferedImage image = new BufferedImage(pageWidth,
101                    pageHeight,
102                    transparent?BufferedImage.TYPE_INT_ARGB:BufferedImage.TYPE_INT_RGB);
103            Graphics g = image.createGraphics();
104            if (!transparent) {
105                    PRectangle pageBoundary = page.getPageBoundary(Page.BOUNDARY_CROPBOX);
106                    float x = 0 - pageBoundary.x;
107                float y = 0 - (pageBoundary.y - pageBoundary.height);
108    
109                g.setColor(Color.WHITE);
110                g.fillRect((int) (0 - x),
111                        (int) (0 - y),
112                        (int) pageBoundary.width,
113                        (int) pageBoundary.height);
114            }
115            
116            page.paint(g, GraphicsRenderingHints.SCREEN,
117                            Page.BOUNDARY_CROPBOX, 0f, scale);
118            
119            
120            
121            g.dispose();
122            cat.getPageTree().releasePage(page, document);
123    
124            return image;
125    
126            }
127    
128            
129            
130            
131    }