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

Pdf

  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import com.jacob.activeX.ActiveXComponent;
  5. import com.jacob.com.Dispatch;
  6. import com.jacob.com.Variant;
  7. import com.lowagie.text.Document;
  8. import com.lowagie.text.DocumentException;
  9. import com.lowagie.text.Image;
  10. import com.lowagie.text.PageSize;
  11. import com.lowagie.text.pdf.PdfWriter;
  12. public class Word2Pdf {
  13. static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
  14. static final int wdFormatPDF = 17;// word转PDF 格式
  15. static final int ppSaveAsPDF = 32;// ppt 转PDF 格式
  16. public static void main(String[] args) throws IOException {
  17. String source1 = "e:\\test.doc";
  18. String source2 = "e:\\asd.xlsx";
  19. String source3 = "e:\\aa.ppt";
  20. String target1 = "e:\\test1.pdf";
  21. String target2 = "e:\\test2.pdf";
  22. String target3 = "e:\\test3.pdf";
  23. Word2Pdf pdf = new Word2Pdf();
  24. //  pdf.word2pdf(source1, target1);
  25. //  pdf.excel2pdf(source2, target2);
  26. //  pdf.ppt2pdf(source3, target3);
  27. //  pdf.imgToPdf("e:/12345.jpg","e:/xu.pdf");
  28. }
  29. public void word2pdf(String source,String target){
  30. System.out.println("启动Word");
  31. long start = System.currentTimeMillis();
  32. ActiveXComponent app = null;
  33. try {
  34. app = new ActiveXComponent("Word.Application");
  35. app.setProperty("Visible", false);
  36. Dispatch docs = app.getProperty("Documents").toDispatch();
  37. System.out.println("打开文档" + source);
  38. Dispatch doc = Dispatch.call(docs,//
  39. "Open", //
  40. source,// FileName
  41. false,// ConfirmConversions
  42. true // ReadOnly
  43. ).toDispatch();
  44. System.out.println("转换文档到PDF " + target);
  45. File tofile = new File(target);
  46. if (tofile.exists()) {
  47. tofile.delete();
  48. }
  49. Dispatch.call(doc,//
  50. "SaveAs", //
  51. target, // FileName
  52. wdFormatPDF);
  53. Dispatch.call(doc, "Close", false);
  54. long end = System.currentTimeMillis();
  55. System.out.println("转换完成..用时:" + (end - start) + "ms.");
  56. } catch (Exception e) {
  57. System.out.println("========Error:文档转换失败:" + e.getMessage());
  58. } finally {
  59. if (app != null)
  60. app.invoke("Quit", wdDoNotSaveChanges);
  61. }
  62. }
  63. public void ppt2pdf(String source,String target){
  64. System.out.println("启动PPT");
  65. long start = System.currentTimeMillis();
  66. ActiveXComponent app = null;
  67. try {
  68. app = new ActiveXComponent("Powerpoint.Application");
  69. Dispatch presentations = app.getProperty("Presentations").toDispatch();
  70. System.out.println("打开文档" + source);
  71. Dispatch presentation = Dispatch.call(presentations,//
  72. "Open",
  73. source,// FileName
  74. true,// ReadOnly
  75. true,// Untitled 指定文件是否有标题。
  76. false // WithWindow 指定文件是否可见。
  77. ).toDispatch();
  78. System.out.println("转换文档到PDF " + target);
  79. File tofile = new File(target);
  80. if (tofile.exists()) {
  81. tofile.delete();
  82. }
  83. Dispatch.call(presentation,//
  84. "SaveAs", //
  85. target, // FileName
  86. ppSaveAsPDF);
  87. Dispatch.call(presentation, "Close");
  88. long end = System.currentTimeMillis();
  89. System.out.println("转换完成..用时:" + (end - start) + "ms.");
  90. } catch (Exception e) {
  91. System.out.println("========Error:文档转换失败:" + e.getMessage());
  92. } finally {
  93. if (app != null) app.invoke("Quit");
  94. }
  95. }
  96. public void excel2pdf(String source, String target) {
  97. System.out.println("启动Excel");
  98. long start = System.currentTimeMillis();
  99. ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel(Excel.Application)
  100. try {
  101. app.setProperty("Visible", false);
  102. Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
  103. System.out.println("打开文档" + source);
  104. Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]{source, new Variant(false),new Variant(false)}, new int[3]).toDispatch();
  105. Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] {
  106. target, new Variant(57), new Variant(false),
  107. new Variant(57), new Variant(57), new Variant(false),
  108. new Variant(true), new Variant(57), new Variant(true),
  109. new Variant(true), new Variant(true) }, new int[1]);
  110. Variant f = new Variant(false);
  111. System.out.println("转换文档到PDF " + target);
  112. Dispatch.call(workbook, "Close", f);
  113. long end = System.currentTimeMillis();
  114. System.out.println("转换完成..用时:" + (end - start) + "ms.");
  115. } catch (Exception e) {
  116. System.out.println("========Error:文档转换失败:" + e.getMessage());
  117. }finally {
  118. if (app != null){
  119. app.invoke("Quit", new Variant[] {});
  120. }
  121. }
  122. }
  123. public boolean imgToPdf(String imgFilePath, String pdfFilePath)throws IOException {
  124. File file=new File(imgFilePath);
  125. if(file.exists()){
  126. Document document = new Document();
  127. FileOutputStream fos = null;
  128. try {
  129. fos = new FileOutputStream(pdfFilePath);
  130. PdfWriter.getInstance(document, fos);
  131. // 添加PDF文档的某些信息,比如作者,主题等等
  132. document.addAuthor("arui");
  133. document.addSubject("test pdf.");
  134. // 设置文档的大小
  135. document.setPageSize(PageSize.A4);
  136. // 打开文档
  137. document.open();
  138. // 写入一段文字
  139. //document.add(new Paragraph("JUST TEST ..."));
  140. // 读取一个图片
  141. Image image = Image.getInstance(imgFilePath);
  142. float imageHeight=image.getScaledHeight();
  143. float imageWidth=image.getScaledWidth();
  144. int i=0;
  145. while(imageHeight>500||imageWidth>500){
  146. image.scalePercent(100-i);
  147. i++;
  148. imageHeight=image.getScaledHeight();
  149. imageWidth=image.getScaledWidth();
  150. System.out.println("imageHeight->"+imageHeight);
  151. System.out.println("imageWidth->"+imageWidth);
  152. }
  153. image.setAlignment(Image.ALIGN_CENTER);
  154. //        //设置图片的绝对位置
  155. // image.setAbsolutePosition(0, 0);
  156. // image.scaleAbsolute(500, 400);
  157. // 插入一个图片
  158. document.add(image);
  159. } catch (DocumentException de) {
  160. System.out.println(de.getMessage());
  161. } catch (IOException ioe) {
  162. System.out.println(ioe.getMessage());
  163. }
  164. document.close();
  165. fos.flush();
  166. fos.close();
  167. return true;
  168. }else{
  169. return false;
  170. }
  171. }
  172. }

