java实现word,ppt,excel,jpg转pdf
word,excel,jpeg 转 pdf
首先下载相关jar包:http://download.csdn.net/detail/xu281828044/6922499
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfWriter; public class Word2Pdf { static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
static final int wdFormatPDF = 17;// word转PDF 格式
static final int ppSaveAsPDF = 32;// ppt 转PDF 格式 public static void main(String[] args) throws IOException {
String source1 = "e:\\test.doc";
String source2 = "e:\\asd.xlsx";
String source3 = "e:\\aa.ppt";
String target1 = "e:\\test1.pdf";
String target2 = "e:\\test2.pdf";
String target3 = "e:\\test3.pdf"; Word2Pdf pdf = new Word2Pdf();
// pdf.word2pdf(source1, target1);
// pdf.excel2pdf(source2, target2);
// pdf.ppt2pdf(source3, target3);
// pdf.imgToPdf("e:/12345.jpg","e:/xu.pdf");
} public void word2pdf(String source,String target){
System.out.println("启动Word");
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,// FileName
false,// ConfirmConversions
true // ReadOnly
).toDispatch(); System.out.println("转换文档到PDF " + target);
File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(doc,//
"SaveAs", //
target, // FileName
wdFormatPDF); Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
if (app != null)
app.invoke("Quit", wdDoNotSaveChanges);
}
} public void ppt2pdf(String source,String target){
System.out.println("启动PPT");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Powerpoint.Application");
Dispatch presentations = app.getProperty("Presentations").toDispatch();
System.out.println("打开文档" + source);
Dispatch presentation = Dispatch.call(presentations,//
"Open",
source,// FileName
true,// ReadOnly
true,// Untitled 指定文件是否有标题。
false // WithWindow 指定文件是否可见。
).toDispatch(); System.out.println("转换文档到PDF " + target);
File tofile = new File(target);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(presentation,//
"SaveAs", //
target, // FileName
ppSaveAsPDF); Dispatch.call(presentation, "Close");
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
} finally {
if (app != null) app.invoke("Quit");
}
} public void excel2pdf(String source, String target) {
System.out.println("启动Excel");
long start = System.currentTimeMillis();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel(Excel.Application)
try {
app.setProperty("Visible", false);
Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
System.out.println("打开文档" + source);
Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]{source, new Variant(false),new Variant(false)}, new int[3]).toDispatch();
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] {
target, new Variant(57), new Variant(false),
new Variant(57), new Variant(57), new Variant(false),
new Variant(true), new Variant(57), new Variant(true),
new Variant(true), new Variant(true) }, new int[1]);
Variant f = new Variant(false);
System.out.println("转换文档到PDF " + target);
Dispatch.call(workbook, "Close", f);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
}finally {
if (app != null){
app.invoke("Quit", new Variant[] {});
}
}
} public boolean imgToPdf(String imgFilePath, String pdfFilePath)throws IOException {
File file=new File(imgFilePath);
if(file.exists()){
Document document = new Document();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(pdfFilePath);
PdfWriter.getInstance(document, fos); // 添加PDF文档的某些信息,比如作者,主题等等
document.addAuthor("arui");
document.addSubject("test pdf.");
// 设置文档的大小
document.setPageSize(PageSize.A4);
// 打开文档
document.open();
// 写入一段文字
//document.add(new Paragraph("JUST TEST ..."));
// 读取一个图片
Image image = Image.getInstance(imgFilePath);
float imageHeight=image.getScaledHeight();
float imageWidth=image.getScaledWidth();
int i=0;
while(imageHeight>500||imageWidth>500){
image.scalePercent(100-i);
i++;
imageHeight=image.getScaledHeight();
imageWidth=image.getScaledWidth();
System.out.println("imageHeight->"+imageHeight);
System.out.println("imageWidth->"+imageWidth);
} image.setAlignment(Image.ALIGN_CENTER);
// //设置图片的绝对位置
// image.setAbsolutePosition(0, 0);
// image.scaleAbsolute(500, 400);
// 插入一个图片
document.add(image);
} catch (DocumentException de) {
System.out.println(de.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
document.close();
fos.flush();
fos.close();
return true;
}else{
return false;
}
}
}
另存为哪种类型是由new variant()里面的参数决定的。
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile, new Variant(WORD_HTML) }, new int[1]);
new Variant(),这里面的根据传入的参数不同,可以另存为不同的类型,但是在网上搜索了一个并没有找到有关这个参数类型的一个说明,自己尝试了一下,结果如下:
|
0 |
Doc |
|
1 |
Dot |
|
2-5 |
Txt |
|
6 |
Rtf |
|
7 |
Txt |
|
8、10 |
htm |
|
11 |
Xml |
|
12、16 |
Docx |
|
13 |
Docm |
|
14 |
Dotx |
|
15 |
Dotm |
|
17 |
|
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.jacob.activeX.ActiveXComponent;
- import com.jacob.com.Dispatch;
- import com.jacob.com.Variant;
- import com.lowagie.text.Document;
- import com.lowagie.text.DocumentException;
- import com.lowagie.text.Image;
- import com.lowagie.text.PageSize;
- import com.lowagie.text.pdf.PdfWriter;
- public class Word2Pdf {
- static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
- static final int wdFormatPDF = 17;// word转PDF 格式
- static final int ppSaveAsPDF = 32;// ppt 转PDF 格式
- public static void main(String[] args) throws IOException {
- String source1 = "e:\\test.doc";
- String source2 = "e:\\asd.xlsx";
- String source3 = "e:\\aa.ppt";
- String target1 = "e:\\test1.pdf";
- String target2 = "e:\\test2.pdf";
- String target3 = "e:\\test3.pdf";
- Word2Pdf pdf = new Word2Pdf();
- // pdf.word2pdf(source1, target1);
- // pdf.excel2pdf(source2, target2);
- // pdf.ppt2pdf(source3, target3);
- // pdf.imgToPdf("e:/12345.jpg","e:/xu.pdf");
- }
- public void word2pdf(String source,String target){
- System.out.println("启动Word");
- 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,// FileName
- false,// ConfirmConversions
- true // ReadOnly
- ).toDispatch();
- System.out.println("转换文档到PDF " + target);
- File tofile = new File(target);
- if (tofile.exists()) {
- tofile.delete();
- }
- Dispatch.call(doc,//
- "SaveAs", //
- target, // FileName
- wdFormatPDF);
- Dispatch.call(doc, "Close", false);
- long end = System.currentTimeMillis();
- System.out.println("转换完成..用时:" + (end - start) + "ms.");
- } catch (Exception e) {
- System.out.println("========Error:文档转换失败:" + e.getMessage());
- } finally {
- if (app != null)
- app.invoke("Quit", wdDoNotSaveChanges);
- }
- }
- public void ppt2pdf(String source,String target){
- System.out.println("启动PPT");
- long start = System.currentTimeMillis();
- ActiveXComponent app = null;
- try {
- app = new ActiveXComponent("Powerpoint.Application");
- Dispatch presentations = app.getProperty("Presentations").toDispatch();
- System.out.println("打开文档" + source);
- Dispatch presentation = Dispatch.call(presentations,//
- "Open",
- source,// FileName
- true,// ReadOnly
- true,// Untitled 指定文件是否有标题。
- false // WithWindow 指定文件是否可见。
- ).toDispatch();
- System.out.println("转换文档到PDF " + target);
- File tofile = new File(target);
- if (tofile.exists()) {
- tofile.delete();
- }
- Dispatch.call(presentation,//
- "SaveAs", //
- target, // FileName
- ppSaveAsPDF);
- Dispatch.call(presentation, "Close");
- long end = System.currentTimeMillis();
- System.out.println("转换完成..用时:" + (end - start) + "ms.");
- } catch (Exception e) {
- System.out.println("========Error:文档转换失败:" + e.getMessage());
- } finally {
- if (app != null) app.invoke("Quit");
- }
- }
- public void excel2pdf(String source, String target) {
- System.out.println("启动Excel");
- long start = System.currentTimeMillis();
- ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel(Excel.Application)
- try {
- app.setProperty("Visible", false);
- Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
- System.out.println("打开文档" + source);
- Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]{source, new Variant(false),new Variant(false)}, new int[3]).toDispatch();
- Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] {
- target, new Variant(57), new Variant(false),
- new Variant(57), new Variant(57), new Variant(false),
- new Variant(true), new Variant(57), new Variant(true),
- new Variant(true), new Variant(true) }, new int[1]);
- Variant f = new Variant(false);
- System.out.println("转换文档到PDF " + target);
- Dispatch.call(workbook, "Close", f);
- long end = System.currentTimeMillis();
- System.out.println("转换完成..用时:" + (end - start) + "ms.");
- } catch (Exception e) {
- System.out.println("========Error:文档转换失败:" + e.getMessage());
- }finally {
- if (app != null){
- app.invoke("Quit", new Variant[] {});
- }
- }
- }
- public boolean imgToPdf(String imgFilePath, String pdfFilePath)throws IOException {
- File file=new File(imgFilePath);
- if(file.exists()){
- Document document = new Document();
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(pdfFilePath);
- PdfWriter.getInstance(document, fos);
- // 添加PDF文档的某些信息,比如作者,主题等等
- document.addAuthor("arui");
- document.addSubject("test pdf.");
- // 设置文档的大小
- document.setPageSize(PageSize.A4);
- // 打开文档
- document.open();
- // 写入一段文字
- //document.add(new Paragraph("JUST TEST ..."));
- // 读取一个图片
- Image image = Image.getInstance(imgFilePath);
- float imageHeight=image.getScaledHeight();
- float imageWidth=image.getScaledWidth();
- int i=0;
- while(imageHeight>500||imageWidth>500){
- image.scalePercent(100-i);
- i++;
- imageHeight=image.getScaledHeight();
- imageWidth=image.getScaledWidth();
- System.out.println("imageHeight->"+imageHeight);
- System.out.println("imageWidth->"+imageWidth);
- }
- image.setAlignment(Image.ALIGN_CENTER);
- // //设置图片的绝对位置
- // image.setAbsolutePosition(0, 0);
- // image.scaleAbsolute(500, 400);
- // 插入一个图片
- document.add(image);
- } catch (DocumentException de) {
- System.out.println(de.getMessage());
- } catch (IOException ioe) {
- System.out.println(ioe.getMessage());
- }
- document.close();
- fos.flush();
- fos.close();
- return true;
- }else{
- return false;
- }
- }
- }
java实现word,ppt,excel,jpg转pdf的更多相关文章
- java操作word,excel,pdf
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
- java操作office和pdf文件java读取word,excel和pdf文档内容
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
- Windows:Word,PPT,EXCEL com+组件配置
本文所涉及到配置前提: 服务器必须安装Office套件(Word,PPT,Excel) 第一部分 Word Com+组件权限配置 1.cmd模式输入dcomcnfg 2.找到Microsoft Wor ...
- word ppt excel文档转换成pdf
1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...
- java 实现Word或Excel 转Pdf
1:首先需要引入相关的jar word转pdf需要引入 aspose-words-15.8.0-jdk16.jar 下载JAR包 Word http://note.youdao.com/notesha ...
- .net 实现Office文件预览 Word PPT Excel 2015-01-23 08:47 63人阅读 评论(0) 收藏
先打个广告: .Net交流群:252713569 本人QQ :524808775 欢迎技术探讨, 近期公司要求上传的PPT和Word都需要可以在线预览.. 小弟我是从来没有接触过这一块的东西 感觉很棘 ...
- Java中Office(word/ppt/excel)转换成HTML实现
运行条件:JDK + jacob.jar + jacob.dll 1) 把jacob.dll在 JAVA_HOME\bin\ 和 JAVA_HOME\jre\bin\ 以及C:\WINDOWS\sys ...
- PHP 实现Word,excel等转换pdf
近期做一个项目,须要将用户上传的word,excel文档转成PDF文档保存并打印.在网上找了非常多资料.并不全面,所以自己写了一份比較全面的教程来分享. 以下是操作步骤: 1. 安装免费 ...
- 全套Office办公软件WORD/PPT/EXCEL视频教程 每日更新中
详情见Processon分享链接:https://www.processon.com/view/link/5b3f40abe4b09a67415e2bfc
随机推荐
- 使用JSP页面生成PDF报表
转自:http://developer.51cto.com/art/200907/134261.htm 1.iText简介 iText是一个开放源码的Java类库,可以用来方便地生成PDF文件.大家通 ...
- 1.JSONObject与JSONArray的使用
参考文献: http://blog.csdn.net/huangwuyi/article/details/5412500 1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib ...
- angular 使用服务共享数据需要注意
在使用服务共享数据时,需要注意一些细节,否则会出现视图不刷新,也不报错这样的问题,遇到了,总结下 如下: <div ng-controller='ctr1'> <a href={{n ...
- 机器学习入门-概率阈值的逻辑回归对准确度和召回率的影响 lr.predict_proba(获得预测样本的概率值)
1.lr.predict_proba(under_text_x) 获得的是正负的概率值 在sklearn逻辑回归的计算过程中,使用的是大于0.5的是正值,小于0.5的是负值,我们使用使用不同的概率结 ...
- ABAP-增强-MRP运行-根据工厂/父件/子件/供应商拆分采购申请
最近有个业务需要,MRP运行过程中需要根据生产计划订单/子件/供应商对应关系来拆解采购申请. 1.具体实例: a.基础数据 整车物料:NL1G58420151001219 子件:00000000888 ...
- PostgreSql别名区分大小写的问题
PostgreSql是区分大小写的,如果别名的大小不一致就会提示错误: SELECT * FROM ( SELECT cpi."product_item_id" "PRO ...
- java 元数据
什么是元数据? 元数据是指用来描述数据的数据,更通俗一点,就是描述代码间关系,或者代码与其他资源(例如数据库表)之间内在联系的数据.在一些技术框架,如struts.EJB.hibernate就不知不觉 ...
- 获取RequestMapping注解中的属性
参考:https://www.cnblogs.com/2013jiutian/p/7294053.html @RequestMapping("/value1") @Controll ...
- PAT L3-008 喊山(广搜)
喊山,是人双手围在嘴边成喇叭状,对着远方高山发出“喂—喂喂—喂喂喂……”的呼唤.呼唤声通过空气的传递,回荡于深谷之间,传送到人们耳中,发出约定俗成的“讯号”,达到声讯传递交流的目的.原来它是彝族先民用 ...
- 【校招面试 之 剑指offer】第9-2题 用两个队列实现一个栈
#include<iostream> #include<queue> using namespace std; // 对于出栈解决的思路是:将queue1的元素除了最后一个外全 ...