转载:https://www.cnblogs.com/start-fxw/p/5853671.html

方法:1、文件转换成pdf(采用openoffice或者jacob)

     2、抓取pdf首页图

第一步:采用jacob:

a、下载jacob  注意区分32位,64位,否则不能用
    将dll文件放在java bin目录下即可
    jar包引入项目

b、转换pdf

package cn.bnsr.edu_yun.util;import java.io.File; import java.io.IOException;import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class TestUtil {static final int wdFormatPDF = 17;// PDF 格式   public void wordToPDF(String sfileName,String toFileName){   System.out.println("启动Word...");     long start = System.currentTimeMillis();     ActiveXComponent app = null; Dispatch doc = null; try {     app = new ActiveXComponent("Word.Application");     app.setProperty("Visible", new Variant(false)); Dispatch docs = app.getProperty("Documents").toDispatch();  //            doc = Dispatch.call(docs,  "Open" , sourceFile).toDispatch();  doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] {                   sfileName, new Variant(false),new Variant(true) }, new int[1]).toDispatch();              System.out.println("打开文档..." + sfileName); System.out.println("转换文档到PDF..." + toFileName);     File tofile = new File(toFileName);     if (tofile.exists()) {     tofile.delete();     }       //            Dispatch.call(doc, "SaveAs",  destFile,  17);                   Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {               toFileName, new Variant(17) }, new int[1]);   long end = System.currentTimeMillis();     System.out.println("转换完成..用时:" + (end - start) + "ms.");               } catch (Exception e) { e.printStackTrace(); System.out.println("========Error:文档转换失败:" + e.getMessage());     } finally { Dispatch.call(doc,"Close",false); System.out.println("关闭文档"); if (app != null)     app.invoke("Quit", new Variant[] {});     } //如果没有这句话,winword.exe进程将不会关闭 ComThread.Release();    } public static void main(String[] args) { TestUtil d = new TestUtil(); d.wordToPDF("D:\\work\\1111.docx", "d:\\work\\1111.pdf"); } }

第二步:抓取首页图:

package cn.bnsr.edu_yun.util;import java.awt.Image;import java.awt.Rectangle;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.RandomAccessFile;import java.lang.reflect.Method;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.security.AccessController;import java.security.PrivilegedAction;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;import com.sun.pdfview.PDFFile;import com.sun.pdfview.PDFPage;public class PdfImageUtil {// 将PDF格式的文件转换为JPG格式的文件public static void pdfToJPG(String inputFile)throws IOException {// load a pdf from a byte bufferFile 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 &// heightrect, // clip rectnull, // null for the ImageObservertrue, // fill background with whitetrue // 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( "D:\\work\\s111"+ ".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>(){@Overridepublic 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;}});}}

第三步、搞定,加入自己的业务逻辑,有问题欢迎提问。

  

java 上传文件-生成文件首页缩略图 生成pdf 抓取图片相关推荐

  1. JAVA上传tiff格式文件

    文章目录 Java上传tiff格式文件 Java上传tiff格式文件 加入pom依赖 <dependency><groupId>com.twelvemonkeys.imagei ...

  2. java上传和下载文件代码_JavaWeb中上传和下载文件实例代码

    一丶先引入上传下载的lib 二丶上传的的servlet package com.test.action; import java.io.file; import java.io.fileoutputs ...

  3. java上传下载_Java文件上传与文件下载实现方法详解

    本文实例讲述了Java文件上传与文件下载实现方法.分享给大家供大家参考,具体如下: Java文件上传 数据上传是客户端向服务器端上传数据,客户端向服务器发送的所有请求都属于数据上传.文件上传是数据上传 ...

  4. java上传与下载文件_java实现文件的上传和下载功能

    准备工作 (视频教程推荐:java课程) 需要导入的jar包 运行截图 文件上传截图 文件下载截图 上传文件代码servlet@WebServlet(name = "UploadServle ...

  5. java 上传csv/xslx文件,预览,导入到数据库中

    声明:我的工程是父子工程聚合 父工程为server,子工程为browser,auth,mbg,common browser继承了common,mbg,auth auth继承了mbg,common 文件 ...

  6. java上传与下载文件_JavaWeb之实现文件上传与下载

    1.什么是上传和下载? -  数据上传是指客户端向服务器上传数据,客户端向服务器发送的所有请求都属于数据上传.文件上传是数据上传的一种特例,指的是客户端向服务器上传文件.即将保存在客户端的文件上传至服 ...

  7. java 上传zip压缩文件并且解压

    public void parseAndAdd(MultipartFile zipFile) {//C:\Users\登录用户~1\AppData\Local\Temp\String pathName ...

  8. java上传+限制单文件,VereMVC 之 单文件上传

    1.打开 Eclipse 新建一个 test 项目   Eclipse -> File -> New -> Dynamic Web Project 2.在 test/WebConte ...

  9. java 上传文件及预览_SpringBoot上传下载文件及在线预览

    SpringBoot上传下载文件及在线预览 今天大概就说说如何使用SpringBoot进行上传和下载以及在线预览文件 本篇主要介绍上传下载的功能,对于界面就简单一点,大致如下: 一.老规矩还是先看看小 ...

  10. java上传文件的要点_Uploadify上传文件方法

    Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示.不过官方提供的实例时php版本的,本文将详细介绍Uploadify在Aspnet中的使用,您也可以点击下面的链接进行演示 ...

最新文章

  1. java 随机数 分布_java – 随机数的分布
  2. MySQL授权命令grant的使用方法
  3. 如何提高一个研发团队的“代码速度”?
  4. spring 注解说明以及@Resource和@Autowired的区别
  5. Java学习笔记之equals和Objects.equals
  6. oracle11连12c报权限错误,关于Oracle12c连接报错的问题
  7. IAP:物联网终端软件升级技术
  8. 第三章:使用email-ext替换Jenkins的默认邮件通知
  9. linux内核zfs,Linus Torvalds 不建议使用 ZFS On Linux
  10. 福建水院计算机一级清考,取消毕业“清考”大学生还敢混日子吗
  11. UBuntu CMake工程配置基础
  12. C语言,使用结构体读入两个在同一年的日期,判断日期是否合法,并计算两个日期之间相差的天数。结构体定义如下:...
  13. CAD编辑指南2:五大CAD查看器的功能盘点
  14. CTFshow-大牛杯
  15. 计算机内打不开小米路由器,小米路由器登录界面打不开怎么办?
  16. 在线html编辑器---htmlarea 附件,在线HTML编辑器htmlarea简单使用
  17. appStore上传苹果应用程序软件发布流程
  18. 如何使用Sheffield大学的Matlab GA遗传算法工具箱
  19. python模拟登录163邮箱_python模拟登陆163邮箱并下载邮件内容(第三版代码片段)
  20. 【Lua】Lua基础语法

热门文章

  1. win10安装mmcv和mmcv-full报错
  2. JS 报错getElementsByClassName.appendChild报错“Uncaught TypeError: s.appendChild is not a function”
  3. 12月更新!EasyOps全平台产品能力再升级,新增22+功能亮点解读~
  4. 西工大计算机学院二级教授,计算机学院高武教授:践行科研育人,培养拔尖创新人才...
  5. AsyncTask下载网络文件,并显示下载进度
  6. java mxs mxm_mxm格式在3DMax中具体怎么用(步骤)?
  7. 火狐浏览器如何关闭右侧的“百度热搜”
  8. windows10电脑在哪配置DNS
  9. 硅烷聚乙二醇硅烷,Silane-PEG-Silane同官能团科研试剂,化学结构式
  10. 爆款短视频速成技巧之视频发布篇,短视频介绍文案怎么写(下)