我试图使用documents4j(https://bintray.com/raphw/maven/documents4j)来使用以下代码将docx文件转换为pdf:
提前非常感谢您,我不知道该如何运作
package documents4j;
import java.io.File;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
public class HelloPDF {
public static void main(String[] args) {
File wordFile = new File("c:/datos/Resultados.docx"), target = new File("c:/datos/Resultados.pdf");
IConverter converter = LocalConverter.builder().baseFolder(new File("c:/datos/"))
.workerPool(20, 25, 2, TimeUnit.SECONDS)
.processTimeout(5, TimeUnit.SECONDS).build();
Future<Boolean> conversion = converter.convert(wordFile).as(DocumentType.DOCX).to(target).as(DocumentType.PDF)
// .prioritizeWith(1000) // optional
.schedule();
}
}
但是我遇到一个非法的国家例外
Exception in thread "main" java.lang.IllegalStateException: class com.documents4j.conversion.msoffice.MicrosoftWordBridge could not be created by a (File, long, TimeUnit) constructor
at com.documents4j.conversion.ExternalConverterDiscovery.make(ExternalConverterDiscovery.java:32)
at com.documents4j.conversion.ExternalConverterDiscovery.makeAll(ExternalConverterDiscovery.java:42)
at com.documents4j.conversion.ExternalConverterDiscovery.loadConfiguration(ExternalConverterDiscovery.java:85)
at com.documents4j.conversion.DefaultConversionManager.<init>(DefaultConversionManager.java:22)
at com.documents4j.job.LocalConverter.makeConversionManager(LocalConverter.java:79)
at com.documents4j.job.LocalConverter.<init>(LocalConverter.java:51)
at com.documents4j.job.LocalConverter$Builder.build(LocalConverter.java:186)
at documents4j.HelloPDF.main(HelloPDF.java:20)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.documents4j.conversion.ExternalConverterDiscovery.make(ExternalConverterDiscovery.java:30)
... 7 more
Caused by: java.lang.NoClassDefFoundError: com/documents4j/util/Reaction$IExceptionBuilder
at com.documents4j.conversion.msoffice.AbstractMicrosoftOfficeBridge.tryStart(AbstractMicrosoftOfficeBridge.java:34)
at com.documents4j.conversion.msoffice.MicrosoftWordBridge.startUp(MicrosoftWordBridge.java:44)
at com.documents4j.conversion.msoffice.MicrosoftWordBridge.<init>(MicrosoftWordBridge.java:39)
... 12 more
Caused by: java.lang.ClassNotFoundException: com.documents4j.util.Reaction$IExceptionBuilder
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 15 more
[Shutdown hook: com.documents4j.job.LocalConverter] INFO com.documents4j.job.ConverterAdapter - Tried to deregister shut down hook in shut down period
java.lang.IllegalStateException: Shutdown in progress
at java.lang.ApplicationShutdownHooks.remove(ApplicationShutdownHooks.java:82)
at java.lang.Runtime.removeShutdownHook(Runtime.java:237)
at com.documents4j.job.ConverterAdapter.deregisterShutdownHook(ConverterAdapter.java:121)
at com.documents4j.job.ConverterAdapter.cleanUp(ConverterAdapter.java:107)
at com.documents4j.job.ConverterAdapter.shutDown(ConverterAdapter.java:98)
at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:109)
at com.documents4j.job.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:134)
Exception in thread "Shutdown hook: com.documents4j.job.LocalConverter" java.lang.NullPointerException
at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:100)
at com.documents4j.job.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:134)
最佳答案
好的,我找到了答案。问题是我没有包括所有库,我补充道:
documents4j-util-all-1.0.3
documents4j-util-standalone-1.0.3
终于成功了
关于java - Documents4j IllegalStateException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41376379/