1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
public class Convertisseur{
Convertisseur() {
System.out.println("la source est "+Impression.source);
try {
File pdfFile = new File(Impression.source);
RandomAccessFile raf = new RandomAccessFile(pdfFile, "rw");
FileChannel channel = raf.getChannel();
String destination = "D://GEC-I/Impression/";
ByteBuffer buf = null;
try {
buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
} catch (IOException ex) {
Logger.getLogger(Convertisseur.class.getName()).log(Level.SEVERE, null, ex);
}
PDFFile pdf = null;
try {
pdf = new PDFFile(buf);
} catch (IOException ex) {
Logger.getLogger(Convertisseur.class.getName()).log(Level.SEVERE, null, ex);
}
int nbPages = pdf.getNumPages();
for(int i = 0; i < nbPages; i++){
try {
PDFPage page = pdf.getPage(i);
Rectangle rect = new Rectangle(0, 0, (int)page.getBBox().getWidth(),
(int)page.getBBox().getHeight());
Image img = page.getImage(rect.width, rect.height, rect, null, true, true);
// sauvegarde de l'image dans un fichier
BufferedImage bImg = toBufferedImage(img);
File imageFile = new File(destination+File.separator + "page_" + i + ".png");
ImageIO.write(bImg, "png", imageFile);
tabImg[i] = ImageIO.read(imageFile);
} catch (Exception ex) {
System.err.println(""+ex);
}
}
lien_image=destination;
// Frame.chargement.setVisible(false);
} catch (FileNotFoundException ex) {
Logger.getLogger(Convertisseur.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static String lien_image;
public static Image[] tabImg = null;
private BufferedImage toBufferedImage(Image img){
if(img instanceof BufferedImage){
return (BufferedImage) img;
}
img = new ImageIcon(img).getImage();
BufferedImage bImg = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bImg = gc.createCompatibleImage(img.getWidth(null), img.getHeight(null), transparency);
} catch (HeadlessException e) {
System.out.println("The system does not have a screen");
}
if (bImg == null) {
int type = BufferedImage.TYPE_INT_RGB;
bImg = new BufferedImage(img.getWidth(null), img.getHeight(null), type);
}
Graphics2D g = bImg.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return bImg;
}
} |
Partager