0

我遇到了 Icepdf 的问题。我想用icepdf 显示一个pdf,但它不起作用。我认为 Swing 控制器有问题,但这并没有从调试器中清楚地显示出来。这是代码

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ResourceBundle;

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

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

public class PDFAnzeigen {

    public PDFAnzeigen() {
        String pdfPath = loadPDF("https://pegasusshop.de/media/pdf/0e/d0/8a/4250231705298_de.pdf");
        if (pdfPath == null) {
            System.err
                    .println("Datei kann nicht geladen werden oder ist keine PDF-Datei.");
            System.exit(1);
        }
        SwingController controller = new SwingController();
        createGUI(controller);
        controller.openDocument(pdfPath);
    }

    public static void main(String[] args) {
        new PDFAnzeigen();
    }

    public String loadPDF(String adresse) {
        if (!adresse.toLowerCase().endsWith("pdf"))
            return null;
        String fileName = adresse.substring(adresse.lastIndexOf("/") + 1,
                adresse.lastIndexOf("."));
        String suffix = adresse.substring(adresse.lastIndexOf("."),
                adresse.length());
        File temp = null;
        try (InputStream in = new URL(adresse).openStream()) {
            temp = File.createTempFile(fileName, suffix);
            temp.deleteOnExit();
            Files.copy(in, Paths.get(temp.toURI()),
                    StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return temp.getAbsolutePath();
    }

    public static void createGUI(SwingController controller) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setLocationRelativeTo(null);
        frame.setTitle("PDF anzeigen");
        frame.setVisible(true);

        PropertiesManager properties = new PropertiesManager(
                System.getProperties(),
                ResourceBundle
                        .getBundle(PropertiesManager.DEFAULT_MESSAGE_BUNDLE));
        properties.set(PropertiesManager.PROPERTY_DEFAULT_ZOOM_LEVEL, "1.5");
        properties.set(PropertiesManager.PROPERTY_VIEWPREF_HIDETOOLBAR, "true");

        // nur für Event-Handling notwendig
        // controller.setIsEmbeddedComponent(true);
        SwingViewBuilder builder = new SwingViewBuilder(controller, properties);
        JPanel viewerPanel = builder.buildViewerPanel();
        frame.getContentPane().add(viewerPanel);
    }
}

这些是我收到的例外情况:

Exception in thread "main" java.lang.NoClassDefFoundError: org/icepdf/core/pobjects/PageTree
    at PDFAnzeigen.<init>(PDFAnzeigen.java:26)
    at PDFAnzeigen.main(PDFAnzeigen.java:32)
Caused by: java.lang.ClassNotFoundException: org.icepdf.core.pobjects.PageTree
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
    ... 2 more

我认为问题与 Swing 控制器有关,但我不知道该怎么办。

非常感谢您的任何帮助!

4

0 回答 0