ITeye博客

java实现word,ppt,excel,jpg转pdf

110shen110 · 2017-06-24 22:43 · java · office · web · 编程语言 · Java · Excel · PPT · 浏览 · Word · Markdown

word,excel,jpeg 转 pdf

首先下载相关jar包:http://download.csdn.net/detail/xu281828044/6922499

 

 

[java] view plain copy

 

  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. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。