java - 使用pdf渲染器在java中查看整个pdf

标签 java pdf pdfrenderer

我试图通过添加拆分和滚动条以及遍历所有页面的 for 循环来在面板中查看整个 pdf。

问题是它只显示最后一页。谁能纠正我的错误。

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.*;


public class NewJFrame extends javax.swing.JFrame  {
    public NewJFrame() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jButton1 = new javax.swing.JButton();

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(64, 64, 64)
                .addComponent(jButton1)
                .addContainerGap(263, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addComponent(jButton1)
                .addContainerGap(212, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        JSplitPane split= new JSplitPane();
    JScrollPane thumbscroll=new JScrollPane();
       JFrame frame = new JFrame("PDF Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PagePanel panel = new PagePanel();
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        File file = new File("C:\\Users\\om\\Downloads\\cse.pdf");
        try{
            RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
            0, channel.size());
        frame.add(split);
      frame.add(thumbscroll); 
      frame.pack();
        // show the first page
        for(int i=0;i<10;i++)
        {
            PDFFile pdffile = new PDFFile(buf);
        PDFPage page = pdffile.getPage(i);

        panel.showPage(page);

        }
        }
       catch(IOException e)
        {
            System.out.print(e.getMessage());
        } 

       public static void main(String args[])  {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JFrame jFrame1;
// End of variables declaration                   
}

最佳答案

每次调用showPage时,显示的页面都会被覆盖。有关 PDF 查看器的工作示例,请查看 here

关于java - 使用pdf渲染器在java中查看整个pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24651194/

相关文章:

java - 无法将字符串转换为本地日期

python - Matplotlib 输出为 PDF 用于 Corel Draw

c++ - 用于 C/C++ 应用程序的开源 PDF 库?

java - PDFRenderer 的问题

java - Maven 快速入门原型(prototype)不存在

java - 从 Java 发出 HTTP 请求时出现 “PKIX path building failed” 错误

java - 这是Java中计算最小数的标准方法吗

android - 在应用程序中创建 PDF/Word (Doc) 文件

java - 获取图像的最右、最左、最上、最下位置 - Itext

javascript - SlimerJs PDF 渲染到文件