package com.glzj.window;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class ComponentInWindow extends JFrame{
private static final long serialVersionUID = 1L;
JTextField text;
JButton wordBtn,pdfBtn,transBtn;
String filepath="";
String wordFilePath = "";
String wordFile = "";
String pdfFile = "";
String temp = "";
public ComponentInWindow() {
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {
setLayout(new FlowLayout()); // 设置布局
wordBtn = new JButton("①选择word文件");
add(wordBtn);
wordBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser(".");
FileFilter filter2 = new FileFilter() {
@Override
public boolean accept(File f) {
String name = f.getName();
return name.toLowerCase().endsWith(".docx");
}
@Override
public String getDescription() {
return "*.docx";
}
};
FileFilter filter = new FileFilter() {
@Override
public String getDescription() {
return "*.doc";
}
@Override
public boolean accept(File f) {
String name = f.getName();
return name.toLowerCase().endsWith(".doc");
}
};
jfc.addChoosableFileFilter(filter);
jfc.addChoosableFileFilter(filter2);
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.setFileFilter(filter);
int returnvl = jfc.showDialog(getParent(), "选择");
if(returnvl==JFileChooser.APPROVE_OPTION) {
File file=jfc.getSelectedFile();
if(file.isFile()){
wordFilePath=file.getAbsolutePath();
System.out.println("文件:"+file.getAbsolutePath());
}
temp = jfc.getSelectedFile().getName().substring(0,jfc.getSelectedFile().getName().lastIndexOf("."))+".pdf";
}
}
});
pdfBtn = new JButton("②pdf文件保存路径");
add(pdfBtn);
pdfBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
int val = jfc.showDialog(getParent(), "选择");
if(val==JFileChooser.APPROVE_OPTION) {
File file=jfc.getSelectedFile();
if(file.isDirectory()){
filepath = file.getAbsolutePath();
System.out.println("文件夹:"+file.getAbsolutePath());
}
//pdfFile = filepath+jfc.getSelectedFile().getName().substring(0,jfc.getSelectedFile().getName().lastIndexOf("."))+".pdf";
pdfFile = filepath+temp;
}
}
});
transBtn = new JButton("③转换");
add(transBtn);
transBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
wordToPdf(wordFilePath,pdfFile);
}
});
JLabel jl = new JLabel("注意:按照按钮上的编号依次操作,否则那你就浪费时间吧!");
add(jl);
}
public void wordToPdf(String word,String pdf) {
ActiveXComponent app = null;
Dispatch documents = null;
String wordFile = word;
String pdfFile = pdf;
System.out.println("开始转换...");
// 开始时间
long start = System.currentTimeMillis();
try {
if(app == null || app.m_pDispatch==0) {
// 打开word
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
app.setProperty("DisplayAlerts", new Variant(false));
}
// 设置word不可见,很多博客下面这里都写了这一句话,其实是没有必要的,因为默认就是不可见的,如果设置可见就是会打开一个word文档,对于转化为pdf明显是没有必要的
// app.setProperty("Visible", false);
if(documents == null || documents.m_pDispatch==0) {
// 获得word中所有打开的文档
documents = app.getProperty("Documents").toDispatch();
}
System.out.println("打开文件: " + wordFile);
// 打开文档
Dispatch document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch();
// 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在
File target = new File(pdfFile);
if (target.exists()) {
target.delete();
}
System.out.println("另存为: " + pdfFile);
// 另存为,将文档报错为pdf,其中word保存为pdf的格式宏的值是17
Dispatch.call(document, "SaveAs", pdfFile, 17);
// 关闭文档
Dispatch.call(document, "Close", false);
// 结束时间
long end = System.currentTimeMillis();
System.out.println("转换成功,用时:" + (end - start) + "ms");
} catch (Exception e) {
System.out.println("转换失败" + e.getMessage());
} finally {
// 关闭office
app.invoke("Quit", 0);
}
}
}