【问题标题】:Print a PDF file using PDFBox jar file使用 PDFBox jar 文件打印 PDF 文件
【发布时间】:2015-10-26 14:28:15
【问题描述】:

我想从 java 打印一个 pdf 文件。我尝试了很多方法来打印 pdf,但都失败了。最后,我在stackoverflow中找到了解决方案。但是我尝试运行下面的代码但是编译代码时出现问题

PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.defaultPage();
Paper paper = new Paper();
paper.setSize(612.0, 832.0);
double margin = 10;
paper.setImageableArea(margin, margin, paper.getWidth() - margin, paper.getHeight() - margin);
pf.setPaper(paper);
pf.setOrientation(PageFormat.LANDSCAPE);

// PDFBox

PDDocument doc=PDDocument.load(file);
job.setPageable(new PDPageable(doc, job));

job.setJobName("funciona?");

if(job.printDialog()){
    job.print();
}

运行代码时出现了一些问题。我在下面给出错误消息

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    at org.apache.pdfbox.pdmodel.PDDocument.<clinit>(PDDocument.java:75)
    at Main.Create_Report_Card$4.actionPerformed(Create_Report_Card.java:419)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 38 more

【问题讨论】:

    标签: java pdfbox


    【解决方案1】:

    将 commons-logging jar 添加到您的项目中。如果您使用的是 mavenized 项目,请在 pom.xml 中添加其依赖项。

    如果您必须手动添加 commons-logging jar,请确保它在类路径中可用。

    对于 maven 依赖,在 pom.xml 中添加以下 artifactId。

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    

    您也可以从以下 URL 搜索或下载最新版本的 jar:

    http://repo1.maven.org/maven2/commons-logging/commons-logging/

    【讨论】:

    • 我将 apache commons-logging jar 添加到我的项目中。但是这个问题没有解决。所以请告诉我我能做什么。
    • .classpath 文件中是否存在 jar 条目?
    • 是的,我在我的项目中添加了一个源文件夹,然后将 jar 文件放入文件夹中,然后将其添加到类路径中
    • 您使用的是哪个版本的 PDFBOX 和 Apache commons-logging?
    • @NahidHasan “但问题没有解决” - 不要忘记检查你得到的错误是相同的还是新的。并阅读 PDFBox 网站上的“依赖项”页面。并说明您使用的是什么版本的 PDFBox。
    猜你喜欢
    • 1970-01-01
    • 2016-03-04
    • 2018-11-27
    • 2018-12-25
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    相关资源
    最近更新 更多