package com.ese.ils.beta.util; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; import com.sun.pdfview.PDFFile; import com.sun.pdfview.PDFPage; /** * erzeugt aus PDF Foliensaetzen PNG Bilder * @author julienhofer * */ public class PNGCreator { /** * konvertiert eine PDF Datei und deren Seiten * in PNG Bilder * @param file * @param moduleId * @return */ public static List<File> convertPDFtoPNG(File file, long moduleId){ //consider different slide ids List<File> slideList = new ArrayList<File>(); RandomAccessFile raf; try { raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdfFile = new PDFFile(buf); //konvertiere erste seite in ein bild int num = pdfFile.getNumPages(); for(int i=0;i<num;i++) { long slideId = Long.valueOf(i); PDFPage page = pdfFile.getPage(i); //ermittle Hoehe und Breite int width = (int)page.getBBox().getWidth(); int height = (int)page.getBBox().getHeight(); //AWT Rectangle rect = new Rectangle(0,0,width, height); int rotation = page.getRotation(); Rectangle rectl = rect; if(rotation == 90 || rotation==270) { rectl=new Rectangle(0,0,rect.height,rect.width); } //generiere Bild BufferedImage img = (BufferedImage) page.getImage( rect.width, rect.height, //Breite Hoehe rectl, null, true, //Fuelle Hintergrund weiss aus true ); //TODO: get string-filename from string-util File tempFile = new File(DeComposer.longToFilename(slideId)+".png"); ImageIO.write(img, "png", tempFile); slideList.add(tempFile); img.flush(); } } //Catcher catch (FileNotFoundException e1){ System.err.println(e1.getLocalizedMessage()); } catch (IOException e2){ System.err.println(e2.getLocalizedMessage()); } return slideList; } }