jacob将word,excel,ppt转换为PDF格式

使用的一些心得吧

jacob的底层调用的其实是office的驱动,由于linux上肯定没有word,所以使用jacob对文档进行处理的话,肯定就不能部署在linux服务器上了,只有在windows服务器上。

也正是因为这个原因,jacob对于word或者excel的操作,就相当于我们打开word或者excel文件进行操作,从道理上来说,能用word处理的,jacob都可以,诸如转换为Pdf,插入图片,编辑文档,另存为,设置文档的页面大小,页边距,excel列缩放为一页转换为pdf。

可以参考微软的开发文档,设置属性

https://docs.microsoft.com/zh-cn/office/vba/api/word.pagesetup

 

首先需要在项目中引用jacob的jar包,可以自己去网上下载jacob_1.18.jar

也可以在maven中直接加入

我的maven配置了阿里仓库的镜像,找了一个版本的jacob,直接加在pom里就可以了,如下

     <dependency>
            <groupId>com.hynnet</groupId>
            <artifactId>jacob</artifactId>
            <version>1.18</version>
    </dependency>

然后,除了jacob之外,还需要在%JAVA_HOME%/jdk/jre/bin中加入dll文件

jacob-1.18-x64.dll

或者32位系统

jacob-1.18-x86.dll

然后我们就可以使用jacob来将我们的excel,或者word文件,ppt文件转换为pdf,我在使用excel转换为pdf时,发现列数太长,转换为pdf时,会自动把多的列放到下一页中,样式混乱,找了好久,才发现,可以选择将多列显示为一页的方法,也写在了下面的方法中。

