简体   繁体   中英

How to write extracted image from pdf to a file

Hopefully this is simple.

I am using pdfbox to extract images from a pdf. I want to write the images to a folder. I don't seem to get any output (the folder has read and write privileges).

I am probably not writing the output stream properly I think.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
public final class JavaImgExtactor
{

    public static void main(String[] args) throws IOException{
        Stuff();
    }

    @SuppressWarnings("resource")
    public static void Stuff() throws IOException{
        File inFile = new File("/Users/sebastianzeki/Documents/Images Captured with Proc Data Audit.pdf");
    PDDocument document = new PDDocument();
            //document=null;
    try {
        document = PDDocument.load(inFile);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    List pages = document.getDocumentCatalog().getAllPages();
    Iterator iter = pages.iterator();
    while (iter.hasNext()) {
                PDPage page = (PDPage) iter.next();
                System.out.println("page"+page);
                PDResources resources = page.getResources();
                Map pageImages = resources.getImages();
                if (pageImages != null) {

                    Iterator imageIter = pageImages.keySet().iterator();
                    System.out.println("Success"+imageIter);
                    while (imageIter.hasNext()) {

                        String key = (String) imageIter.next();
                        PDXObjectImage image = (PDXObjectImage) pageImages.get(key);
                        FileOutputStream out = new FileOutputStream("/Users/sebastianzeki/Documents/ImgPDF.jpg");
                        try {
                            image.write2OutputStream(out);

                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
    }
}
}

You are not closing the output stream, and the file name is always the same.

try (FileOutputStream out = new FileOutputStream("/Users/sebastianzeki/Documents/ImgPDF" + key + ".jpg") {
    write2OutputStream(out);
} (Exception e) {
    printStackTrace();
}

try-with-resources will automatically close out . Not sure whether key is usable as file name part.

image.write2OutputStream(out); writes the bytes from the image object to the out FileOutputStream object but it doesn't flush the buffer of out .

Add it should do the job :

out.flush();

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM