当前位置: 首页 > article >正文

word,exl,txt转pdf

有些时候需要上传一些附件,比如exl,word,预览的时候客户需要用pdf展示,下面就简答介绍下转换方法

效果图

依赖:

这是我从网上下载到本地然后再依赖的,也可以使用pom文件依赖更好

代码如下:

import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;

import java.io.*;


public class Office2PdfUtil {
    /**
     * 生成pdf方法
     * @param sPath
     * @param dPath
     */
    public static void excel2pdf(String sPath, String dPath) {
        // 验证License 否则有水印
        if (!authrolizeLicense()){
            System.out.println("许可证无效!");
        }

        try {
            // 原始excel路径
            Workbook wb = new Workbook(sPath);
            FileOutputStream fileOS = new FileOutputStream(dPath);
            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
            //把内容放在一张PDF 页面上;
            pdfSaveOptions.setOnePagePerSheet(false);
            pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
            wb.save(fileOS, pdfSaveOptions);
            fileOS.flush();
            fileOS.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean doc2pdf(String inPath, String outPath) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!authrolizeLicense()) {
            return false;
        }
        FileOutputStream os = null;
        try {
            long old = System.currentTimeMillis();
            // 新建一个空白pdf文档
            File file = new File(outPath);
            os = new FileOutputStream(file);
            // Address是将要被转化的word文档
            Document doc = new Document(inPath);
            // 全面支持DOC, DOCX, OOXML, TXT,RTF HTML, OpenDocument, PDF,
            doc.save(os, SaveFormat.PDF);
            // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            // 转化用时
            System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }


    /**
     * 鉴权
     * @return
     */
    public static boolean authrolizeLicense() {
        boolean result = false;
        try {
            InputStream is = License.class.getResourceAsStream("/com.aspose.cells.lic_2999.xml");
            License asposeLicense = new License();
            asposeLicense.setLicense(is);
            is.close();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        excel2pdf("D:\\work\\pdf\\xls测试.xls", "D:\\work\\output\\xls测试.pdf");
        excel2pdf("D:\\work\\pdf\\xlsx测试.xlsx", "D:\\work\\output\\xlsx测试.pdf");
        doc2pdf("D:\\work\\pdf\\文本测试.txt", "D:\\work\\output\\文本测试.pdf");
        doc2pdf("D:\\work\\pdf\\docx测试.docx", "D:\\work\\output\\docx测试.pdf");
        doc2pdf("D:\\work\\pdf\\doc测试.doc", "D:\\work\\output\\doc测试.pdf");
    }
}


http://www.kler.cn/news/363127.html

相关文章:

  • 微软开源的GraphRAG能做什么?
  • proteus中没有STM32F103C8(已解决)
  • 易控天地|易控天地标准版3.0(EconTNT STD3.0)安装记录
  • 空间数据分析实验04:空间统计分析
  • Java 使用 itextpdf 自定义 生成 pdf
  • 机器学习与神经网络:科技的星辰大海
  • HttpOnly Cookie
  • 产品经理应掌握的 API 接口技术知识
  • 暴雨基于NVIDIA Blackwell的AI服务器开始交付给客户
  • React与TypeScript
  • 分布式环境的分布式锁 - Redlock方案和fencing token方案
  • 手撕数据结构 —— 堆(C语言讲解)
  • Spring Boot:植物健康的智能守护者
  • 【建议收藏】2024年最新Windows系统重装教程:轻松学会,小白必看,赶紧收藏!
  • Unity3D VisionPro 环境扫描 空间理解 网格扫描 AR Mesh
  • Linux - 文件描述符 | 文件系统 | 软硬链接
  • 【实战场景】java.util.LinkedHashMap cannot be cast to XXXX 问题
  • 私域小程序怎么运营,如何引流?(二)
  • 基于MATLAB车道检测与跟踪
  • 《计算机视觉》—— 换脸
  • 背景音乐自动播放createjs
  • keepalived(高可用)+nginx(负载均衡)+web
  • 在 Spring 框架中,@ComponentScan` 扫描的注解类型
  • MissingSemester-版本控制系统Git
  • 讯飞、阿里云、腾讯云:Android 语音合成服务对比选择
  • 计算机网络——可靠数据传输原理