自学内容网 自学内容网

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");
    }
}


原文地址:https://blog.csdn.net/qq_30299243/article/details/142998382

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!