Ho ripreso direttamente l'esempio di codice dalla pagina ufficiale di PDFRenderer in quanto e rimaneggiato un attimo (in sostanza, ho splittato il metodo setup, aggiunto un costruttore esplicitamente dichiarato ed un listener)
Mi sono accorto che pagina 0 o 1 sono la stessa cosa... allora per comodità parto da pagina 1. Presta solo attenzione al metodo showPage: è lì che viene usato validate.codice:import com.sun.pdfview.PDFFile; import com.sun.pdfview.PDFPage; import com.sun.pdfview.PagePanel; import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * An example of using the PagePanel class to show PDFs. For more advanced * usage including navigation and zooming, look ad the * com.sun.pdfview.PDFViewer class. * * @author joshua.marinacci@sun.com */ public class ViewTest extends JFrame { private class MyPageButtonListener implements ActionListener { public MyPageButtonListener() { if (pg == 1) { indietro.setEnabled(false); } if (pg == tot_pages) { avanti.setEnabled(false); } } public void actionPerformed(ActionEvent ae) { JButton src = (JButton)ae.getSource(); if (src.equals(indietro)) { pg--; if (pg <= 1) { indietro.setEnabled(false); } avanti.setEnabled(true); showPage(pg); } else { pg++; if (pg >= tot_pages) { avanti.setEnabled(false); } indietro.setEnabled(true); showPage(pg); } } } private JButton avanti, indietro; private PagePanel panel; private PDFFile pdffile; private String path; private int pg, tot_pages; public ViewTest(String path, int pg) { super("Test PDF"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.avanti = new JButton("Avanti"); this.indietro = new JButton("Indietro"); this.pg = pg; this.panel = new PagePanel(); this.path = path; this.getContentPane().add(new JScrollPane(panel), BorderLayout.CENTER); JPanel northPanel = new JPanel(new GridLayout(1,2)); northPanel.add(indietro); northPanel.add(avanti); this.getContentPane().add(northPanel, BorderLayout.NORTH); this.setSize(800,800); this.setVisible(true); try { this.setup(path); this.showPage(pg); } catch (Exception e) { e.printStackTrace(); } indietro.addActionListener(new MyPageButtonListener()); avanti.addActionListener(new MyPageButtonListener()); } public void showPage(int pg) { PDFPage page = pdffile.getPage(pg); panel.showPage(page); panel.validate(); } public void setup(String path) throws IOException { //set up the frame and panel //load a pdf from a byte buffer File file = new File(path); RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); pdffile = new PDFFile(buf); tot_pages = pdffile.getNumPages(); } public static void main(final String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new ViewTest("tuo_file_pdf", 1); } }); } }

Rispondi quotando