设为首页 加入收藏

TOP

用Aspose-Java免费实现 PDF、Word、Excel、Word互相转换并将转换过得文件上传OSS,返回转换后的文件路径(二)
2023-07-25 21:28:40 】 浏览:75
Tags:Aspose-Java 费实现 PDF Word Excel 相转换 文件上 OSS 后的文 路径
ages().size(); index++) { String fileName = UUID.randomUUID() + ".png"; String filePath = ossUpLoadTools.getSavePath() + "/" + fileName; File tmpFile = new File(filePath); FileOutputStream fileOS = new FileOutputStream(tmpFile); pngDevice.process(pdfDocument.getPages().get_Item(index), fileOS); res.add(ossUpLoadTools.uploadOssFile(fileName, tmpFile)); fileOS.close(); tmpFile.delete(); } } else { throw new ServiceException(2, "抱歉超过10页暂时无法转图片"); } return res; } case "HTML" : { String fileName = UUID.randomUUID() + ".html"; String filePath = ossUpLoadTools.getSavePath() + "/" + fileName; Document doc = new Document(file.getInputStream()); HtmlSaveOptions saveOptions = new HtmlSaveOptions(); saveOptions.setFixedLayout(true); saveOptions.setSplitIntoPages(false); saveOptions.setRasterImagesSavingMode(HtmlSaveOptions.RasterImagesSavingModes.AsExternalPngFilesReferencedViaSvg); doc.save(filePath , saveOptions); doc.close(); File outputfile = new File(filePath); res.add(ossUpLoadTools.uploadOssFile(fileName, outputfile)); outputfile.delete(); return res; } default:{} } } catch (Exception e) { e.printStackTrace(); } return null; } private List<String> switchFile(MultipartFile file, SaveFormat saveFormat, String suffix) { List<String> resUrl = new ArrayList<>(); try { long old = System.currentTimeMillis(); // 输出路径 String fileName = UUID.randomUUID() + "." + suffix; String filePath = ossUpLoadTools.getSavePath() + "/" + fileName; FileOutputStream os = new FileOutputStream(filePath); Document doc = new Document(file.getInputStream()); doc.save(os, saveFormat); os.close(); doc.close(); File outputfile = new File(filePath); resUrl.add(ossUpLoadTools.uploadOssFile(fileName, outputfile)); outputfile.delete(); long now = System.currentTimeMillis(); log.info("共耗时:" + ((now - old) / 1000.0) + "秒"); }catch (IOException e) { e.printStackTrace(); } return resUrl; }

 ③ 合并两个、多个PDF文件

    /**
      * @description: 合并两个PDF文件
      * @return: java.lang.String
      * @author: zhouhong
      * @date: 2023/5/1 23:40
      */
    @Override
    public String mergeTwoPdfFile(MultipartFile  file1, MultipartFile file2) {
        try {
            Document doc1 = new Document(file1.getInputStream());
            Document doc2 = new Document(file2.getInputStream());
            doc1.getPages().add(doc2.getPages());

            String fileName = UUID.randomUUID() + ".pdf";
            String filePath = ossUpLoadTools.getSavePath() + "/" + fileName;
            doc1.save(filePath);
            doc1.close();
            File outputfile  = new File(filePath);
            String res = ossUpLoadTools.uploadOssFile(fileName, outputfile);
            outputfile.delete();
            return res;
        } catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }
    /**
      * @description:  合并对个PDF文件
      * @return: java.lang.String
      * @author: zhouhong
      * @date: 2023/5/1 23:40
      */
    @Override
    public String mergeMorePdfFile(MultipartFile ... file) {
        try {
            String mergeFileName = UUID.randomUUID() + ".pdf";
            String mergePdfPath = ossUpLoadTools.getSavePath() + "/"  + mergeFileName;
            String[] chilPdfPath = new String[file.length];
            // 读取PDF并获取路径
            for (int i = 0; i < file.length; i++) {
                String fileName = UUID.randomUUID() + ".pdf";
                String fileP
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇.MD语法入门,教你写好readme文档 下一篇Spring源码:Bean的生命周期(二)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目