-1

我有一个小型测试类来在 JPanel 中呈现 PDF。每当我执行它时,它都会告诉我无论我选择哪种 PDF 都不支持该 PDF。我的代码是:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;

public class TestPDF {

    JFrame mainFrame;
    final Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestPDF window = new TestPDF();
                    window.mainFrame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TestPDF() {
        mainFrame = new JFrame();
        mainFrame.setTitle("Test");
        mainFrame.setSize(new Dimension((int) (SCREEN_SIZE.getWidth() / 2), (int) (SCREEN_SIZE.getHeight() / 2)));
        JScrollPane contentPane = new JScrollPane();
        mainFrame.add(contentPane);
        SwingController controller = new SwingController();
        SwingViewBuilder factory = new SwingViewBuilder(controller);
        JPanel viewerComponentPanel = factory.buildViewerPanel();
        controller.getDocumentViewController().setAnnotationCallback(new org.icepdf.ri.common.MyAnnotationCallback(controller.getDocumentViewController()));
        try{
        controller.openDocument("gcc.pdf"); //GCC manual
        } catch (Exception e){
            e.printStackTrace();
        }
        contentPane.add(viewerComponentPanel);
    }
}

我正在尝试加载GCC 手册作为测试。

编辑1:

我正在使用 ICEpdf 6.2.2,在运行课程时,会打开一个弹出窗口,其中说明ICEpdf could not open the specified file at gcc.pdf The file may be corrupt or not a supported file type.

4

1 回答 1

0

返回一个 JPanel ,factory.buildViewerPanel()它有一个工具栏、实用程序窗格和一个主查看区域。我怀疑您不需要额外的 JScrollpane 或它导致 Swing 布局问题。

public TestPDF() {
    mainFrame = new JFrame();
    mainFrame.setTitle("Test");
    mainFrame.setSize(new Dimension((int) (SCREEN_SIZE.getWidth() / 2), (int) (SCREEN_SIZE.getHeight() / 2)));
    
    SwingController controller = new SwingController();
    SwingViewBuilder factory = new SwingViewBuilder(controller);
    JPanel viewerComponentPanel = factory.buildViewerPanel();

    // add the panel directly to the main frame
    mainFrame.add(viewerComponentPanel);        
    // boiler plate annotation handler
    controller.getDocumentViewController().setAnnotationCallback(
       new org.icepdf.ri.common.MyAnnotationCallback(controller.getDocumentViewController()));

    // finally open up the document
    try {
       controller.openDocument("gcc.pdf"); //GCC manual
    } catch (Exception e){
        e.printStackTrace();
    }
}
于 2021-02-23T04:17:58.477 回答