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

Pdf

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