#想编辑一个pdf预览的小程序,完成后运行报错,fontbox-2.0.9.jar和pdfbox-2.0.9.jar都已经添加,哪位帮忙指点一下代码如下:
#源程序
package pdf预览;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
public class yulan {
public static void main(String[] args) {
File pdfFile = new File("path/to/your/document.pdf"); // PDF文件路径
try (PDDocument document = PDDocument.load(pdfFile)) {
PDFRenderer pdfRenderer = new PDFRenderer(document);
// 预览第一页
BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);
ImageIO.write(bufferedImage, "JPEG", new File("path/to/output/page-1.jpg")); // 输出图片路径
} catch (IOException e) {
e.printStackTrace();
}
}
}
报错如下:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.pdfbox.pdmodel.PDDocument.<clinit>(PDDocument.java:95)
at pdf预览.yulan.main(yulan.java:16)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
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:520)
... 2 more