package com.cn.uk.common.utils;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordOrExcelToPdf {
	 private static final Integer WORD_TO_PDF_OPERAND = 17;
	 private static final Integer PPT_TO_PDF_OPERAND = 32;
	 private static final Integer EXCEL_TO_PDF_OPERAND = 0;
	 private static final Integer WD_FORMAT_DOCUMENT = 0;
	 
	 public static void docSave(String filePath) {
	       ActiveXComponent app = null;  
	        Dispatch doc = null;
	        try {  
	            ComThread.InitSTA();  
	            app = new ActiveXComponent("Word.Application");  
	            app.setProperty("Visible", false);  
	            Dispatch docs = app.getProperty("Documents").toDispatch(); 
	            Object[] obj = new Object[]{
	            		filePath, 
	                    new Variant(false),  
	                    new Variant(false),//是否只读  
	                    new Variant(false),   
	                    new Variant("pwd")
	            };
	            doc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch(); 
	            Dispatch.put(doc, "RemovePersonalInformation", false);  
	            Dispatch.call(doc, "Save"); //word保存 
	     /*       Dispatch.call(doc, "SaveAs","path",WD_FORMAT_DOCUMENT);*/
	        }catch (Exception e) {  
	            e.printStackTrace();
	            throw e;
	        } finally {  
	            if (doc != null) {  
	                Dispatch.call(doc, "Close", false);  
	            }  
	            if (app != null) {  
	                app.invoke("Quit", 0);  
	            }  
	            ComThread.Release();  
	        }  
	 }
	 //word转换为pdf
	    public static void doc2pdf(String srcFilePath, String pdfFilePath) throws Exception {  
	        ActiveXComponent app = null;  
	        Dispatch doc = null;
	        Dispatch pageSetup=null;
	        try {  
	            ComThread.InitSTA();  
	            app = new ActiveXComponent("Word.Application");  
	            app.setProperty("Visible", false);  
	            Dispatch docs = app.getProperty("Documents").toDispatch(); 
	            Object[] obj = new Object[]{
	                    srcFilePath, 
	                    new Variant(false),  
	                    new Variant(false),//是否只读  
	                    new Variant(false),   
	                    new Variant("pwd")
	            };
	            doc = Dispatch.invoke(docs, "Open", Dispatch.Method, obj, new int[1]).toDispatch(); 
	            //设置纸张宽度,高度,PaperSize,PageHeight
	            pageSetup = Dispatch.get(doc, "PageSetup").toDispatch();   
//	          Dispatch.put(doc, "Compatibility", false);  //兼容性检查,为特定值false不正确  
	            Dispatch.put(pageSetup, "PageWidth","50");
	            Dispatch.put(doc, "RemovePersonalInformation", false);  
	            Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, WORD_TO_PDF_OPERAND); // word保存为pdf格式宏,值为17  
	            
	  
	        }catch (Exception e) {  
	            e.printStackTrace();
	            throw e;
	        } finally {  
	            if (doc != null) {  
	                Dispatch.call(doc, "Close", false);  
	            }  
	            if (app != null) {  
	                app.invoke("Quit", 0);  
	            }  
	            ComThread.Release();  
	        }  
	    }  
	    
	    //ppt转换为pdf
	    public static void ppt2pdf(String srcFilePath, String pdfFilePath) throws Exception {
	        ActiveXComponent app = null;
	        Dispatch ppt = null;
	        try {
	            ComThread.InitSTA();
	            app = new ActiveXComponent("PowerPoint.Application");
	            Dispatch ppts = app.getProperty("Presentations").toDispatch();

	            /*
	             * call 
	             * param 4: ReadOnly
	             * param 5: Untitled指定文件是否有标题
	             * param 6: WithWindow指定文件是否可见
	             * */
	            ppt = Dispatch.call(ppts, "Open", srcFilePath, true,true, false).toDispatch();
	            Dispatch.call(ppt, "SaveAs", pdfFilePath, PPT_TO_PDF_OPERAND); // ppSaveAsPDF为特定值32

	        } catch (Exception e) {
	            e.printStackTrace();
	            throw e;
	        } finally {
	            if (ppt != null) {
	                Dispatch.call(ppt, "Close");
	            }
	            if (app != null) {
	                app.invoke("Quit");
	            }
	            ComThread.Release();
	        }
	    }
	    
	    //excel转换为pdf
	    public static void excel2Pdf(String inFilePath, String outFilePath) throws Exception {
	    	ComThread.InitSTA(true);
	    	ActiveXComponent ax=new ActiveXComponent("Excel.Application");
	    	try{
	    		ax.setProperty("Visible", new Variant(false));
	    		ax.setProperty("AutomationSecurity", new Variant(3)); //禁用宏
	    		Dispatch excels=ax.getProperty("Workbooks").toDispatch();
	    		
	    		Dispatch excel=Dispatch.invoke(excels,"Open",Dispatch.Method,new Object[]{
	    			inFilePath,
	    			new Variant(false),
	    			new Variant(false)
	    		},
	    		new int[9]).toDispatch();
		   	      
	    		//将所有列缩放为一页
		    		Dispatch currentSheet = Dispatch.get((Dispatch) excel,
		                    "ActiveSheet").toDispatch();
		            Dispatch pageSetup = Dispatch.get(currentSheet, "PageSetup")
		                    .toDispatch();
		            Dispatch.put(pageSetup, "Orientation", new Variant(2));
		            Dispatch.put(pageSetup, "Zoom",false); 
		          /*  Dispatch.put(pageSetup, "StandardHeight",18); */
		            Dispatch.put(pageSetup, "FitToPagesWide",1); // 所有列为一页(1或false)
		            Dispatch.put(pageSetup, "FitToPagesTall",false);//行不缩放为一页StandardHeight
		            
	    		//转换格式
	    		Dispatch.invoke(currentSheet,"ExportAsFixedFormat",Dispatch.Method,new Object[]{
	    			new Variant(0), //PDF格式=0
	    			outFilePath,
	    			new Variant(0)  //0=标准 (生成的PDF图片不会变模糊) 1=最小文件 (生成的PDF图片糊的一塌糊涂)
	    		},new int[1]);
	    		//这里放弃使用SaveAs
	    		/*Dispatch.invoke(excel,"SaveAs",Dispatch.Method,new Object[]{
	    			outFile,
	    			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]);*/
    		Dispatch.call(excel, "Close",new Variant(false));
	     
	    		if(ax!=null){
	    			ax.invoke("Quit",new Variant[]{});
	    			ax=null;
	    		}
	    		ComThread.Release();
	    		ComThread.quitMainSTA();
	    	}catch(Exception es){
	    		es.printStackTrace();
	    	}

	    }
	    
	    public static void main(String[] args) throws Exception{
	/*    	WordOrExcelToPdf.docSave("D:\\home\\20190911143555_测试任务1_abnormal.doc");*/
			WordOrExcelToPdf.doc2pdf("D:\\home\\20190911143555_测试任务1_abnormal.doc", "D:\\home\\20190911143555_测试任务1_abnormal.pdf");
		
	    }
}

另外在文章的最后附上jacob的jar包和dll文件的下载。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值