java实现word,ppt,excel,jpg转pdf的更多相关文章

  1. java操作word,excel,pdf

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  2. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  3. Windows:Word,PPT,EXCEL com+组件配置

    本文所涉及到配置前提: 服务器必须安装Office套件(Word,PPT,Excel) 第一部分 Word Com+组件权限配置 1.cmd模式输入dcomcnfg 2.找到Microsoft Wor ...

  4. word ppt excel文档转换成pdf

    1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...

  5. java 实现Word或Excel 转Pdf

    1:首先需要引入相关的jar word转pdf需要引入 aspose-words-15.8.0-jdk16.jar 下载JAR包 Word http://note.youdao.com/notesha ...

  6. .net 实现Office文件预览 Word PPT Excel 2015-01-23 08:47 63人阅读 评论(0) 收藏

    先打个广告: .Net交流群:252713569 本人QQ :524808775 欢迎技术探讨, 近期公司要求上传的PPT和Word都需要可以在线预览.. 小弟我是从来没有接触过这一块的东西 感觉很棘 ...

  7. Java中Office(word/ppt/excel)转换成HTML实现

    运行条件:JDK + jacob.jar + jacob.dll 1) 把jacob.dll在 JAVA_HOME\bin\ 和 JAVA_HOME\jre\bin\ 以及C:\WINDOWS\sys ...

  8. PHP 实现Word,excel等转换pdf

    近期做一个项目,须要将用户上传的word,excel文档转成PDF文档保存并打印.在网上找了非常多资料.并不全面,所以自己写了一份比較全面的教程来分享. 以下是操作步骤: 1.        安装免费 ...

  9. 全套Office办公软件WORD/PPT/EXCEL视频教程 每日更新中

    详情见Processon分享链接:https://www.processon.com/view/link/5b3f40abe4b09a67415e2bfc

随机推荐

  1. Git 代码版本还原方法

    因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,Git 代码版本还原方法 在使用 Git 管理自己的代码和资料时,难免会遇到意料 ...

  2. Linux开机挂载windows共享文件夹

    https://blog.csdn.net/zhaogang1993/article/details/79573271  (可行) 命令: mount -t cifs -o username=&quo ...

  3. delphi ios grid BindSourceDB bug

    BindSourceDB4.DataSet :=nil; BindSourceDB4.DataSet :=FDMemTable1; grid绑定后显示数据正常,第二次赋值BindSourceDB4.D ...

  4. C++实现ping功能<转>

    今天接到需求要实现ping的功能,然后网上查了一些资料,对网络编程的一些函数熟悉了一下,虽然还有一些细节不清楚,但是慢慢积累. 要实现这样的功能: 基础知识 ping的过程是向目的IP发送一个type ...

  5. windows7安装svn客户端

    全部选择默认的即可, 上面的这种检出方式会报错,要使用下面这种检出方式

  6. 再识ASCII实体、符号实体和字符实体

    一.前言            相信大家都熟悉通过字符实体   来实现多个连续空格的输入吧!本文打算对三类HTML实体及JS相关操作作进一步的整理和小结,若有纰漏请大家指正,谢谢. 二.初识HTML实 ...

  7. 框架中spring有专门的类用于处理乱码

    在三大框架spring中有专门的一个过滤器类用于处理乱码问题--->CharacterEncodingFilter 我们只需在web.xml中添加如下几行代码就可解决乱码问题 <filte ...

  8. TensorFlow RNN MNIST字符识别演示快速了解TF RNN核心框架

    TensorFlow RNN MNIST字符识别演示快速了解TF RNN核心框架 http://blog.sina.com.cn/s/blog_4b0020f30102wv4l.html

  9. hive sql split 分隔符

    Hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...

  10. Ansible介绍/安装/入门

    http://docs.ansible.com/ansible/ https://galaxy.ansible.com/ Ansible是一个IT自动化工具. 它可以配置系统,部署软件,并编排更先进的 ...