Java实现Word文档转PDF,PDF转Word,PDF转Excel,PDF转换工具

前言

java实现word文档转PDF,PDF转word
解决只能转换4页问题
解决每页头部存在水印问题

实现

引入依赖

<dependency>
   <groupId>com.documents4j</groupId>
    <artifactId>documents4j-local</artifactId>
    <version>1.0.3</version>
</dependency>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-word</artifactId>
    <version>1.0.3</version>
</dependency>

破解的jar包
链接: https://pan.baidu.com/s/1MO8OBuf4FQ937R9KDtofPQ 提取码: 4tsn

package com.common.util;

import com.aspose.pdf.Document;
import com.aspose.pdf.SaveFormat;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;


import java.io.*;

/**
 * PDF转换工具类
 * @author yyq
 */
public class PdfUtil {

    public static void main(String[] args) {

        //pdfToObj("C:\\Users\\Administrator\\Desktop\\测试.pdf", "docx");

        String filePath = "C:\\Users\\Administrator\\Desktop\\测试.docx";
        String outFilePath = "C:\\Users\\Administrator\\Desktop\\测试.pdf";
        objToPdf(filePath, outFilePath, "docx");
    }

    /**
     * PDF 转 doc、Excel、xml
     * @param pdfPath 需要转换的pdf路径
     * @param suffix 文件后缀
     */
    public static void pdfToObj(String pdfPath, String suffix) {
        long old = System.currentTimeMillis();
        FileOutputStream os = null;
        Document doc = null;
        try {
            // 新建一个word文档
            String wordPath = pdfPath.substring(0, pdfPath.lastIndexOf(".")) + "." + suffix;
            os = new FileOutputStream(wordPath);
            // doc是将要被转化的word文档
            doc = new Document(pdfPath);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            if(suffix.equals("doc")){
                doc.save(os, SaveFormat.Doc);
            }else if(suffix.equals("docx")){
                doc.save(os, SaveFormat.DocX);
            }else if(suffix.equals("xls") || suffix.equals("xlsx")){
                doc.save(os, SaveFormat.Excel);
            }else if(suffix.equals("html")){
                doc.save(os, SaveFormat.Html);
            }else if(suffix.equals("xml")){
                doc.save(os, SaveFormat.Xml);
            }
            os.flush();
            os.close();
            doc.close();
            // 转化耗时
            long now = System.currentTimeMillis();
            long useTime = ((now - old) / 1000);
            System.out.println("Pdf 转 Word 共耗时:" + useTime + "秒");
        } catch (Exception e) {
            System.out.println("Pdf 转 Word 失败...");
            e.printStackTrace();
        }finally {
           try {
                if (os != null){
                    os.flush();
                    os.close();
                }
                if (doc != null){
                    doc.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * txt、doc、Excel、xml 转 PDF
     * @param oidPath 需要转的文件路径
     * @param newPath 转成的PDF文件路径
     */
    public static void objToPdf(String oidPath, String newPath, String suffix){
        InputStream inputStream = null;
        OutputStream outputStream = null;
        IConverter converter = null;
        try {
            // 源文件地址
            File oidFile = new File(oidPath);
            // 导出文件地址
            File newFile = new File(newPath);
            // 文件读取
            inputStream = new FileInputStream(oidFile);
            outputStream = new FileOutputStream(newFile);
            // 开始转换
            converter = LocalConverter.builder().build();
            boolean flag = false;
            if(suffix.equals("doc")){
                flag = converter.convert(inputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
            }else if(suffix.equals("docx")){
                flag = converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            }else if(suffix.equals("txt")){
                flag = converter.convert(inputStream).as(DocumentType.TEXT).to(outputStream).as(DocumentType.PDF).execute();
            }else if(suffix.equals("xls")){
                flag = converter.convert(inputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute();
            }else if(suffix.equals("xlsx")){
                flag = converter.convert(inputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
            }else if(suffix.equals("html")){
                flag = converter.convert(inputStream).as(DocumentType.MHTML).to(outputStream).as(DocumentType.PDF).execute();
            }else if(suffix.equals("xml")){
                flag = converter.convert(inputStream).as(DocumentType.XML).to(outputStream).as(DocumentType.PDF).execute();
            }
            if (flag) {
                converter.shutDown();
            }
            inputStream.close();
            outputStream.flush();
            outputStream.close();
            System.out.println("转换成功");
        } catch (Exception e) {
            converter.shutDown();
            e.printStackTrace();
            System.out.println("转换失败");
        }finally {
            try {
                if (inputStream != null){
                    inputStream.close();
                }
                if (outputStream != null){
                    outputStream.flush();
                    outputStream.close();
                }
                if(converter != null){
                    converter.shutDown();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}

源码路径:https://download.csdn.net/download/weixin_43992507/88215577

像流读取文件这些要关闭释放,不然异常报错文件的读取不会断开的

Excel转PDF的实现方式可以参考:https://blog.csdn.net/m0_37969960/article/details/105519581

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用Apache POI库来实现PDFWord的功能。 POI是一个开源的Java库,提供了操作Microsoft Office格式文件的功能,包括WordExcel和PowerPoint等。 要实现PDFWord,可以按照以下步骤进行操作: 1. 导入Apache POI库的依赖。可以在项目的pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 使用PDFBox库来解析PDF文件。PDFBox是一个用于处理PDF文件的Java库,可以从PDF文件中提取文本内容。可以在项目的pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.24</version> </dependency> ``` 3. 使用Apache POI库创建一个新的Word文档,并将PDF文件中提取的文本内容写入到Word文档中。可以使用Apache POI提供的XWPFDocument类来创建Word文档,使用XWPFParagraph类来创建段落,使用XWPFRun类来创建文本内容。 下面是一个简单的示例代码,演示了如何将PDF文件转换Word文档: ```java import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.poi.xwpf.usermodel.*; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class PdfToWordConverter { public static void main(String[] args) { try { // 加载PDF文件 File pdfFile = new File("input.pdf"); PDDocument document = PDDocument.load(pdfFile); // 创建Word文档 XWPFDocument wordDocument = new XWPFDocument(); // 提取PDF文件中的文本内容 PDFTextStripper stripper = new PDFTextStripper(); String text = stripper.getText(document); // 创建段落并写入文本内容 XWPFParagraph paragraph = wordDocument.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(text); // 保存Word文档 FileOutputStream out = new FileOutputStream("output.docx"); wordDocument.write(out); out.close(); // 关闭PDF文件 document.close(); System.out.println("PDFWord成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 请注意,上述代码只是一个简单的示例,实际应用中可能需要处理更复杂的PDF文件结构和格式。另外,还可以使用其他第三方库或工具实现PDFWord的功能,如iText、Aspose等。 希望以上信息对您有所帮助!如果您有任何其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值