O código abaixo é bem simples e serve para gerar imagens a partir de um arquivo pdf.
package pdf2image;
package pdf2image;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jpedal.PdfDecoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class PdfToImage {
public PdfToImage() {
}
public static void main(String[] args) {
try{
PdfDecoder decode_pdf = new PdfDecoder(true);
decode_pdf.useHiResScreenDisplay(true);
String pdfToDecode = "c:/SeuArquivoPdf.pdf";
decode_pdf.openPdfFile(pdfToDecode);
int imageHeight = 1268;
int pageFromPDF = 1;
BufferedImage image_to_save = decode_pdf.getPageAsThumbnail(pageFromPDF, imageHeight);
/* Caso queira a imagem em escala de cinza, remover este comentário
g2.drawImage(image_to_save, 0, 0, image_to_save.getWidth(), image_to_save.getHeight(), null);
*/
System.out.println(bi);
saveImage(bi, .9f, "c:/test2.jpg");
}
catch(Exception e){
e.printStackTrace();
}
new PdfToImage();
}
public static void saveImage(BufferedImage image, float quality, String fileName){
try{
FileOutputStream fos = null;
fos = new FileOutputStream(fileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(image);
param.setQuality(quality, true);
encoder.encode(image, param);
fos.flush();
fos.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jpedal.PdfDecoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class PdfToImage {
public PdfToImage() {
}
public static void main(String[] args) {
try{
PdfDecoder decode_pdf = new PdfDecoder(true);
decode_pdf.useHiResScreenDisplay(true);
String pdfToDecode = "c:/SeuArquivoPdf.pdf";
decode_pdf.openPdfFile(pdfToDecode);
int imageHeight = 1268;
int pageFromPDF = 1;
BufferedImage image_to_save = decode_pdf.getPageAsThumbnail(pageFromPDF, imageHeight);
/* Caso queira a imagem em escala de cinza, remover este comentário
BufferedImage bi = new BufferedImage(image_to_save.getWidth(), image_to_save.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2 = (Graphics2D) bi.getGraphics();g2.drawImage(image_to_save, 0, 0, image_to_save.getWidth(), image_to_save.getHeight(), null);
*/
System.out.println(bi);
saveImage(bi, .9f, "c:/test2.jpg");
}
catch(Exception e){
e.printStackTrace();
}
new PdfToImage();
}
public static void saveImage(BufferedImage image, float quality, String fileName){
try{
FileOutputStream fos = null;
fos = new FileOutputStream(fileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(image);
param.setQuality(quality, true);
encoder.encode(image, param);
fos.flush();
fos.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
}
Para receber a lib do JPEDAL, enviar email para dougcpinheiro@yahoo.com