Search code examples
javaimagepdfjarsave

Im trying to save imge in java as pdf


Here are my imports: 
package semestralka;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
    public void save(String format, String imageSaveName) {
//        BufferedImage finalCard = new BufferedImage(CARD_WIDTH, CARD_HEIGHT, BufferedImage.TYPE_INT_RGB);
//        Graphics2D img = (Graphics2D) finalCard.getGraphics();
//        paint(img);
        
        BufferedImage panelImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D panelGraphics = panelImage.createGraphics();

        paint(panelGraphics);

        BufferedImage finalCard = new BufferedImage(CARD_WIDTH, CARD_HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D img = finalCard.createGraphics();

        img.drawImage(panelImage, 0, 0, null);
        
        if (format.equals("pdf")) {
            System.out.println(format);
            try{
                PDDocument doc = new PDDocument();
                PDPage page = new PDPage();
                doc.addPage(page);

                PDPageContentStream contentStream = new PDPageContentStream(doc, page);

                PDImageXObject pdImage = LosslessFactory.createFromImage(doc, finalCard);

                contentStream.drawImage(pdImage, 300, 300, CARD_WIDTH, CARD_HEIGHT);

                contentStream.close();

                File file = new File(imageSaveName + "." + format);
                doc.save(file);
                doc.close();
                System.out.println("PDF saved successfully");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            //img.drawImage(icon, CARD_WIDTH - QRSize, CARD_HEIGHT / 8, QRSize, QRSize, this);

            File file = new File(imageSaveName + "." + format);
            try {
                ImageIO.write(finalCard, format, file);
                System.out.println("Image saved successfully");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    And here is an error that I'm getting: Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDocument
    at semestralka.DesignPanel.save(DesignPanel.java:160)
    at semestralka.Card$12.actionPerformed(Card.java:739)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2314)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:407)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6620)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3398)
    at java.desktop/java.awt.Component.processEvent(Component.java:6385)
    at java.desktop/java.awt.Container.processEvent(Container.java:2266)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4995)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4827)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4827)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:98)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocument
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 37 more

so I'm trying to save image as pdf but there seems to by problem with my imports, but I couldn't find any solution I imported these jars as I always do, I have them all in the same file so there shouldn't be with not finding file or smt, when installing I also followed a youtube video, if anyone could help I would be very grateful

if any other info is needed let me know ill try to provide if possible

so this is my external jars


Solution

  • You are using two versions of the pdfbox library which causes java to throw the NoClassDefFoundError, because there are two classes with same package and name in the classpath.

    Remove one of the versions to resolve.

    It would be advisable to use a build system like Maven or Gradle to manage the dependencies and avoid this kind of errors.