派生自 projectDept/qhighschool

胡仁荣
2022-10-31 fe7381d6e8ec1f427408de0297ac7f41533202f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.qxueyou.scc.base.util;
 
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
 
public class JacobUtil {  
    
    public static final String DOC = "doc";  
    public static final String DOCX = "docx";  
    public static final String PDF = "pdf";  
    public static final String XLS = "xls";  
    public static final String XLSX = "xlsx";  
    public static final String MP4 = "mp4";  
    public static final String PPT = "ppt";  
    public static final String PPTX = "pptx";  
   
     // 8 ´ú±íword±£´æ³Éhtml    
     public static final int WORD2HTML = 8;    
     // 17´ú±íword±£´æ³Épdf  
     public static final int WD2PDF = 17;  
     public static final int PPT2PDF = 32;  
     public static final int XLS2PDF = 0;  
   
 public static void main(String[] args) {     
     String pptfile = "D:/upload/ÐÂÔ±¹¤ÈëÖ°Ïòµ¼¼°ÐÅÏ¢201605.xlsx";    
     //String pdffile = "D:/upload/b.pdf";    
     String htmlfile="D:/upload/excel.pdf";
     //ppt2pdf(pptfile,pdffile);    
     excel2pdf(pptfile,htmlfile);
 }    
     
/** 
  * @param resourceType ×ÊÔ´ÀàÐÍ 
  * @param path ×ÊԴ·¾¶ 
  * @return 
  * TODO Îļþת»» 
  */  
 public static Integer formatConvert(String resourceType, String resourcePath) {  
     Integer pages = 0;  
     String resource = resourcePath.substring(0, resourcePath.lastIndexOf("."));  
     if(resourceType.equalsIgnoreCase(DOC)||resourceType.equalsIgnoreCase(DOCX)){  
         //wordת³ÉpdfºÍͼ??  
         word2pdf(resourcePath, resource+".pdf");  
     }else if(resourceType.equalsIgnoreCase(XLS)||resourceType.equalsIgnoreCase(XLSX)){  
         //excelÎļþת³ÉͼƬ  
         excel2pdf(resourcePath, resource+".pdf");  
     }else if(resourceType.equalsIgnoreCase(PPT)||resourceType.equalsIgnoreCase(PPTX)){  
         ppt2pdf(resourcePath, resource+".pdf");  
     }else if(resourceType.equalsIgnoreCase(MP4)){  
         //ÊÓÆµÎļþ²»×ª??  
         pages = 0;  
     }  
     return pages;  
 }  
     
   
 public static void word2pdf(String docfile, String pdffile)       
 {       
     // Æô¶¯wordÓ¦ÓóÌÐò(Microsoft Office Word 2003)    
     ActiveXComponent app = null;    
     try{  
         ComThread.InitSTA();  
         app = new ActiveXComponent("Word.Application");       
         app.setProperty("Visible", false);  
         System.out.println("*****ÕýÔÚת»»...*****");    
         // ÉèÖÃwordÓ¦ÓóÌÐò²»¿É??  
        // app.setProperty("Visible", new Variant(false));      
         // documents±íʾword³ÌÐòµÄËùÓÐÎĵµ´°¿Ú£¬£¨wordÊǶàÎĵµÓ¦ÓóÌÐò???   
         Dispatch docs = app.getProperty("Documents").toDispatch();      
         // ´ò¿ªÒª×ª»»µÄwordÎļþ    
        /* Dispatch doc = Dispatch.invoke(      
                 docs,      
                 "Open",      
                 Dispatch.Method,      
                 new Object[] { docfile, new Variant(false),    
                         new Variant(true) }, new int[1]).toDispatch(); */  
           
         Dispatch doc = Dispatch.call(  
                 docs,       
                 "Open",  
                 docfile,  
                 false,  
                 true).toDispatch();       
         // µ÷ÓÃDocument¶ÔÏóµÄsaveAs·½·¨,½«Îĵµ±£´æÎªpdf¸ñʽ  
         /*Dispatch.invoke(doc, "ExportAsFixedFormat", Dispatch.Method, new Object[] {      
                 pdffile, new Variant(wdFormatPDF) }, new int[1]);*/    
           
         Dispatch.call(doc, "ExportAsFixedFormat", pdffile, WD2PDF);    
         // ¹Ø±ÕwordÎļþ    
         Dispatch.call(doc, "Close", false);   
     }       
     catch (Exception e)       
     {   ComThread.Release();    
         e.printStackTrace();      
     }       
     finally      
     {       
         //¹Ø±ÕwordÓ¦ÓóÌÐò    
         app.invoke("Quit", 0);     
         ComThread.Release();  
     }     
     System.out.println("*****ת»»Íê±Ï********");    
 }  
      
   
   
 public static void ppt2pdf(String pptfile,String pdffile){  
     ActiveXComponent app = null;  
     //app.setProperty("Visible", new Variant(false));  
     try {  
         ComThread.InitSTA();  
         app = new ActiveXComponent("PowerPoint.Application");  
         Dispatch files = app.getProperty("Presentations").toDispatch();  
         Dispatch file = Dispatch.call(files, "open", pptfile, false, true).toDispatch();  
         Dispatch.call(file, "SaveAs", pdffile, PPT2PDF);  
         Dispatch.call(file,"Close");  
     } catch (Exception e) {  
         ComThread.Release();  
         e.printStackTrace();  
         //throw e;  
     }finally{  
         app.invoke("Quit");  
         ComThread.Release();  
     }  
 }  
   
 public static void excel2pdf(String excelfile,String pdffile){  
     ActiveXComponent app = null;  
     try{  
         ComThread.InitSTA(true);  
         app = new ActiveXComponent("Excel.Application");  
         app.setProperty("Visible", false);  
         app.setProperty("AutomationSecurity", new Variant(3));//½ûÓÃ??  
         Dispatch excels = app.getProperty("Workbooks").toDispatch();  
         /*Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method, new Object[]{ 
                 excelfile, 
                 new Variant(false), 
                 new Variant(false), 
         },new int[9]).toDispatch();*/  
         Dispatch excel = Dispatch.call(excels, "Open",   
                 excelfile,false,true).toDispatch();  
         //ת»»¸ñʽExportAsFixedFormat  
         /*Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[]{ 
                 new Variant(0),//pdf¸ñʽ=0 
                 pdffile, 
                 new Variant(0)//0=±ê×¼(Éú³ÉµÄpdfͼƬ²»»á±äÄ£??) 1=??СÎÄ??(Éú³ÉµÄpdfͼƬģºýµÄÒ»Ëúºý??) 
         }, new int[1]);*/  
         Dispatch.call(excel, "ExportAsFixedFormat",XLS2PDF,  
                 pdffile);  
         Dispatch.call(excel, "Close", false);  
         if(app!=null){  
             app.invoke("Quit");  
             app=null;  
         }  
     }catch(Exception e){  
         ComThread.Release();  
         e.printStackTrace();  
     }finally{  
         ComThread.Release();  
     }  
 }  
 
}