使用 Apache POI 在 Java 中获取单词的缩略图 [英] get thumbnail of word in java using Apache POI

查看:20
本文介绍了使用 Apache POI 在 Java 中获取单词的缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在研究jsf中的一个网络共享项目.在这个项目中,用户可以上传.doc,.pdf,.ppt,..etc等文件.我想将此文档的第一页显示为缩略图.经过一番谷歌搜索后,我找到了 Apache POI.有人对我的问题有什么建议吗?如何返回word doc第一页的缩略图?我试试这个代码.这个代码只是得到 word doc 包含的第一张图片:

I study on a web sharing project in jsf.In this project users can upload documents such as .doc,.pdf,.ppt,..etc . I want show this documents first pages as a thumbnail. After some googling around I found Apache POI.Can anybody has any suggestion for my problem? How can I return thumbnail image of word doc's first page? I try this code.This code just get first picture that word doc contains:

        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:\\test.doc"));
        HWPFDocument doc = new HWPFDocument(fs);
        PicturesTable pt=doc.getPicturesTable();
        List<Picture> p=pt.getAllPictures();
        BufferedImage image=ImageIO.read(new ByteArrayInputStream(p.get(0).getContent()));
        ImageIO.write(image, "JPG", new File("d:\\test.jpg"));

推荐答案

你在做什么,一事无成.HWPFDocument 可以提取嵌入在文档中的缩略图(保存文件时,选中添加预览"选项).所以HWPFDocument只能提取有缩略图的文档的缩略图.

What's you are doing make nothing. HWPFDocument can extract thumbnail embedded in document (when saving files, check on 'add preview' option). So HWPFDocument can extract only thumbnail of documents having thumbnail.

甚至,要做到这一点,您必须:{代码}

Even, to do that, you have to make: {code}

static byte[] process(File docFile) throws Exception {
    final HWPFDocumentCore wordDocument = AbstractWordUtils.loadDoc(docFile);
    SummaryInformation summaryInformation = wordDocument.getSummaryInformation();
    System.out.println(summaryInformation.getAuthor());
    System.out.println(summaryInformation.getApplicationName() + ":" + summaryInformation.getTitle());
    Thumbnail thumbnail = new Thumbnail(summaryInformation.getThumbnail());
    System.out.println(thumbnail.getClipboardFormat());
    System.out.println(thumbnail.getClipboardFormatTag());
    return thumbnail.getThumbnailAsWMF();
}

{代码}之后,您可能必须将 WMF 文件格式转换为更常见的格式(jpeg、png...).ImageMagick 可以提供帮助.

{code} after that, you have to probably convert WMF file format to more common format (jpeg, png...). ImageMagick can help.

这篇关于使用 Apache POI 在 Java 中获取单词的缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