http://bbs.csdn.net/topics/390055515

——————————————————————————————————————————————————

基本思路是:先将world转换为pdf,,在将pdf转换为JPG 。。然后清空缓存,,删除PDF文件!!

WordToPDF1.java

package test;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class WordToPDF1 {
public static void wordToPDF(String docfile, String toFile,int type) { ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word try { app.setProperty("Visible", new Variant(false));
//设置word程序非可视化运行 Dispatch docs = app.getProperty("Documents").toDispatch(); Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { docfile, new Variant(false),
new Variant(true) }, new int[]).toDispatch(); //new Variant(type),这里面的type的决定另存为什么类型的文件
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
toFile, new Variant(type) }, new int[]);
//作为PDF格式保存文件 Variant f = new Variant(false); System.out.println(toFile+".pdf"); //关闭文件
Dispatch.call(doc, "Close", f); } catch (Exception e) {
e.printStackTrace();
} finally {
//退出word程序
app.invoke("Quit", new Variant[] {});
}
} public static void main(String[] args) {
//源文件全路径
//String docfile ="D:\\1.doc";
String docfile ="D:\\2.docx";
// String docfile ="D:\\ceshi.doc";
//for (int i = 0; i < 18; i++) {
//些路径test为实际存在的目录,s后面为要另存为的文件名
String toFile="d:\\"+;
wordToPDF(docfile, toFile,);
//17 表示格式 为PDF
// }
}
}

PdfToJpg.java

package test;

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 javax.swing.SwingUtilities; 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 PdfToJpg {
public static void setup() throws IOException { // load a pdf from a byte buffer
File file = new File("d://1.pdf");
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, , channel
.size());
PDFFile pdffile = new PDFFile(buf); System.out.println("页数: " + pdffile.getNumPages()); BufferedImage tag = null; //将图片放入frame中
//JFrame frame =null;
//frame的名称
// frame = new JFrame("PDF Test");
//JLabel label = null;
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); for (int i = ; i <= pdffile.getNumPages(); i++) {
// draw the first page to an image
PDFPage page = pdffile.getPage(i);
// get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(, , (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
);
tag = new BufferedImage(rect.width, rect.height,
BufferedImage.TYPE_INT_RGB); // label = new JLabel(new ImageIcon(img)); // System.out.println(label); tag.getGraphics().drawImage(img, , , rect.width, rect.height,
null);
FileOutputStream out = new FileOutputStream("d://picture//gao//"
+ i + ".jpg"); // 输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); // JPEG编码
out.close(); // unmap(buf);
System.out.println("PDF文件转换JPG文件成功");
//将label添加给frame
// frame.add(label);
// System.out.println(frame); } channel.close(); raf.close(); unmap(buf);//如果要在转图片之后删除pdf,就必须要这个关闭流和清空缓冲的方法
// show the image in a frame
//frame.pack();
// frame.setVisible(true);
} /** * 清空缓冲 * @param buffer */ public static void unmap(final Object buffer) { AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { Method getCleanerMethod = buffer.getClass().getMethod(
"cleaner", new Class[]); getCleanerMethod.setAccessible(true); sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod
.invoke(buffer, new Object[]); cleaner.clean(); } catch (Exception e) { e.printStackTrace(); } System.out.println("清空缓冲成功"); return null; } }); } public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
PdfToJpg.setup();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}

