wps那些转换要充会员,所以写了个转换工具,可以把pdf转成doc、docx、ppt、xls、svg
依托aspose-pdf,exe4j打包
链接: https://pan.baidu.com/s/1R2rM_GD_4WczdLhsf9st4A?pwd=7ra7 提取码: 7ra7 复制这段内容后打开百度网盘手机App,操作更方便哦
- package com.business;
-
- import com.aspose.pdf.Document;
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileOutputStream;
-
- import static javax.swing.JFrame.EXIT_ON_CLOSE;
-
- public class Pdf2Word {
- public static String path = "";
-
-
- public static void main(String[] args) {
-
- JFrame frame = new JFrame("pdf转换工具");
- JPanel panel = new JPanel();
- panel.setLayout(new FlowLayout());
- String type[] = {"Doc", "DocX", "Svg", "Excel", "Pptx"};
-
- //下拉选
- JComboBox jComboBox = new JComboBox(type);
- JLabel tipLable = new JLabel("请选择要转换的文件类型");
- JLabel label = new JLabel();
- JButton button = new JButton("开始转换");
- //选择文件
- JMenuItem jMenuItem = new JMenuItem("请选择pdf文件路径");
- jMenuItem.setBackground(Color.red);
-
- jMenuItem.addActionListener(
- new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == jMenuItem) {
- JFileChooser fileChooser = new JFileChooser();
- int i = fileChooser.showOpenDialog(panel);
- if (i == JFileChooser.APPROVE_OPTION) {
- File file = fileChooser.getSelectedFile();
- String filepath = file.getPath();
- System.out.println("你选择的文件路径为" + filepath);
- initPath(filepath);
- }
- }
- }
- }
- );
-
- panel.add(tipLable);
- panel.add(label);
-
- panel.add(jComboBox);
-
-
- panel.add(jMenuItem);
- panel.add(button);
-
-
- button.addActionListener(
- new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String msg = "你选择的类型是: "
- + jComboBox.getItemAt(jComboBox.getSelectedIndex()) + ",开始转换pdf,请耐心等待";
- System.out.println("点击button,此时path" + path);
- if (Pdf2Word.path.endsWith("pdf") || Pdf2Word.path.endsWith("PDF")) {
- label.setText(msg);
- jComboBox.setVisible(false);
- tipLable.setVisible(false);
- button.setVisible(false);
- jMenuItem.setVisible(false);
- String resStr = pdf2doc(Pdf2Word.path, jComboBox.getItemAt(jComboBox.getSelectedIndex()).toString());
- label.setText(resStr);
-
- } else {
- label.setText("请选择pdf文件!");
- }
- }
- }
- );
- frame.add(panel);
- frame.setSize(600, 400);
- frame.setLocationRelativeTo(null);
- frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
- frame.setVisible(true);
-
-
- //pdf2doc("C:\\Users\\Administrator\\Desktop\\安全渗透-检测报告.pdf");
- }
-
- public static String initPath(String path) {
- Pdf2Word.path = path;
- return path;
- }
-
-
- //pdf转doc
- public static String pdf2doc(String pdfPath, String type) {
- System.out.println("开始转化pdf,转化类型" + type);
- long old = System.currentTimeMillis();
- try {
- String filePath = "";
- int oprationCode = 0;
- switch (type) {
- case "Doc":
- filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".doc";
- oprationCode = 1;
- break;
- case "Html":
- filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".html";
- oprationCode = 3;
- break;
- case "Xml":
- filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".xml";
- oprationCode = 4;
- break;
- case "TeX":
- filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".txt";
- oprationCode = 5;
- break;
- case "DocX":
- filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".docx";
- oprationCode = 6;
- break;
- case "Svg":
- filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".svg";
- oprationCode = 7;
- break;
- case "Excel":
- filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".xls";
- oprationCode = 9;
- break;
- case "Pptx":
- filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".ppt";
- oprationCode = 14;
- break;
- }
-
-
- //新建一个word文档
- FileOutputStream os = new FileOutputStream(filePath);
- //doc是将要被转化的word文档
- Document doc = new Document(pdfPath);
- //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换 SaveFormat.Doc
- doc.save(os, oprationCode);
- os.close();
- //转化用时
- long now = System.currentTimeMillis();
- String resStr = "Pdf 转 Word 共耗时:" + ((now - old) / 1000.0) + "秒";
- System.out.println(resStr);
- return resStr;
- } catch (Exception e) {
- System.out.println("Pdf 转 Word 失败...");
- e.printStackTrace();
- }
- return "转换遇到异常";
- }
-
- }