2019-10-08 JAVA-Word转PDF实现方式

ActiveXComponent app =null;

Dispatch doc =null;

try {

app =new ActiveXComponent("Word.Application");

app.setProperty("Visible",new Variant(false));

Dispatch docs = (Dispatch) app.getProperty("Documents").toDispatch();

//转换前的文件路径

    String startFile = inPath;

//转换后的文件路径

    String overFile =  pdfFile;

doc = Dispatch.call(docs,"Open" , startFile).toDispatch();

File tofile =new File(overFile);

if (tofile.exists()) {

tofile.delete();

}

//        private static final int wdFormatPDF = 17;// PDF 格式

    Dispatch.call(doc,"SaveAs", overFile,17);

}catch (Exception e) {

System.out.println(e.getMessage());

}finally {

Dispatch.call(doc,"Close",false);

if (app !=null)

app.invoke("Quit",new Variant[] {});

}

//结束后关闭进程

ComThread.Release();

你可能感兴趣的:(2019-10-08 JAVA-Word转PDF实现方式)