pdf转换工具(pdf转word、xls等)

软件背景

wps那些转换要充会员(擦没忍住,买了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 "转换遇到异常";
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Apache POI 和 iText 库来实现 Java 中的 xls pdf。 首先,使用 Apache POI 库读取 xls 文件中的数据,将其转换为一个二维数组。然后,使用 iText 库创建一个 PDF 文档,并将数据写入 PDF 文件中。以下是一个简单的示例代码: ```java import java.io.*; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; public class XlsToPdfConverter { public static void main(String[] args) { try { // 读取 xls 文件 FileInputStream fileInputStream = new FileInputStream("input.xls"); Workbook workbook = new HSSFWorkbook(fileInputStream); Sheet sheet = workbook.getSheetAt(0); // 创建 pdf 文件 Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); // 将 xls 数据写入 pdf PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum()); for (Row row : sheet) { for (Cell cell : row) { table.addCell(cell.toString()); } } document.add(table); // 关闭文件流 document.close(); fileInputStream.close(); System.out.println("xls pdf 完成!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 在该示例中,我们使用 HSSFWorkbook 类从 xls 文件中读取数据,并使用 PdfPTable 类将数据写入 pdf 文件中。请注意,我们必须在循环中逐个遍历 xls 文件中的所有单元格,并将其添加到 PdfPTable 中,以便将其写入 pdf 文件。 这只是一个简单的示例。实际转换过程可能更加复杂,具体取决于你的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值