0

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

enter image description here

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
Vicky Johnson
  • 13
  • 1
  • 4

3 Answers3

1

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

Sandunka Mihiran
  • 556
  • 4
  • 19
  • i already added jar files, only i need to know exact jar files – Vicky Johnson Mar 11 '20 at 11:50
  • Try adding documents4j-local jar file. You can download it from here https://mvnrepository.com/artifact/com.documents4j/documents4j-local/0.2.1 Then add it to the classpath – Sandunka Mihiran Mar 11 '20 at 11:53
  • I added your jar file it show exception : Exception in thread "main" java.lang.NoClassDefFoundError: com/documents4j/job/ConverterAdapter – Vicky Johnson Mar 11 '20 at 12:08
  • Also add this jar (documents4j-transformer-msoffice-word) https://mvnrepository.com/artifact/com.documents4j/documents4j-transformer-msoffice-word/0.2.1 – Sandunka Mihiran Mar 11 '20 at 12:29
  • "Exception in thread "main" java.lang.NoClassDefFoundError: com/documents4j/job/ConverterAdapter" came, and again i added documents4j.util.conversion jar file, agian its show "Error occurred during initialization of boot layer java.lang.module.ResolutionException: Module documents4j.local contains package com.documents4j.job, module documents4j.util.conversion exports package com.documents4j.job to documents4j.local" – Vicky Johnson Mar 12 '20 at 05:10
0

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.

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
  • I added your jar file it show exception : Exception in thread "main" java.lang.NoClassDefFoundError: com/documents4j/job/ConverterAdapter – Vicky Johnson Mar 11 '20 at 11:48
  • @VickyJohnson add this https://repo1.maven.org/maven2/com/documents4j/documents4j-util-conversion/1.0.3/documents4j-util-conversion-1.0.3.jar – seenukarthi Mar 11 '20 at 11:50
  • Again it showing error : The package com.documents4j.job is accessible from more than one module: documents4j.local, documents4j.util.conversion – Vicky Johnson Mar 11 '20 at 12:05
  • Error occurred during initialization of boot layer java.lang.module.ResolutionException: Module documents4j.local contains package com.documents4j.job, module documents4j.util.conversion exports package com.documents4j.job to documents4j.local – Vicky Johnson Mar 11 '20 at 12:06
0

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.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84