Question

I'm trying to save the images extracted from a. doc file in a temporary folder. The folder I was able to create it and also the images were extracted but I have no idea how to save them in the folder. Can you help?

This is what has been done so far:

private static void documentImagesCapture(HWPFDocument doc) {
    PicturesTable picturesTable = doc.getPicturesTable();
    List<Picture> allPictures = picturesTable.getAllPictures();
    System.out.println("Number of pictures in the document: "+allPictures.size());

    if (allPictures.size()>0){

        final Path folderPath = Paths.get("src/test/resources/test-documents/");
        final Path tmpDir;

        try {
            // Create tmp folder
            tmpDir = Files.createTempDirectory(folderPath, null);
            System.out.println("This is a temporary folder: " + tmpDir);

            // Extract a Picture file
            for (Picture pic : allPictures){
                System.out.println("this is the picture that I want to save : " + pic);
                //Save pic in tmpDir???????
            }

        } catch (IOException e) {
            System.err.println(">>>>>>>>> ERROR >>>>>>>>> WordDocSplitter.documentImagesCapture()");
            e.printStackTrace();
        }
    }
}

Thanks


Thanks David!!!! I solved in this way:

for (Picture pic : allPictures){
    File newFilePic = new File(pic.suggestFullFileName());
    pic.writeImageContent(new DataOutputStream(new FileOutputStream(newFilePic)));
    FileUtils.copyFileToDirectory(newFilePic, new File(tmpDir.toString()));
    FileUtils.forceDelete(newFilePic);
}
Was it helpful?

Solution

Use the writeImageContent method for the Picture object. Documentation here.

writeImageContent

public void writeImageContent(java.io.OutputStream out) throws java.io.IOException

Writes Picture's content bytes to specified OutputStream. Is useful when there is need to write picture bytes directly to stream, omitting its representation in memory as distinct byte array. Parameters: out - a stream to write to Throws: java.io.IOException - if some exception is occured while writing to specified out

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top