源码分享!!!world文档转换为JPG图片的更多相关文章

  1. quartz.net任务调度:源码及使用文档

    目录: 1.quartz.net任务调度:源码及使用文档 2.quartz.net插件类库封装 前言 前段时间把自己封装quartz.net 类库的过程总结到博客园,有网友想要看一下源码,所以就把源码 ...

  2. 在MyEclipse显示struts2源码和doc文档及自动完成功能

    分类: struts2 2010-01-07 16:34 1498人阅读 评论(1) 收藏 举报 myeclipsestruts文档xmlfileurl 在MyEclipse显示struts2源码和d ...

  3. eclipse导入java和android sdk源码,帮助文档

    eclipse导入java和android sdk源码,帮助文档 http://blog.csdn.net/ashelyhss/article/details/37993261 JavaDoc集成到E ...

  4. 【C#附源码】数据库文档生成工具支持(Excel+Html)

    [2015] 很多时候,我们在生成数据库文档时,使用某些工具,可效果总不理想,不是内容不详细,就是表现效果一般般.很多还是word.html的.看着真是别扭.本人习惯用Excel,所以闲暇时,就简单的 ...

  5. 【C#附源码】数据库文档生成工具支持(Excel+Htm)

    数据库文档生成工具是用C#开发的基于NPOI组件的小工具.软件源码大小不到10MB.支持生成Excel 和Html 两种文档形式.了解更多,请访问:http://www.oschina.net/cod ...

  6. MyEclipse10查看Struts2源码及Javadoc文档

    1:查看Struts2源码 (1):Referenced Libraries >struts2-core-2.1.6.jar>右击>properties. (2):Java Sour ...

  7. Mahout源码分析之 -- 文档向量化TF-IDF

    fesh个人实践,欢迎经验交流!Blog地址:http://www.cnblogs.com/fesh/p/3775429.html Mahout之SparseVectorsFromSequenceFi ...

  8. MyEclipse查看Struts2源码及Javadoc文档

    一.查看Struts2源码 1.Referenced Libraries >struts2-core-2.1.6.jar>右击>properties. 2.Java Source A ...

  9. 基于SpringBoot的药店管理系统java药房管理系统(源码+数据库文件+文档)

    注意:该项目只展示部分功能,如需了解,评论区咨询即可. 1.开发环境 开发语言:Java 后台框架:SpringBoot 前端技术:HTML+CSS+JavaScript+Bootstrap+jQue ...

随机推荐

  1. sar详解(linux/unix通用)

    转载:http://www.toxingwang.com/linux-unix/2027.html sar是linux和unix通用的性能报告工具,常用命令组合如下: sar 命令行的常用格式: sa ...

  2. STL - 算法 - 普通拷贝

    list<, , , , , , , , }; vector<int> coll2; cout << "** collection 1: **" &l ...

  3. Python 3 操作json 文件

    背景 json 是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成. 一般表现形式是一个无序的 键值对 的集合. 资料: 官方文档: https://docs.python.org ...

  4. hdu 3065 AC自动机模版题

    题意:输出每个模式串出现的次数,查询的时候呢使用一个数组进行记录就好. 同上题一样的关键点,其他没什么难度了. #include <cstdio> #include <cstring ...

  5. PHP 5 时区

    PHP 5 时区 PHP 支持的时区 下面是 PHP 支持的时区的完整列表,这些对一些 PHP 日期函数很有用. 非洲 美洲 南极洲 北冰洋 亚洲 大西洋 大洋洲 欧洲 印度洋 太平洋 非洲 Afri ...

  6. 通过反射获取class文件中的构造方法,运行构造方法

    /* * 通过反射获取class文件中的构造方法,运行构造方法 * 运行构造方法,创建对象 * 1.获取class文件对象 * 2.从class文件对象中,获取需要的成员 * * Constructo ...

  7. flip 翻转效果 css3实现

    1.实现代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UT ...

  8. 参数依赖查找(ADL,Argument-dependent lookup)

    参数依赖查找(Argument-dependent lookup),又称 ADL 或 Koenig 查找,是一组于函数调用表达式查找非限定函数名的规则,包含对重载运算符的隐式函数调用.在通常非限定名称 ...

  9. 【转发】Visual Studio 2013 如何关闭调试而不关闭IIS Express

    在VS主面板打开:工具->选项->调试->编辑继续   取消选中[启用"编辑并继续"] 就OK了 (英文版的请对应相应的操作) 不过这是针对所有的调试,如果你想针 ...

  10. Android SDK 更新和下载慢怎么办?

    博客搬家:因为各种原因,我如今的博客将首发于blog.mojijs.com, 能够百度搜索 "姜哥的墨迹技术博客" , 或者 点击这里 本文地址 http://blog.mojij ...