ppt增長水印

使用jacob,引入jacob.jar,將jacob-1.18-x64.dll放入system32java

 

裏面有兩個關鍵類ActiveXComponent和Dispatch,ActiveXComponent能夠理解爲window中的一個程序,例如 word ppt ...,Dispatch能夠表明其中的一個對象.web

關鍵語句apache

//啓動ppt程序
ActiveXComponent ppt=null;  			      
ppt = new ActiveXComponent("PowerPoint.Application");
Dispatch pptDocument = ppt.getProperty("Presentations").toDispatch();
//打開文檔
Dispatch curDocument =Dispatch.call(pptDocument, "Open", filePath).toDispatch();

getProperty獲取屬性,call執行方法具體代碼以下ide

package cn.com.do1.utils;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.SafeArray;
import com.jacob.com.Variant;

/**
 * ppt 工具類
 * @author ydy
 * */
public class PptUtil {
	private static Logger logger=Logger.getLogger(PptUtil.class);
	//臨時目錄
	private static final String TEMP_FILE_PATH;
	static {
		String catalinaHome=System.getProperty("catalina.home");
		if(StringUtils.isBlank(catalinaHome)) {
			TEMP_FILE_PATH=PptUtil.class.getResource("/").getPath()+File.separator+"temp";
			
		}else {//web環境
			TEMP_FILE_PATH=catalinaHome+File.separator+"temp";
		}
		File fileTemp=new File(TEMP_FILE_PATH);
		if(fileTemp.exists()) {
			
		}else {
			fileTemp.mkdirs();
		}
		
	}
	/**
	 * ppt添加水印
	 * @param is 輸入流
	 * @param os 輸出流
	 * @param waterContent 水印內容
	 * @param officeType 文檔類型
	 * 
	 * */
	public static  boolean addWater(InputStream is,OutputStream  os,String waterContent,OfficeType officeType) {
		try {
			//生成文件
			String filePath =TEMP_FILE_PATH+File.separator+officeType.getName();
			File fileFrom=new File(filePath);
			OutputStream osFileFrom=FileUtils.openOutputStream(fileFrom);
			IOUtils.copy(is, osFileFrom);
			is.close();
			
			//初始化com的線程
			ComThread.InitSTA();
			//ppt程序
			ActiveXComponent ppt=null;  			      
			ppt = new ActiveXComponent("PowerPoint.Application");
			Dispatch pptDocument = ppt.getProperty("Presentations").toDispatch();
			//打開文檔
			Dispatch curDocument =Dispatch.call(pptDocument, "Open", filePath).toDispatch();
			
			//全部幻燈片
			Dispatch slides=	Dispatch.get(curDocument, "Slides").toDispatch();
			//獲取幻燈片數量
			Variant slidesCount = Dispatch.get(slides, "Count");
			
			//遍歷幻燈片
			for(int i=0;i<slidesCount.getInt();i++) {
				Dispatch slide= Dispatch.call(slides, "Item", new Variant(i+1)).toDispatch();
				//獲取幻燈片內全部元素
			    Dispatch shapes =	Dispatch.get(slide, "Shapes").toDispatch();
			    //添加水印
			    Dispatch.call(shapes, "AddTextEffect",new Variant(0),waterContent,"宋體",new Variant(10),new Variant(0),new Variant(1),new Variant(0),new Variant(0)).toDispatch();
			}
			//保存
			Dispatch.call(curDocument, "Save");
			//關閉文件
			Dispatch.call(curDocument, "Close");
			//關閉程序
			ppt.invoke("Quit", new Variant[] {});
			//釋放COM
			ComThread.quitMainSTA();
			InputStream isFileFrom=FileUtils.openInputStream(fileFrom);
			IOUtils.copy(isFileFrom, os);
			os.close();
			isFileFrom.close();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.error(ExceptionUtils.getStackTrace(e));
		}
		return false;
		
	}
	
	
	public static void openDocument(String filePath) {
		 //初始化com的線程
		ComThread.InitSTA();
		 ActiveXComponent ppt=null;  
//		  ActiveXComponent presentation=null;  
		      
		ppt = new ActiveXComponent("PowerPoint.Application");  
		ppt.setProperty("Visible", new Variant(true));  
		Dispatch pptDocument = ppt.getProperty("Presentations").toDispatch();
		Dispatch curDocument =Dispatch.call(pptDocument, "Open", filePath).toDispatch(); 
//Slides;
		//全部幻燈片
		Dispatch slides=	Dispatch.get(curDocument, "Slides").toDispatch();
		//獲取幻燈片數量
		Variant slidesCount = Dispatch.get(slides, "Count");
		System.out.println("slidesCount:"+slidesCount);
		
		//遍歷幻燈片
		for(int i=0;i<slidesCount.getInt();i++) {
			Dispatch slide= Dispatch.call(slides, "Item", new Variant(i+1)).toDispatch();
			//獲取幻燈片內全部元素
		    Dispatch shapes =	Dispatch.get(slide, "Shapes").toDispatch();
		    Dispatch textEffect= Dispatch.call(shapes, "AddTextEffect",new Variant(0),"測試水印","宋體",new Variant(10),new Variant(0),new Variant(1),new Variant(0),new Variant(0)).toDispatch();
//		    textEffect
		}
		ComThread.quitMainSTA();
        
        
		
		
		
		
		
		
		
		
	}
	
	public static void main(String[] args) {
		openDocument("d:/a.pptx");
	}

}
相關文章
相關標籤/搜索