• pdf转换工具


    软件背景

    wps那些转换要充会员,所以写了个转换工具,可以把pdf转成doc、docx、ppt、xls、svg

    依托aspose-pdf,exe4j打包

    网盘地址

    链接: https://pan.baidu.com/s/1R2rM_GD_4WczdLhsf9st4A?pwd=7ra7 提取码: 7ra7 复制这段内容后打开百度网盘手机App,操作更方便哦

    软件截图

     

     

     

     

     代码

    1. package com.business;
    2. import com.aspose.pdf.Document;
    3. import javax.swing.*;
    4. import java.awt.*;
    5. import java.awt.event.ActionEvent;
    6. import java.awt.event.ActionListener;
    7. import java.io.File;
    8. import java.io.FileOutputStream;
    9. import static javax.swing.JFrame.EXIT_ON_CLOSE;
    10. public class Pdf2Word {
    11. public static String path = "";
    12. public static void main(String[] args) {
    13. JFrame frame = new JFrame("pdf转换工具");
    14. JPanel panel = new JPanel();
    15. panel.setLayout(new FlowLayout());
    16. String type[] = {"Doc", "DocX", "Svg", "Excel", "Pptx"};
    17. //下拉选
    18. JComboBox jComboBox = new JComboBox(type);
    19. JLabel tipLable = new JLabel("请选择要转换的文件类型");
    20. JLabel label = new JLabel();
    21. JButton button = new JButton("开始转换");
    22. //选择文件
    23. JMenuItem jMenuItem = new JMenuItem("请选择pdf文件路径");
    24. jMenuItem.setBackground(Color.red);
    25. jMenuItem.addActionListener(
    26. new ActionListener() {
    27. @Override
    28. public void actionPerformed(ActionEvent e) {
    29. if (e.getSource() == jMenuItem) {
    30. JFileChooser fileChooser = new JFileChooser();
    31. int i = fileChooser.showOpenDialog(panel);
    32. if (i == JFileChooser.APPROVE_OPTION) {
    33. File file = fileChooser.getSelectedFile();
    34. String filepath = file.getPath();
    35. System.out.println("你选择的文件路径为" + filepath);
    36. initPath(filepath);
    37. }
    38. }
    39. }
    40. }
    41. );
    42. panel.add(tipLable);
    43. panel.add(label);
    44. panel.add(jComboBox);
    45. panel.add(jMenuItem);
    46. panel.add(button);
    47. button.addActionListener(
    48. new ActionListener() {
    49. @Override
    50. public void actionPerformed(ActionEvent e) {
    51. String msg = "你选择的类型是: "
    52. + jComboBox.getItemAt(jComboBox.getSelectedIndex()) + ",开始转换pdf,请耐心等待";
    53. System.out.println("点击button,此时path" + path);
    54. if (Pdf2Word.path.endsWith("pdf") || Pdf2Word.path.endsWith("PDF")) {
    55. label.setText(msg);
    56. jComboBox.setVisible(false);
    57. tipLable.setVisible(false);
    58. button.setVisible(false);
    59. jMenuItem.setVisible(false);
    60. String resStr = pdf2doc(Pdf2Word.path, jComboBox.getItemAt(jComboBox.getSelectedIndex()).toString());
    61. label.setText(resStr);
    62. } else {
    63. label.setText("请选择pdf文件!");
    64. }
    65. }
    66. }
    67. );
    68. frame.add(panel);
    69. frame.setSize(600, 400);
    70. frame.setLocationRelativeTo(null);
    71. frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    72. frame.setVisible(true);
    73. //pdf2doc("C:\\Users\\Administrator\\Desktop\\安全渗透-检测报告.pdf");
    74. }
    75. public static String initPath(String path) {
    76. Pdf2Word.path = path;
    77. return path;
    78. }
    79. //pdf转doc
    80. public static String pdf2doc(String pdfPath, String type) {
    81. System.out.println("开始转化pdf,转化类型" + type);
    82. long old = System.currentTimeMillis();
    83. try {
    84. String filePath = "";
    85. int oprationCode = 0;
    86. switch (type) {
    87. case "Doc":
    88. filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".doc";
    89. oprationCode = 1;
    90. break;
    91. case "Html":
    92. filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".html";
    93. oprationCode = 3;
    94. break;
    95. case "Xml":
    96. filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".xml";
    97. oprationCode = 4;
    98. break;
    99. case "TeX":
    100. filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".txt";
    101. oprationCode = 5;
    102. break;
    103. case "DocX":
    104. filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".docx";
    105. oprationCode = 6;
    106. break;
    107. case "Svg":
    108. filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".svg";
    109. oprationCode = 7;
    110. break;
    111. case "Excel":
    112. filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".xls";
    113. oprationCode = 9;
    114. break;
    115. case "Pptx":
    116. filePath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + ".ppt";
    117. oprationCode = 14;
    118. break;
    119. }
    120. //新建一个word文档
    121. FileOutputStream os = new FileOutputStream(filePath);
    122. //doc是将要被转化的word文档
    123. Document doc = new Document(pdfPath);
    124. //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换 SaveFormat.Doc
    125. doc.save(os, oprationCode);
    126. os.close();
    127. //转化用时
    128. long now = System.currentTimeMillis();
    129. String resStr = "Pdf 转 Word 共耗时:" + ((now - old) / 1000.0) + "秒";
    130. System.out.println(resStr);
    131. return resStr;
    132. } catch (Exception e) {
    133. System.out.println("Pdf 转 Word 失败...");
    134. e.printStackTrace();
    135. }
    136. return "转换遇到异常";
    137. }
    138. }

  • 相关阅读:
    Java中的高级特性与最佳实践
    电子器件系列37:SD卡座(Push-Push和Push-Pull)
    中国PETG市场预测及战略研究报告(2022版)
    Discourse 的用户快速找到管理员账号
    C++|list的模拟实现
    Arduino驱动BMA220三轴加速度传感器(惯性测量传感器篇)
    HTTP框架 - HttpMaster 核心基类上传
    类型体系与基本数据类型(第一节)
    DBLink 简介
    ASW3410数据手册|ASW3410设计参数|USB3.0高速数据开关切换IC说明书
  • 原文地址:https://blog.csdn.net/m0_37615458/article/details/126279363