Salve ragazzi, avrei una questione da proporvi.

Sono riuscito a caricare un file PDF nel mio programma attraverso la librerie PDF-Renderer.

Preavviso che per alcuni motivi devo assolutamente utilizzare questa.

Devo visualizzare il file pdf in una componente del tipo JInternalFrame.

Fin qui nulla di complicato e ci sono riuscito.

Adesso però come voi ben sapete un file pdf può avere più pagine mica per forza solo una.

Con il codice di esempio del sito : https://pdf-renderer.dev.java.net/examples.html

sono riuscito a far visualizzare la prima pagina di qualsiasi documento pdf.

Adesso dvo dare la possibilità di poter visualizzare anche le altre pagine ho apportato alcune modifiche ed ho inserito un pulsante per andare in avanti così come noi facciamo con acrobat reader.

Allora se visualizzo la nuova pagine nel JInternalFrame che contiene la prima pagine non visualizzo nulla Se creo un nuovo JInternalFrame mi viene visualizzata correttamente, potete ovviare a questo problema??

Intanto posto il codice della mia classe:

codice:
public class ImageMain {
public static int numeroPagine;
public static int i;
    public static void setup() throws IOException {
    	i=1;
    	final JFrame frame2 = new JFrame("PDF Test");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //load a pdf from a byte buffer
        
        final JInternalFrame frame = new JInternalFrame("PDF Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        final File file = new File("c:/m.pdf");
        final RandomAccessFile raf = new RandomAccessFile(file, "r");
        final FileChannel channel = raf.getChannel();
        final ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        final PDFFile pdffile = new PDFFile(buf);
        numeroPagine = pdffile.getNumPages();
      
        // draw the first page to an image
        PDFPage page = pdffile.getPage(0);
        final BufferedImage img = new BufferedImage((int)page.getBBox().getWidth(),
                (int)page.getBBox().getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //get the width and height for the doc at the default zoom 
        Rectangle rect = new Rectangle(0,0,
                (int)page.getBBox().getWidth(),
                (int)page.getBBox().getHeight());
        try{
        //generate the image
        PDFRenderer renderer = new PDFRenderer(page, g2, 
        	    new Rectangle(0, 0, (int)page.getBBox().getWidth(),
                        (int)page.getBBox().getHeight()), null, Color.white);
        	page.waitForFinish();
        	renderer.run();
        }catch(Exception e){}
        
        final JButton Botton = new JButton("Avanti");
        Botton.setSize( Botton.getPreferredSize() );
        frame2.add(Botton);
        Botton.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
            	if(i>numeroPagine){
            	   Botton.setEnabled(false);	
            	}
            	else{
            		try{
            		i++;
            		
            		PDFPage page2 = pdffile.getPage(i);
            		
            		BufferedImage img = new BufferedImage((int)page2.getBBox().getWidth(),
                            (int)page2.getBBox().getHeight(), BufferedImage.TYPE_INT_ARGB);
            		 Graphics2D g2 = img.createGraphics();
            	        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            	        //get the width and height for the doc at the default zoom 
            	        Rectangle rect = new Rectangle(0,0,
            	                (int)page2.getBBox().getWidth(),
            	                (int)page2.getBBox().getHeight());
            	        
            	            //generate the image
            	            PDFRenderer renderer = new PDFRenderer(page2, g2, 
            	            	    new Rectangle(0, 0, (int)page2.getBBox().getWidth(),
            	                            (int)page2.getBBox().getHeight()), null, Color.white);
            	            	page2.waitForFinish();
            	            	renderer.run();
            	            	
            	           	JFrame framed = new JFrame("DDD");
                  	           
                    	        
                	           JInternalFrame frame3 = new JInternalFrame("PDFTEST"); 
                	           frame3.add(new JLabel(new ImageIcon(img)));
                	            frame3.pack();
                	            frame3.setVisible(true);
                	            framed.add(frame3);
                	            framed.pack();
                	           framed.setVisible(true);
                	            System.out.println("sono arrivato");
            	            }catch(Exception r){}
            	            System.out.println("sono arrivato");
            	            //frame2.removeAll();
            	           // frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            	          // frame.removeAll();
            	         //  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            	          
            	}
            }
            });
        //show the image in a frame
        
        frame.add(new JLabel(new ImageIcon(img))); 
        frame.pack();
        frame.setVisible(true);
        
       
        frame2.add(frame);
        frame2.pack();
        frame2.setVisible(true);
    }

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    ImageMain.setup();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}

Con questo codice io apro ogni volta un nuovo Frame e un nuovo JInternalFrame ma io devo fare in modo che questo avvenga tutto in uno.

Saluti Michele