File deleted after conversion using documents4j

277 Views Asked by At

I am trying to use documents4j for converting file types. I have tried converting multiple file types from one to another. The code is successfully executed and I can intermittently see files getting converted and produced. But at the end of the execution the converted files are, I think, deleted automatically. I cannot see the converted files in the temp folder that is created.

I printed Future conversion object and here is the result: LocalConversion{pending=false, cancelled=false, done=true, priority=Priority{value=1000, creationTime=1527163966676}, file-system-target=C:\Users\USERNAME\Desktop\New folder\63cabe72-b2cf-4d52-b428-530dfc0fd63d\temp2}.

Is the target file moved to some other location after the conversion? Or am I missing some lines of code that copies the target file to other location?

I am using 1.0.3 versions of documents4j libs.

Code:

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
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 Test {

    public static void main (String[] args) {
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            InputStream in = new BufferedInputStream(new FileInputStream("SOME_.TXT_FILE"));
            IConverter converter = LocalConverter.builder()
                                                .baseFolder(new File("SOME_FOLDER_PATH"))
                                                 .workerPool(20, 25, 2, TimeUnit.SECONDS)
                                                 .processTimeout(5, TimeUnit.SECONDS)
                                                 .build();
            Future<Boolean> conversion = converter
                                            .convert(in).as(DocumentType.TEXT)
                                            .to(bo).as(DocumentType.DOCX)
                                            .prioritizeWith(1000)
                                            .schedule();
            conversion.get();
            System.out.println(conversion);
            if(conversion.isDone()) {
                System.out.println("Done");
            } else if(conversion.isCancelled()){
                System.out.println("Cancelled");
            }
        }catch(Exception e) {
            System.out.println(e);
        }
    }
}

I am using maven for dependencies management. My pom.xml

    <properties>
        <documents4j.version>1.0.3</documents4j.version>
    </properties>
    <dependencies>
        <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-api</artifactId>
        <version>${documents4j.version}</version>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-util-conversion</artifactId>
        <version>${documents4j.version}</version>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-transformer</artifactId>
        <version>${documents4j.version}</version>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-util-all</artifactId>
        <version>${documents4j.version}</version>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-local</artifactId>
        <version>${documents4j.version}</version>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-transformer-msoffice-word</artifactId>
        <version>${documents4j.version}</version>
    </dependency>
    <dependency>
        <groupId>com.documents4j</groupId>
        <artifactId>documents4j-transformer-msoffice-base</artifactId>
        <version>${documents4j.version}</version>
    </dependency>
</dependencies>
1

There are 1 best solutions below

0
On

I figured it out after some one debugging. So the target file is stored back in the ByteArrayOutputStream object bo. Just need to save that as a file.

 FileOutputStream fos = new FileOutputStream("C:\\Users\\USERNAME\\Desktop\\New folder\\OTGv4.docx");
        bo.writeTo(fos);

Hope this helps!