Java中将word生成缩略图

解决思路是:

1、先将word生成pdf,这个采用openoffice或者jacob

2、然后将pdf生成图片

具体代码如下:

private void officeToPdf(){
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
} catch (ConnectException e) {
e.printStackTrace();
}
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
converter.convert(officeFile, pdfFile);
// close the connection
connection.disconnect();
}


// 将PDF格式的文件转换为JPG格式的文件
private  void pdfToJPG(String inputFile)
throws IOException {


// load a pdf from a byte buffer
File file = new File(inputFile);


RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
//这句代码通道建立了map映射,如果要删除file那么得接触映射
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
channel.size());
PDFFile pdffile = new PDFFile(buf);
int totalpage =pdffile.getNumPages();
for (int i = 1; i <= totalpage; i++) {
if (i == 1) {
// draw the first page to an image
// 以图片的形式来描绘首页
PDFPage page = pdffile.getPage(i);
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
.getWidth(), (int) page.getBBox().getHeight());

// generate the image
// 生成图片
Image img = page.getImage(rect.width, rect.height, // width &
// height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
BufferedImage tag = new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img.getScaledInstance(rect.width, rect.height,  Image.SCALE_SMOOTH), 0, 0, rect.width, rect.height,
null);

FileOutputStream out = new FileOutputStream( imagePath+"\\"+fileName.substring(fileName.lastIndexOf("/")+1)
+ ".jpg"); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // JPEG编码
// 关闭输出流
out.close();
break;
}
}
buf.clear();
channel.close();
raf.close();
unmap(buf);
file.delete();

}
//解除map映射
public static <T> void unmap(final Object buffer) {
   AccessController.doPrivileged(new PrivilegedAction<T>(){
       @Override
   public T run() {                
       try {
       Method getCleanerMethod = buffer.getClass().getMethod("cleaner", new Class[0]);
       getCleanerMethod.setAccessible(true);
       sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(buffer, new Object[0]);
       cleaner.clean();
       } catch(Exception e) {
           e.printStackTrace();
       }               
       return null;
   }           
   });
}

需要注意的是,我生成完图片后将pdf删除,但是删除失败,经过大牛的指点加上了unmap就ok了,故写下来分享给大家。

需要的jar包是PDFRenderer.jar和jodconverter-2.2.2.jar包,如果使用jacob还得加入jacob.jar

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱人间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值