Utilisation de PDF Renderer

alex_dudu Messages postés 1 Date d'inscription lundi 21 février 2005 Statut Membre Dernière intervention 12 mars 2008 - 12 mars 2008 à 18:34
cs_simomami Messages postés 1 Date d'inscription jeudi 2 décembre 2004 Statut Membre Dernière intervention 7 juin 2009 - 7 juin 2009 à 22:42
Bonjour à tous,
nous vous postons ce message pour solliciter votre aide sur l'utilisation de la bibliothèque PDF Renderer.
Le code source suivant est censé nous permettre de convertir un fichier PDF en une série d'images au format .png. Le problème est que la conversion réalisée ici déforme partiellement certains documents (caractères qui se chevauchent ou images pixellisées).

Voici le code source que nous utilisons et que nous avons trouvé sur cette page :
http://blog.rubypdf.com/2008/01/30/pdftoimage-convert-pdf-to-image-by-using-pdfrenderer-library/

Merci par avance de votre aide.

Voici le code source:

public class Pdf2Image {
 
    /**
     * @param args
     */
    public static String[] liste; //liste a afficher
    public static int nb = 0;
    
    public static void main(String[] args) {
        Fenetre fen = new Fenetre();
        fen.ChooseRep();    

        File file = new File(Fenetre.nomRep);
        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);
            
            // draw the first page to an image
            int num = pdffile.getNumPages();
            for(int i = 1 ; i <= num ; i++)
            {
                PDFPage page = pdffile.getPage(i);
 
                //get the width and height for the doc at the default zoom                
                int width=(int)page.getBBox().getWidth();
                System.out.println(width);
                int height=(int)page.getBBox().getHeight();        
                System.out.println(height);
 
                Rectangle rect = new Rectangle(0,0,width,height);
                int rotation = page.getRotation();
                Rectangle rect1=rect;                if(rotation 90 || rotation 270)
                    rect1 = new Rectangle(0, 0, rect.height, rect.width);
 
                //generate the image
                BufferedImage img = (BufferedImage)page.getImage(
                            rect.width, rect.height, //width & height
                            rect1, // clip rect
                            null, // null for the ImageObserver
                            true, // fill background with white
                            true  // block until drawing is done
                    );
 
                ImageIO.write(img, "png", new File("C:/Documents and Settings/srun/Bureau/Nouveaudossier/pdf"+i+".png"));
                System.out.println("Image "+ i+ " enregistrer");
                
                
                System.out.println("Image "+ i+ " afficher");
            }
            fen.afficher();
        }
        catch (FileNotFoundException e1) {
            System.err.println(e1.getLocalizedMessage());
        } catch (IOException e) {
            System.err.println(e.getLocalizedMessage());
        }
    }

}

1 réponse

cs_simomami Messages postés 1 Date d'inscription jeudi 2 décembre 2004 Statut Membre Dernière intervention 7 juin 2009
7 juin 2009 à 22:42
je pense que le fait d'augmenter la résolution de l'image généré peut résoudre le probléme, par exemple le doubler,
remplacer :

BufferedImage img = (BufferedImage)page.getImage(
                            rect.width, rect.height, //width & height
                            rect1, // clip rect
                            null, // null for the ImageObserver
                            true, // fill background with white
                            true  // block until drawing is done
                    );

par
BufferedImage img = (BufferedImage)page.getImage(
                            2*rect.width, 2*rect.height, //width & height
                            rect1, // clip rect
                            null, // null for the ImageObserver
                            true, // fill background with white
                            true  // block until drawing is done
                    );

 
0
Rejoignez-nous