直接上代码
package wgtx.wlzx.YZConverter;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
import application.Application;
import application.Workbooks;
import application.workbooks.Workbook;
public class ConverTo
{
public static void main(String[] args)
{
String frompath="D:\\yztest\\123456.doc"; //要转换的文件路径
String topath="D:\\yztest\\result"; //转换后保存的路径
System.out.println(jpg(frompath,topath));
}
public static String jpg(String frompath,String topath)
{
//获取后缀名并转成小写
String extension=frompath.substring(frompath.lastIndexOf(".")).toLowerCase();
//控制文件类型
if(!(extension.equals(".doc")||extension.equals(".docx")))
return "不支持"+extension+"类型";
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Application.createInstance(frame.getContentPane());
frame.add(Application.getInstance());
frame.setSize(1440,900);
frame.setVisible(true);
Workbooks books=Application.getWorkbooks();
System.out.println("选择文件路径:"+frompath);
books.openWorkbook(frompath);
Workbook book=Application.getWorkbooks().getActiveWorkbook();
try {
System.out.println("保存文件路径:"+topath);
book.getDocuments().getActiveDocument().convertToJPEG(topath);
Application.exitSystem();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return "success";
}
}