资源简介
这是我在网上找两天的结果,用的是java的jacob把word转成pdf文件,然后把pdf转成图片,图片还算清晰,但是转换的过程有点慢,希望各位给点意见!
我这里面有相关jar包,操作步骤,亲测可用!
代码片段和文件信息
package limit.test;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
public class wordToImages {
public static void main(String[] args) throws Exception {
String source = “e:\\罗运简历2.doc“;
wordToImages wpi = new wordToImages();
wpi.transformationImages(source);
}
public void transformationImages(String source) throws Exception {
// 生成UUID作为图片名称
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
String uuidStr = str.replace(“-“ ““);
System.out.println(str + “++++++“ + uuidStr);
File directory = new File(““);// 设定为当前文件夹
String pdfName = directory.getCanonicalPath() + “\\“ + uuidStr + “.pdf“;// 生成的pdf文件名称和路径
String imgName = directory.getCanonicalPath() + “\\“ + uuidStr + “.jpg“;// 生成的图片文件名称和路径
// 1.先把word转成pdf
wordToPdf(source pdfName);
long start = System.currentTimeMillis();
PDDocument document = new PDDocument();
File pdfFile = new File(pdfName);
document = PDDocument.load(pdfFile (String) null);
int size = document.getNumberOfPages();
List piclist = new ArrayList();
for (int i = 0; i < size; i++) {
BufferedImage image = new PDFRenderer(document).renderImageWithDPI(i 130 ImageType.RGB);
piclist.add(image);
}
document.close();
// 2.再把pdf转换成图片
yPic(piclist imgName);
long end = System.currentTimeMillis();
System.out.println(“PDF转Images时间:“ + (end - start));
//删除pdf文件
pdfFile.delete();
}
static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
static final int wdFormatPDF = 17;// word转PDF 格式
public boolean wordToPdf(String source String target) {
System.out.println(“Word转PDF开始启动...“);
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent(“Word.Application“);
app.setProperty(“Visible“ false);
Dispatch docs = app.getProperty(“Documents“).toDispatch();
System.out.println(“打开文档:“ + source);
Dispatch doc = Dispatch.call(docs “Open“ source false true).toDispatch();
System.out.println(“转换文档到PDF:“ + target);
File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc “SaveAs“ target wdFormatPDF);
Dispatch.call(doc “Close“ false);
long end = System.currentTimeMillis();
System.out.println(“Word转PDF,转换完成,用时:“ + (end - start) + “ms“);
return true;
} catch (Exception e) {
System.out.println(“Word转PDF出错:“ + e.getMessage());
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-12-26 16:36 wordToImages\
文件 205312 2015-08-05 05:01 wordToImages\jacob-1.18-x64.dll
目录 0 2017-12-26 16:30 wordToImages\jar\
文件 60686 2017-12-25 11:29 wordToImages\jar\commons-logging-1.1.1.jar
文件 1483772 2017-12-26 14:54 wordToImages\jar\fontbox-2.0.8.jar
文件 49231 2015-08-05 05:01 wordToImages\jar\jacob.jar
文件 2485070 2017-12-26 14:48 wordToImages\jar\pdfbox-2.0.8.jar
目录 0 2017-12-26 16:36 wordToImages\Test\
文件 560 2017-12-26 16:29 wordToImages\Test\.classpath
文件 386 2017-12-26 16:26 wordToImages\Test\.project
目录 0 2017-12-26 16:36 wordToImages\Test\.settings\
文件 598 2017-12-26 16:26 wordToImages\Test\.settings\org.eclipse.jdt.core.prefs
文件 469784 2017-12-26 16:32 wordToImages\Test\01b13a3835b0469dbdfffa71ae5e85cf.jpg
文件 469784 2017-12-26 16:31 wordToImages\Test\0c32a3abb8b1401cbcb6d2881f30b572.jpg
文件 108533 2017-12-26 16:28 wordToImages\Test\45459008ee57445faef9f9d9883cfec5.pdf
文件 184104 2017-12-26 16:29 wordToImages\Test\71895d2bb4304ff394f924ce7bd43a55.jpg
目录 0 2017-12-26 16:36 wordToImages\Test\bin\
目录 0 2017-12-26 16:36 wordToImages\Test\bin\test\
文件 60686 2017-12-25 11:29 wordToImages\Test\bin\test\commons-logging-1.1.1.jar
文件 1483772 2017-12-26 16:27 wordToImages\Test\bin\test\fontbox-2.0.8.jar
文件 49231 2017-12-26 16:27 wordToImages\Test\bin\test\jacob.jar
文件 2485070 2017-12-26 16:27 wordToImages\Test\bin\test\pdfbox-2.0.8.jar
文件 6658 2017-12-26 16:30 wordToImages\Test\bin\test\wordToImages.class
目录 0 2017-12-26 16:36 wordToImages\Test\src\
目录 0 2017-12-26 16:36 wordToImages\Test\src\test\
文件 60686 2017-12-25 11:29 wordToImages\Test\src\test\commons-logging-1.1.1.jar
文件 1483772 2017-12-26 16:27 wordToImages\Test\src\test\fontbox-2.0.8.jar
文件 49231 2017-12-26 16:27 wordToImages\Test\src\test\jacob.jar
文件 2485070 2017-12-26 16:27 wordToImages\Test\src\test\pdfbox-2.0.8.jar
文件 5300 2017-12-26 16:30 wordToImages\Test\src\test\wordToImages.java
文件 5278 2017-12-26 16:19 wordToImages\wordToImages.java
............此处省略1个文件信息
- 上一篇:大鱼吃小鱼游戏java源码
- 下一篇:Android天气预报源码
评论
共有 条评论