0

Apache poi を使用して ms-office .doc ファイルの画像を読み取る方法は? 次のコードで試しましたが、うまくいきません。

try {
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("C:\\DATASTORE\\ImageDocument.doc"));
    Document document = new Document();
    OutputStream fileOutput = new FileOutputStream(new File("C:/DATASTORE/ImageDocumentPDF.pdf"));
    PdfWriter.getInstance(document, fileOutput);
    document.open();

    HWPFDocument hdocument=new HWPFDocument(fs);
    Range range=hdocument.getOverallRange();
    PdfPTable createTable;
    CharacterRun run;
    PicturesTable picture=hdocument.getPicturesTable();
    int picoffset=run.getPicOffset();
    for(int i=0;i<range.numParagraphs();i++) {
        run =range.getCharacterRun(i);
        if(picture.hasPicture(run)) {
            Picture pic=picture.extractPicture(run, true);
            byte[] picturearray=pic.getContent();
            com.itextpdf.text.Image image=com.itextpdf.text.Image.getInstance(picturearray);
            document.add(image);
        }
    }
}

上記のコードを実行して画像のオフセット値を出力すると、 -1が表示され、入力ファイルに画像があるにもかかわらず、画像 を印刷するとfalseが返されます

解決策を見つけるのを手伝ってください。ありがとうございました

4

2 に答える 2

2
public static List<byte[]> extractImagesFromWord(File file) {
    if (file.exists()) {
        try {
            List<byte[]> result  = new ArrayList<byte[]>();
            if ("docx".equals(getMimeType(file).getExtension())) {
                org.apache.poi.xwpf.usermodel.XWPFDocument doc = new XWPFDocument(new FileInputStream(file));
                for (org.apache.poi.xwpf.usermodel.XWPFPictureData picture : doc.getAllPictures()) {
                    result.add(picture.getData());
                }
            } else if ("doc".equals(getMimeType(file).getExtension())) {
                org.apache.poi.hwpf.HWPFDocument doc = new HWPFDocument(new FileInputStream(file));
                for (org.apache.poi.hwpf.usermodel.Picture picture : doc.getPicturesTable().getAllPictures()) {
                    result.add(picture.getContent());
                }
            }
            return result;
        } catch (Exception e) {
            throw new RuntimeException( e);
        }
    }
    return null;
}
于 2014-03-20T09:26:53.293 に答える