首页 文章

使用带有XWPF的Apache POI获取java中的单词缩略图

提问于
浏览
0

这个问题涉及使用HWPF获取单词doc的缩略图:

get thumbnail of word in java using Apache POI

我想用XWPF - 用于word xml文档的Apache POI API(.docx)来做这件事 . 没有getThumbnail()方法或类似方法 . 我怎样才能做到这一点?我想使用“另存为...”对话框中的“生成缩略图”选项提取Word生成的嵌入式缩略图 - 该工具适用于带有HWPF的.doc文档 .

2 回答

  • 0

    经过一些激烈的研究,特别是关于开放式包装公约,我自己找到了答案 . XWPF Document API中没有“getThumbnai()”便捷方法 . 缩略图必须通过特定的包关系提取:

    XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(docxFile));
    
    ArrayList<PackagePart> packageParts= wordDocument.getPackage().getPartsByRelationshipType
    ("http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail");
    
    PackagePart packagePart = packageParts.get(0);
    FileOutputStream fos = new FileOutputStream("c:\\temp\\thumb.emf");
    IOUtils.copy(packagePart.getInputStream(), fos);
    
  • 0

    你需要升级你的Apache POI版本!你需要使用Apache POI 3.15 beta 2 or newer

    在这些较新的版本上,您将在POIXMLProperties上找到一些与Thumbnail相关的方法,例如getThumbnailFilename()getThumbnailImage()

    要使用XWPF将缩略图图像保存到文件,您可以执行以下操作:

    XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(docxFile));
    POIXMLProperties props = workDocument.getProperties();
    
    String thumbnail = props.getThumbnailFilename();
    if (thumbnail == null) {
       // No thumbnail
    } else {
       FileOutputStream fos = new FileOutputStream("c:\\temp\\"+thumbnail);
       IOUtils.copy(props.getThumbnailImage(), fos);
    }
    

相关问题