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 @Override 029 public Image toImage(byte[] input, int pageNumber) throws PageException { 030 return toImage(input, pageNumber, 100,false); 031 } 032 033 public Image toImage(byte[] input, int pageNumber, int scale, boolean transparent) throws PageException { 034 Document document = toDocument(input); 035 BufferedImage bi=toBufferedImage(document,pageNumber,scale/100f,transparent); 036 document.dispose(); 037 return new Image(bi); 038 } 039 040 @Override 041 public void writeImages(byte[] input, Set pages, Resource outputDirectory, 042 String prefix, String format, int scale, boolean overwrite, 043 boolean goodQuality, boolean transparent) throws PageException, 044 IOException { 045 if(scale<1) 046 throw new ExpressionException("invalid scale definition ["+Caster.toString(scale)+"], value should be in range from 1 to n"); 047 048 Document document = toDocument(input); 049 try{ 050 Resource res; 051 int count = document.getNumberOfPages(); 052 for(int page=1;page<=count;page++) { 053 if(pages!=null && !pages.contains(Integer.valueOf(page)))continue; 054 res=createDestinationResource(outputDirectory,prefix,page,format,overwrite); 055 //res=outputDirectory.getRealResource(prefix+"_page_"+page+"."+format); 056 writeImage(document,page,res,format,scale,overwrite,goodQuality, transparent); 057 } 058 } 059 finally{ 060 061 } 062 document.dispose(); 063 064 } 065 066 private static void writeImage(Document document, int page, Resource destination,String format, int scale, 067 boolean overwrite, boolean goodQuality, boolean transparent) throws PageException, IOException { 068 069 BufferedImage bi=toBufferedImage(document,page,scale/100f,transparent); 070 Image img = new Image(bi); 071 img.writeOut(destination,format, overwrite, goodQuality?1f:0.5f); 072 } 073 074 075 076 private Document toDocument(byte[] input) throws PageException { 077 Document document = new Document(); 078 try { 079 document.setByteArray(input, 0, input.length, null); 080 } catch (Throwable t) { 081 throw Caster.toPageException(t); 082 } 083 return document; 084 } 085 086 private static BufferedImage toBufferedImage(Document document, int pageNumber,float scale,boolean transparent) { 087 System.getProperties().put("org.icepdf.core.screen.background", "VALUE_DRAW_NO_BACKGROUND"); 088 089 Catalog cat = document.getCatalog(); 090 Page page = cat.getPageTree().getPage(pageNumber-1, document); 091 PDimension sz = page.getSize(Page.BOUNDARY_CROPBOX, 0f, scale); 092 093 int pageWidth = (int) sz.getWidth(); 094 int pageHeight = (int) sz.getHeight(); 095 096 BufferedImage image = new BufferedImage(pageWidth, 097 pageHeight, 098 transparent?BufferedImage.TYPE_INT_ARGB:BufferedImage.TYPE_INT_RGB); 099 Graphics g = image.createGraphics(); 100 if (!transparent) { 101 PRectangle pageBoundary = page.getPageBoundary(Page.BOUNDARY_CROPBOX); 102 float x = 0 - pageBoundary.x; 103 float y = 0 - (pageBoundary.y - pageBoundary.height); 104 105 g.setColor(Color.WHITE); 106 g.fillRect((int) (0 - x), 107 (int) (0 - y), 108 (int) pageBoundary.width, 109 (int) pageBoundary.height); 110 } 111 112 page.paint(g, GraphicsRenderingHints.SCREEN, 113 Page.BOUNDARY_CROPBOX, 0f, scale); 114 115 116 117 g.dispose(); 118 cat.getPageTree().releasePage(page, document); 119 120 return image; 121 122 } 123 124 125 126 127 }