java jfilechooser报错_java 报错信息提示:PowerPoint 无法将 ^0 保存到 ^1是啥意思?...

自己摸索了一天,终于解决,下面贴代码。(支持office文件和txt文件转PDF文件,同需求的朋友可以参考,需要准备两个dll库和对应的jar包)

import java.awt.Component;

import java.io.File;

import javax.swing.JFileChooser;

import javax.swing.filechooser.FileSystemView;

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.ComThread;

import com.jacob.com.Dispatch;

import com.jacob.com.Variant;

public class OfficeToPDF

{

private static final int wdFormatPDF = 17;

private static final int xlTypePDF = 0;

private static final int ppSaveAsPDF = 32;

private static final int msoTrue = -1;

private static final int msofalse = 0;

public static void main(String[] args)

{

String path = "F:/转PDF测试/测试样本/pptx/测试PPT.pptx";

doPDF(path);

}

/**

* 处理office、txt文件得到pdf文件

* @param path

* office文件路径

* */

public static void doPDF(String path){

String inputFile = path;

String suffixName = path.substring(path.lastIndexOf("."),path.length());

String pdfFile = path.replace(suffixName, ".pdf");

long a=System.currentTimeMillis();

boolean result = convert2PDF(inputFile,pdfFile);

System.out.println("耗时 : "+(System.currentTimeMillis()-a)/1000f+" 秒 ");

if(result){

System.out.println("成功");

}else{

System.out.println("失败");

}

}

public static boolean convert2PDF(String inputFile, String pdfFile) {

String suffix = getFileSufix(inputFile);

File file = new File(inputFile);

if(!file.exists()){

System.out.println("文件不存在!");

return false;

}

if(suffix.equals("pdf")){

System.out.println("PDF not need to convert!");

return false;

}

if(suffix.equals("doc")||suffix.equals("docx")||suffix.equals("txt")){

return word2PDF(inputFile,pdfFile);

}else if(suffix.equals("ppt")||suffix.equals("pptx")){

return ppt2PDF(inputFile,pdfFile);

//return pptToPDF(inputFile,pdfFile);

}else if(suffix.equals("xls")||suffix.equals("xlsx")){

return excel2PDF(inputFile,pdfFile);

}else{

System.out.println("文件格式不支持转换!");

return false;

}

}

public static String getFileSufix(String fileName){

int splitIndex = fileName.lastIndexOf(".");

return fileName.substring(splitIndex + 1);

}

public static boolean word2PDF(String inputFile,String pdfFile){

try{

//打开word应用程序

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

//设置word不可见

app.setProperty("Visible", false);

//获得word中所有打开的文档,返回Documents对象

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

//调用Documents对象中Open方法打开文档,并返回打开的文档对象Document

Dispatch doc = Dispatch.call(docs,

"Open",

inputFile,

false,

true

).toDispatch();

//调用Document对象的SaveAs方法,将文档保存为pdf格式

/*

Dispatch.call(doc,

"SaveAs",

pdfFile,

wdFormatPDF//word保存为pdf格式宏,值为17

);

*/

Dispatch.call(doc,

"ExportAsFixedFormat",

pdfFile,

wdFormatPDF//word保存为pdf格式宏,值为17

);

//关闭文档

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

//关闭word应用程序

app.invoke("Quit", 0);

return true;

}catch(Exception e){

e.printStackTrace();

return false;

}

}

public static boolean excel2PDF(String inputFile,String pdfFile){

try{

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

app.setProperty("Visible", false);

Dispatch excels = app.getProperty("Workbooks").toDispatch();

Dispatch excel = Dispatch.call(excels,

"Open",

inputFile,

false,

true

).toDispatch();

Dispatch.call(excel,

"ExportAsFixedFormat",

xlTypePDF,

pdfFile

);

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

app.invoke("Quit");

return true;

}catch(Exception e){

e.printStackTrace();

return false;

}

}

public static boolean ppt2PDF(String inputFile,String pdfFile){

try{

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

//app.setProperty("Visible", msofalse);

Dispatch ppts = app.getProperty("Presentations").toDispatch();

Dispatch ppt = Dispatch.call(ppts,

"Open",

inputFile,

true,//ReadOnly

true,//Untitled指定文件是否有标题

false//WithWindow指定文件是否可见

).toDispatch();

Dispatch.call(ppt,

"SaveAs",

pdfFile,

ppSaveAsPDF

);

Dispatch.call(ppt, "Close");

app.invoke("Quit");

return true;

}catch(Exception e){

e.printStackTrace();

return false;

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值