I am trying to convert Docx file to pdf file in java using documents4j 1.0.3.jar, but I can't to able to convert that. I saw some references, everyone suggesting to do in the maven project with pom file dependencies. we don't maven project, I want solution only in a java project with added jar files.
code :
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
public class Doit {
public static void main(String[] args) {
File inputWord = new File("/home/enad2/Downloads/Sample.docx");
File outputFile = new File("/home/enad2/Desktop/Sample.pdf");
try {
InputStream docxInputStream = new FileInputStream(inputWord);
OutputStream outputStream = new FileOutputStream(outputFile);
IConverter converter = LocalConverter.builder().build();
converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream)
.as(DocumentType.PDF).execute();
outputStream.close();
System.out.println("success");
} catch (Exception e) {
e.printStackTrace();
}
}
You can add a jar file in Eclipse by: right-clicking on the Project → Build Path → Configure Build Path. Under Libraries tab, click Add Jars or Add External JARs and give the Jar.
For further instructions, visit https://www.edureka.co/community/4028/how-to-import-a-jar-file-in-eclipse
The class com.documents4j.job.LocalConverter
is not in your classpath since you are getting the error you have to add documents4j-local
jar you can get the jar here . Download the jar and add it to the classpath.
Here's the output of mvn dependency:tree
[INFO] +- com.documents4j:documents4j-local:jar:1.1.2-SNAPSHOT:compile
[INFO] | +- com.documents4j:documents4j-api:jar:1.1.2-SNAPSHOT:compile
[INFO] | +- com.documents4j:documents4j-transformer:jar:1.1.2-SNAPSHOT:compile
[INFO] | +- com.documents4j:documents4j-util-conversion:jar:1.1.2-SNAPSHOT:compile
[INFO] | | \- javax.activation:activation:jar:1.1.1:compile
[INFO] | +- com.google.guava:guava:jar:28.1-jre:compile
[INFO] | | +- com.google.guava:failureaccess:jar:1.0.1:compile
[INFO] | | +- com.google.guava:listenablefuture:jar:9999.0-empty-to-avoid-conflict-with-guava:compile
[INFO] | | +- com.google.code.findbugs:jsr305:jar:3.0.2:compile
[INFO] | | +- org.checkerframework:checker-qual:jar:2.8.1:compile
[INFO] | | +- com.google.errorprone:error_prone_annotations:jar:2.3.2:compile
[INFO] | | +- com.google.j2objc:j2objc-annotations:jar:1.3:compile
[INFO] | | \- org.codehaus.mojo:animal-sniffer-annotations:jar:1.18:compile
[INFO] | +- org.zeroturnaround:zt-exec:jar:1.11:compile
[INFO] | \- org.slf4j:slf4j-api:jar:1.7.28:compile
[INFO] +- com.documents4j:documents4j-transformer-msoffice-word:jar:1.1.2-SNAPSHOT:compile
[INFO] | +- com.documents4j:documents4j-transformer-msoffice-base:jar:1.1.2-SNAPSHOT:compile
[INFO] | +- com.documents4j:documents4j-transformer-api:jar:1.1.2-SNAPSHOT:compile
[INFO] | | \- com.documents4j:documents4j-util-all:jar:1.1.2-SNAPSHOT:compile
[INFO] | \- com.documents4j:documents4j-util-transformer-process:jar:1.1.2-SNAPSHOT:compile
[INFO] \- org.slf4j:slf4j-simple:jar:1.7.28:compile
You might want to use maven outside your project just to create a shaded jar, or to gather all these deps for you so you can easily copy them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.