word文件转成pdf文件

关键字:word,pdf ,java
word文件转成pdf文件主要是打开WORD文档通过PDF虚拟打印机输出成PDF文件。
第一步:安装相关软件。
1、office
2、AcroPro8 (专业版)
3、gs811w32(PDF转换时所需要的脚本ps)
4、postscript(PDF虚拟打印机的驱动)
第二步:配置java操作word插件
先下载jacob包,把包中一个DLL动态库复制window系统system32目录
第三步:编写java类,如下:
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import org.apache.commons.lang.StringUtils;

public class WordToPDF {
/**
* wordCom ActiveX部件
*/
private ActiveXComponent wordCom = null;

/**
* wordDoc word文档
*/
private Object wordDoc = null;

/**
* False 的Variant对象
*/
private final Variant False = new Variant(false);

/**
* true 的Variant对象
*/
private final Variant True = new Variant(true);


/**
* word文档路径
*/
private String sDocFilePath;


/**
* ps文档路径
*/
private String sPsFilePath;

/**
* pdf文档路径
*/
private String sPdfFilePath;


/**
* 密码
*/
private String password=null;
private String password1="whxxmm@yahoo.com.cn";

/**
* 加密码文件路径
*/
private String SWaterMarkPath;

/**
* 是否显示后台打印
*/
private boolean bPrintDos=false;


public WordToPDF(){

}

public WordToPDF(boolean bool){
this.bPrintDos=bool;
}


/**
* @return the bPrintDos
*/
public boolean isBPrintDos() {
return bPrintDos;
}

/**
* @param printDos the bPrintDos to set
*/
public void setBPrintDos(boolean printDos) {
bPrintDos = printDos;
}

/**
* @return the password1
*/
public String getPassword1() {
return password1;
}

/**
* @param password1 the password1 to set
*/
public void setPassword1(String password1) {
this.password1 = password1;
}

/**
* @return the password
*/
public String getPassword() {
return password;
}

/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}

/**
* @return the sDocFilePath
*/
public String getSDocFilePath() {
return sDocFilePath;
}

/**
* @param docFilePath the sDocFilePath to set
*/
public void setSDocFilePath(String docFilePath) {
sDocFilePath = docFilePath;
}

/**
* @return the sPdfFilePath
*/
public String getSPdfFilePath() {
return sPdfFilePath;
}

/**
* @param pdfFilePath the sPdfFilePath to set
*/
public void setSPdfFilePath(String pdfFilePath) {
sPdfFilePath = pdfFilePath;
}

/**
* @return the sPsFilePath
*/
public String getSPsFilePath() {
return sPsFilePath;
}

/**
* @param psFilePath the sPsFilePath to set
*/
public void setSPsFilePath(String psFilePath) {
sPsFilePath = psFilePath;
}

/**
* @return the sWaterMarkPath
*/
public String getSWaterMarkPath() {
return SWaterMarkPath;
}

/**
* @param waterMarkPath the sWaterMarkPath to set
*/
public void setSWaterMarkPath(String waterMarkPath) {
SWaterMarkPath = waterMarkPath;
}

/**
* 打开word文档
*
* @param filePath
* word文档
* @return 返回word文档对象
* @throws Exception
*/
public boolean openWord(String filePath) throws Exception {
boolean bool=false;
// 建立ActiveX部件
wordCom = new ActiveXComponent("Word.Application");

try {
// 返回wrdCom.Documents的Dispatch
Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();
// 调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc
wordDoc = Dispatch.invoke(wrdDocs, "Open", Dispatch.Method,
new Object[] { filePath }, new int[1]).toDispatch();
bool=true;
} catch (Exception ex) {
if(bPrintDos){
System.out.println("openWord - "+ex.getMessage());
}
throw new Exception("打开WORD应用程序出错!!!");
}
return bool;
}

/**
* 关闭word文档
*/
public void closeWord() {
// 关闭word文件
wordCom.invoke("Quit", new Variant[] {});
}

