java进行文档类型转换PDF

该博客介绍了如何利用Jacob库在Java中将Word、Excel和PowerPoint文档转换为PDF格式。Jacob是一个Java到COM桥,使得在Windows环境下可以进行文件类型的转换。博主提供了详细的代码示例,包括转换方法及所需的.dll文件下载链接。
摘要由CSDN通过智能技术生成

使用jacob进行文档类型转换支持PPT、Excel、Word转为PDF模式

本方法对Windows部署的项目友好,最后需要在jdk/bin目录下导入与jar包版本一致的.dll文件

文件地址

链接:https://pan.baidu.com/s/1rSCC8RgwL3DN0Fz0pkCARQ
提取码:a065

jar包

 <!-- 文档转换 -->
        <dependency>
            <groupId>net.sf.jacob-project</groupId>
            <artifactId>jacob</artifactId>
            <version>1.14.3</version>
        </dependency>

文档转换工具类

package com.shouzhi.service.common;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

import java.io.File;

/**
 * 文档类型转换为PDF预览
 * @author Mr
 * @date 2021/4/2 14:02
 */
public class PDFUtil {
    private static final int wdFormatPDF = 17;
    private static final int xlTypePDF = 0;
    private static final int ppSaveAsPDF = 32;


    //判断文档类型
    public static boolean change2PDF(String inputFile, String pdfFile) {
        String suffix = getFileSufix(inputFile);
        File file = new File(inputFile);
        if (!file.exists()) {
            return false;
        }
        if (suffix.equals("pdf")) {
            return false;
        }
        if (suffix.equals("doc") || suffix.equals("docx")) {
            return wordToPDF(inputFile, pdfFile);
        } else if (suffix.equals("ppt") || suffix.equals("pptx")) {
            return pptToPDF(inputFile, pdfFile);
        } else if (suffix.equals("xls") || suffix.equals("xlsx")) {
            return excelToPDF(inputFile, pdfFile);
        } else {
            return false;
        }
    }
    public static String getFileSufix(String fileName) {
        int splitIndex = fileName.lastIndexOf(".");
        return fileName.substring(splitIndex + 1);
    }
    // word转换为pdf
    public static boolean wordToPDF(String inputFile, String pdfFile) {
        try {
            // 打开word应用程序
            ActiveXComponent app = new ActiveXComponent("Word.Application");
            // 设置word不可见
            app.setProperty("Visible", false);
            // 获得word中所有打开的文档,返回Documents对象
            Dispatch docs = app.getProperty("Documents").toDispatch();
            // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
            Dispatch doc = Dispatch.call(docs, "Open", inputFile, false, true)
                    .toDispatch();
            Dispatch.call(doc, "ExportAsFixedFormat", pdfFile, wdFormatPDF);// word保存为pdf格式宏,值为17
            // 关闭文档
            Dispatch.call(doc, "Close", false);
            // 关闭word应用程序
            app.invoke("Quit", 0);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    // excel转换为pdf
    public static boolean excelToPDF(String inputFile, String pdfFile) {
        try {
            ActiveXComponent app = new ActiveXComponent("Excel.Application");
            app.setProperty("Visible", false);
            Dispatch excels = app.getProperty("Workbooks").toDispatch();
            Dispatch excel = Dispatch.call(excels, "Open", inputFile, false,
                    true).toDispatch();
            Dispatch.call(excel, "ExportAsFixedFormat", xlTypePDF, pdfFile);
            Dispatch.call(excel, "Close", false);
            app.invoke("Quit");
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    // ppt转换为pdf
    public static boolean pptToPDF(String inputFile, String pdfFile) {
        try {
            ActiveXComponent app = new ActiveXComponent(
                    "PowerPoint.Application");
            // app.setProperty("Visible", msofalse);
            Dispatch ppts = app.getProperty("Presentations").toDispatch();

            Dispatch ppt = Dispatch.call(ppts, "Open", inputFile, true,// ReadOnly
                    true,// Untitled指定文件是否有标题
                    false// WithWindow指定文件是否可见
            ).toDispatch();

            Dispatch.call(ppt, "SaveAs", pdfFile, ppSaveAsPDF);

            Dispatch.call(ppt, "Close");

            app.invoke("Quit");
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用Apache POI和iText库来实现Java docx文档转换成pdf文档。 首先,你需要从Apache POI网站下载POI和POI-OOXML JAR文件。这些库将帮助你读取和处理docx文件。 然后,你需要从iText网站下载iText JAR文件。这个库将帮助你生成pdf文件。 下面是一个简单的Java代码示例,演示如何使用这些库将docx文件转换为pdf文件: ```java import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfWriter; public class DocxToPdfConverter { public static void main(String[] args) { try { // 读取docx文件 FileInputStream in = new FileInputStream("input.docx"); XWPFDocument document = new XWPFDocument(in); // 准备生成pdf文件 PdfOptions options = PdfOptions.create(); FileOutputStream out = new FileOutputStream("output.pdf"); Document pdfDocument = new Document(); PdfWriter.getInstance(pdfDocument, out); // 将docx文件转换为pdf文件 PdfConverter.getInstance().convert(document, pdfDocument, options); // 关闭文件pdfDocument.close(); out.close(); document.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这个示例代码将读取名为“input.docx”的docx文件,并将其转换为名为“output.pdf”的pdf文件。你可以根据自己的需求修改文件名和文件路径。 代码执行完毕后,你将在指定的输出路径中找到生成的pdf文件
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值