Android怎么实现excel/pdf/word/odt/图片相互转换

发布时间:2023-04-15 17:13:04 作者:iii
来源:亿速云 阅读:104

本篇内容主要讲解“Android怎么实现excel/pdf/word/odt/图片相互转换”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android怎么实现excel/pdf/word/odt/图片相互转换”吧!

实践过程

pdf转excel

public static long pdfToExcel(String inFile, String outFile) throws Exception {
    if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
        return 0;
    }
    try {
        long old = System.currentTimeMillis();
        Document doc = new Document(inFile);
        ExcelSaveOptions options = new ExcelSaveOptions();
        options.setFormat(ExcelSaveOptions.ExcelFormat.XLSX);
        doc.save(outFile, options);
        Out.print(inFile, outFile, System.currentTimeMillis(), old);
        return new File(outFile).length();
    }catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e.getMessage());
    }
}

excel转pdf

public static long excelToPdf(String inFile, String outFile) throws Exception {
    if (!com.yrnet.transfer.business.transfer.file.License.getExcelLicense()) {
        return 0;
    }
    try {
        long old = System.currentTimeMillis();
        File pdfFile = new File(outFile);
        Workbook wb = new Workbook(inFile);
        PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
        pdfSaveOptions.setOnePagePerSheet(true);
        FileOutputStream fileOS = new FileOutputStream(pdfFile);
        wb.save(fileOS, SaveFormat.PDF);
        fileOS.close();
        long now = System.currentTimeMillis();
        Out.print(inFile, outFile, now, old);
        return pdfFile.length();
    }catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e.getMessage());
    }
}

ppt转pdf

public static long pptToPdf(String inFile, String outFile) throws Exception {
 if (!com.yrnet.transfer.business.transfer.file.License.getPptLicense()) {
        return 0;
    }
    try {
        long old = System.currentTimeMillis();
        File pdfFile = new File(outFile);
        FileOutputStream os = new FileOutputStream(pdfFile);
        Presentation pres = new Presentation(inFile);
        pres.save(os, com.aspose.slides.SaveFormat.Pdf);
        os.close();
        long now = System.currentTimeMillis();
        Out.print(inFile, outFile, now, old);
        return pdfFile.length();
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e.getMessage());
    }
}

pdf转ppt

public static long pdfToPpt(String inFile, String outFile) {
   if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
        return 0;
    }
    long old = System.currentTimeMillis();
    Document pdfDocument = new Document(inFile);
    PptxSaveOptions pptxOptions = new PptxSaveOptions();
    pptxOptions.setExtractOcrSublayerOnly(true);
    pdfDocument.save(outFile, pptxOptions);
    long now = System.currentTimeMillis();
    Out.print(inFile, outFile, now, old);
    return new File(outFile).length();
}

pdf转word

public static long pdfToDoc(String inFile, String outFile) {
  if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
        return 0;
    }
    log.info("开始转换...");
    long old = System.currentTimeMillis();
    Document pdfDocument = new Document(inFile);
    DocSaveOptions saveOptions = new DocSaveOptions();
    /** 或者DocSaveOptions.DocFormat.DocX*/
    saveOptions.setFormat(DocSaveOptions.DocFormat.Doc);
    pdfDocument.save(outFile, saveOptions);
    long now = System.currentTimeMillis();
    Out.print(inFile, outFile, now, old);
    log.info("转换结束...");
    return new File(outFile).length();
}

word转pdf

public static long wordToPdf(String inFile, String outFile) throws Exception {
    if (!com.yrnet.transfer.business.transfer.file.License.getWordLicense()) {
        return 0;
    }
    try {
        long old = System.currentTimeMillis();
        File file = new File(outFile);
        FileOutputStream os = new FileOutputStream(file);
        Document doc = new Document(inFile);
        Document tmp = new Document();
        tmp.removeAllChildren();
        tmp.appendDocument(doc, ImportFormatMode.USE_DESTINATION_STYLES);
        System.out.println("开始解析word文档" + inFile);
        doc.save(os, SaveFormat.PDF);
        long now = System.currentTimeMillis();
        log.info("target file size:{}",file.length());
        os.close();
        Out.print(inFile, outFile, now, old);
        return file.length();
    } catch (Exception e) {
        log.error(inFile + "转换失败,请重试",e);
        throw new Exception(e.getMessage());
    }
}

excel转图片

public static long excelToPic(String inFile, String outFile) throws Exception {
   if (!com.yrnet.transfer.business.transfer.file.License.getExcelLicense()) {
        return 0;
    }
    try {
        long old = System.currentTimeMillis();
        Workbook wb = new Workbook(inFile);
        Worksheet sheet = wb.getWorksheets().get(0);
        ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
        imgOptions.setImageFormat(ImageFormat.getPng());
        imgOptions.setCellAutoFit(true);
        imgOptions.setOnePagePerSheet(true);
        SheetRender render = new SheetRender(sheet, imgOptions);
        render.toImage(0, outFile);
        long now = System.currentTimeMillis();
        Out.print(inFile, outFile, now, old);
        return new File(outFile).length();
    }catch (Exception e) {
        e.printStackTrace();
        throw new Exception(e.getMessage());
    }
}

pdf转图片

public static long pdfToPng(String inFile, List<String> outFile) throws Exception {
  long size = 0;
   if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
       return size;
   }
   try {
       long old = System.currentTimeMillis();
       Document pdfDocument = new Document(inFile);
       Resolution resolution = new Resolution(960);
       JpegDevice jpegDevice = new JpegDevice(resolution);
       for (int index=1;index<=pdfDocument.getPages().size();index++) {
           String path = inFile.substring(0,inFile.lastIndexOf(".")) + "_"+index+".png";
           File file = new File(path);
           size += file.length();
           FileOutputStream fileOs = new FileOutputStream(file);
           jpegDevice.process(pdfDocument.getPages().get_Item(index), fileOs);
           outFile.add(path);
           fileOs.close();
           long now = System.currentTimeMillis();
           Out.print(inFile, path, now, old);
       }
       return size;
   }catch (Exception e){
       log.error(e.getMessage(),e);
       throw new Exception(e.getMessage());
   }
}

odt转pdf

public static long pdfToPng(String inFile, List<String> outFile) throws Exception {
  long size = 0;
    if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
        return size;
    }
    try {
        long old = System.currentTimeMillis();
        Document pdfDocument = new Document(inFile);
        Resolution resolution = new Resolution(960);
        JpegDevice jpegDevice = new JpegDevice(resolution);
        for (int index=1;index<=pdfDocument.getPages().size();index++) {
            String path = inFile.substring(0,inFile.lastIndexOf(".")) + "_"+index+".png";
            File file = new File(path);
            size += file.length();
            FileOutputStream fileOs = new FileOutputStream(file);
            jpegDevice.process(pdfDocument.getPages().get_Item(index), fileOs);
            outFile.add(path);
            fileOs.close();
            long now = System.currentTimeMillis();
            Out.print(inFile, path, now, old);
        }
        return size;
    }catch (Exception e){
        log.error(e.getMessage(),e);
        throw new Exception(e.getMessage());
    }
}

到此,相信大家对“Android怎么实现excel/pdf/word/odt/图片相互转换”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 学习安卓开发基础的小技巧
  2. Android如何获取当前系统日期和时间

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android excel pdf

上一篇:vue中怎么给el-radio添加tooltip并实现点击跳转

下一篇:基于pdf2docx模块怎么用Python实现批量将PDF转Word文档

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》