0

I have got a problem with Icepdf. I would like to show a pdf with icepdf, but it does not work. I think the Swing Controller makes a problem, but that's not clearly shown from the debugger. Here is the code

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);
    }
}

these are the exceptions that i've received:

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

I think the problem is connected with the Swing Controller, but I do not know, what to do.

Thank you very much for any kind of help!

PePe
  • 1
  • i dont think its caused by swing, it's probably becuase Icepdf is not a part of the jdk, and you should add its jar in your project before using it – oubaydos Dec 18 '21 at 18:34
  • refer to this question [How can i Open .pdf file in java](https://stackoverflow.com/questions/2546968/open-pdf-file-on-the-fly-from-a-java-application) because i think that icePdf is a really old way and there are better ways – oubaydos Dec 18 '21 at 18:37
  • I've already used icepdf-core.jar and icepdf-viewer.jar. So that should not cause the problem. Maybe something else is missing, that i do not know... – PePe Dec 19 '21 at 11:09
  • Double check your class path, org.icepdf.core.pobjects.PageTree lives in the icepdf-core.jar. The stack trace seems to indicate that the icepdf-core.jar is missing or wasn't created correctly. I've been maintaining the library over github (https://github.com/pcorless/icepdf/) if you wanted to try the latest snapshot with greatly improved font rendering support. – pcorless Dec 19 '21 at 20:14
  • icepdf.core and icepdf.viever version should be same – Kashinath Jun 20 '22 at 11:04

0 Answers0