/**
* * 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件 *
*
* @param sourceFilePath
* 源文件路径 *
* @param destinPSFilePath
* 首先生成的PS文件路径 *
* @param destinPDFFilePath
* 生成PDF文件路径
* @throws Exception
*/
public void docToPDF(String sourceFilePath, String destinPSFilePath,
String destinPDFFilePath) throws Exception {
if (!openWord(sourceFilePath)) {
closeWord();
return;
}
// 建立Adobe Distiller的com对象
ActiveXComponent distiller = new ActiveXComponent(
"PDFDistiller.PDFDistiller.1");
try {
// 设置当前使用的打印机,我的Adobe Distiller打印机名字为"Adobe PDF"
// MS Publisher Color Printer为打印机的名称。
wordCom.setProperty("ActivePrinter", new Variant(
"Adobe PDF")); //必需根打印名字一样呀:"MS Publisher Color Printer"
// 设置printout的参数,将word文档打印为postscript文档。目前只使用了前5个参数,如果要使用更多的话可以参考MSDN的office开发相关api
// 是否在后台运行
Variant Background = False;
// 是否追加打印
Variant Append = False;
// 打印所有文档
int wdPrintAllDocument = 0;
Variant Range = new Variant(wdPrintAllDocument);
// 输出的postscript文件的路径
Variant OutputFileName = new Variant(destinPSFilePath);

Dispatch.callN((Dispatch) wordDoc, "PrintOut", new Variant[] {
Background, Append, Range, OutputFileName });
if(bPrintDos){
System.out.println("由word文档打印为ps文档成功!");
}
// 调用Distiller对象的FileToPDF方法所用的参数,详细内容参考Distiller Api手册
// 作为输入的ps文档路径
Variant inputPostScriptFilePath = new Variant(destinPSFilePath);
// 作为输出的pdf文档的路径
Variant outputPDFFilePath = new Variant(destinPDFFilePath);
// 定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件
Variant PDFOption = new Variant("");
// 调用FileToPDF方法将ps文档转换为pdf文档
Variant s=Dispatch.callN(distiller, "FileToPDF", new Variant[] {
inputPostScriptFilePath, outputPDFFilePath, PDFOption });
if(bPrintDos){
System.out.println("由ps文档转换为pdf文档成功!");
}
} catch (Exception ex) {
if(bPrintDos){
System.out.println("docToPDF - "+ex.getMessage());
}
throw new Exception("WORD文档转换PDF文档配置有误,请联系管理员!!!");
} finally {
closeWord();
wordCom = null;
// 释放在程序线程中引用的其它com,比如Adobe PDFDistiller
ComThread.Release();
}
}

/**
* 在pdf文件中添加水印,加密
* @param inputFile 原始文件
*
* @param outputFile 水印输出文件
*
* @param userPassWord 用户密码
*
* @param ownerPassWord 安全密码
*
* @param waterMarkName 水印名字(暂时没有加此功能)
*
* @param permission
*/
public void waterMark(String inputFile, String outputFile,
String userPassWord, String ownerPassWord, String waterMarkName,
int permission) throws Exception {
PdfStamper stamper=null;
try {
// 设置密码
PdfReader reader = new PdfReader(inputFile);
stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
if(StringUtils.isNotBlank(userPassWord)){
stamper.setEncryption(userPassWord.getBytes(), ownerPassWord.getBytes(), permission, false);
}else {
stamper.setEncryption(null, null, permission, false);
}
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
stamper.close();
} catch (Exception e) {
if(bPrintDos){
System.out.println("waterMark - "+e.getMessage());
}
throw new Exception("文件已经加密或文件损坏,请联系管理员!!!");
}finally{
if(bPrintDos){
System.out.println("文件加密成功!");
}

}
}

/**
* 把一个WORD文档转换成PDF
* @throws Exception
*/
public void docToPDF() throws Exception{
this.docToPDF(sDocFilePath,sPsFilePath,sPdfFilePath);
}


/**
* 把一个WORD文档转换成PDF
* @param path WORD文档路径
* @throws Exception
*/
public void DtoP(String path) throws Exception{
if(StringUtils.isBlank(path)){
throw new Exception("没有WORD文档");
}
this.sDocFilePath=path;
this.sPsFilePath=this.headerPath(path)+".ps";
this.sPdfFilePath=this.headerPath(path)+".pdf";
this.docToPDF();

}

/**
* 取一个文件路径,把这文件路径中的文件扩展名除掉。
* @param path 文件全路径
* @return
*/
public String headerPath(String path){
String header=path.substring(0, path.lastIndexOf("."));
return header;
}

/**
* 删除转换时的临时文件,PS和LOG、pdf
* @param path 文件路径(文件扩展名除掉)
*/
public void removefile(String path){
String header=this.headerPath(path);
File f1=new File(header+".log");
f1.delete();
File f2=new File(header+".ps");
f2.delete();
if(StringUtils.isNotBlank(this.SWaterMarkPath)){
File f3=new File(header+".pdf");
f3.delete();
}

}

/**
* 删除转换时的临时文件,PS和LOG、pdf
*
*/
public void removefile(){
this.removefile(this.sPdfFilePath);
}

/**
* 加密PDF文件
* @throws Exception
*/
public void waterMark() throws Exception{
String pass=null;
String pass1=null;
String water=null;
if(StringUtils.isNotBlank(password)){
pass=password;
pass1=password1;
water=this.headerPath(this.sPdfFilePath)+"@@.pdf";
}else{
water=this.headerPath(this.sPdfFilePath)+"@.pdf";
}

String input=this.sPdfFilePath;
this.waterMark(input, water, pass,pass1, null, 0);
this.SWaterMarkPath=water;
}

public static void main(String[] argv) {
String docFile = "d:/12.doc";
WordToPDF wordToPDF = new WordToPDF();
try {
wordToPDF.DtoP(docFile);
// wordToPDF.setPassword("1234");
// wordToPDF.waterMark();
wordToPDF.removefile();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值