简体   繁体   English

使用PDFBox jar文件打印PDF文件

[英]Print a PDF file using PDFBox jar file

I want to print a pdf file from java. 我想从Java打印pdf文件。 I try many ways to print pdf but fail. 我尝试了多种打印pdf的方法,但是失败了。 At last, I found a solution in stackoverflow. 最后,我在stackoverflow中找到了解决方案。 But I try to run the below code but there is a problem when compile the code 但是我尝试运行以下代码,但是编译代码时出现问题

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

There some problem occurs when I run the code. 运行代码时出现一些问题。 I give the error message in below 我在下面给出错误信息

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

Add commons-logging jar into your project. 将commons-logging jar添加到您的项目中。 If you are using mavenized project then add its dependency in pom.xml. 如果您使用的是Mavenized项目,则将其依赖项添加到pom.xml中。

If you have to add commons-logging jar manually then make sure it's available in classpath. 如果必须手动添加commons-logging jar,请确保它在classpath中可用。

For maven dependency, add following artifactId in pom.xml. 对于Maven依赖性,在pom.xml中添加以下artifactId。

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

Also you can search or download jar for latest versions from below URL: 您也可以从以下URL搜索或下载jar以获取最新版本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM