Java jInternalFrame 不显示文件(PDF)

问题描述 投票:0回答:1

我可以在

JFrame
中将 PDF 作为图像打开,没问题,但我不想为其打开一个新窗口; PDF 必须以
jInternalFrame
形式显示。 我使用
Netbeans
创建了一个设计,并将使用以下代码打开文档(
Apache PDFBox
):

public void openFile(){
        
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    File file = new File("/home/ritzmo/Labor_Clotten/PDF_Befunde/test.pdf");
                    PDDocument pdffile = Loader.loadPDF(file);
                    PDFRenderer pdfRenderer = new PDFRenderer(pdffile);
                    BufferedImage image = pdfRenderer.renderImage(0);
                    JLabel label = new JLabel(new ImageIcon(image));
                    jInternalFrame2.setTitle("PDF G");                                     
                    jInternalFrame2.add(new JScrollPane(label));
                    jInternalFrame2.setVisible(true);
                } catch (IOException e) {
                }
            }
        });
    }

jInternalFrame
位于主
JFrame
上,但不会显示PDF。 我在 google、chatgpt 上搜索了 3 个多小时,但没有找到答案。 程序启动时没有错误。

有人有想法吗?

伟大的莫里茨

java pdf jinternalframe
1个回答
0
投票

Java 17,pdfbox 2.0.27

PDFViewer.java

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class PDFViewer extends JFrame {
    private JDesktopPane desktopPane;
    public PDFViewer(){
        setTitle("PDF Viewer Demo App ");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000, 800);
        desktopPane = new JDesktopPane();
        add(new JScrollPane(desktopPane), BorderLayout.CENTER);
    }
    public void openFile(String pdfPath) throws IOException {
        //String pdfPath ="/home/demo/Downloads/demo.pdf";
        File pdfFile = new File(pdfPath);
        PDDocument document = PDDocument.load(pdfFile );
        PDFRenderer pdfRenderer = new PDFRenderer(document);
        int pageCount = document.getNumberOfPages();

        JInternalFrame internalFrame = new JInternalFrame(pdfFile.getName(), true, true, true, true);
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        for (int i = 0; i < pageCount; i++) {
            BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 300);
            ImageIcon imageIcon = new ImageIcon(bim);
            JLabel label = new JLabel(imageIcon);
            panel.add(label);
        }

        internalFrame.add(new JScrollPane(panel), BorderLayout.CENTER);
        internalFrame.pack();
        internalFrame.setVisible(true);
        internalFrame.setSize(800, 600);
        desktopPane.add(internalFrame);
        document.close();
    }

    private void openPDF() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            try {
                PDDocument document = PDDocument.load(selectedFile);
                PDFRenderer pdfRenderer = new PDFRenderer(document);
                int pageCount = document.getNumberOfPages();

                JInternalFrame internalFrame = new JInternalFrame(selectedFile.getName(), true, true, true, true);
                JPanel panel = new JPanel();
                panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

                for (int i = 0; i < pageCount; i++) {
                    BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 300);
                    ImageIcon imageIcon = new ImageIcon(bim);
                    JLabel label = new JLabel(imageIcon);
                    panel.add(label);
                }

                internalFrame.add(new JScrollPane(panel), BorderLayout.CENTER);
                internalFrame.pack();
                internalFrame.setVisible(true);
                internalFrame.setSize(800, 600);
                desktopPane.add(internalFrame);
                document.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        String pdfPath1="/home/demo/Downloads/demo.pdf";
        String pdfPath2="/home/demo/Downloads/demo2.pdf";
        String pdfPath3="/home/demo/Downloads/demo3.pdf";

        SwingUtilities.invokeLater(() -> {
            PDFViewer viewer = new PDFViewer();
            try {
                viewer.openFile( pdfPath1);
                //viewer.openFile( pdfPath2);
                //viewer.openFile( pdfPath3);
                viewer.openPDF();
                viewer.openPDF();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            viewer.setVisible(true);

        });

    }
}

使用 JDesktopPane

private JDesktopPane desktopPane;
...
desktopPane = new JDesktopPane();
add(new JScrollPane(desktopPane), BorderLayout.CENTER);
...
desktopPane.add(internalFrame);

将内部框架添加到桌面窗格

2个例子

  • openFile(字符串 pdfPath)
  • 打开PDF()
© www.soinside.com 2019 - 2024. All rights reserved.