zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android修行手册 - Aspose文档格式excel/pdf/word/odt/图片等相互转换

AndroidExcel转换文档PDF 图片 格式 手册
2023-09-14 09:04:16 时间
专栏分享

👉关于作者

众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣 !!!
专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
有什么需要欢迎私我,交流群让学习不再孤单

在这里插入图片描述

👉实践过程

😜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());
    }
}

👉其他

📢作者:小空和小芝中的小空
📢转载说明-务必注明来源:https://zhima.blog.csdn.net/
📢这位道友请留步☁️,我观你气度不凡,谈吐间隐隐有王者霸气💚,日后定有一番大作为📝!!!旁边有点赞👍收藏🌟今日传你,点了吧,未来你成功☀️,我分文不取,若不成功⚡️,也好回来找我。

温馨提示点击下方卡片获取更多意想不到的资源。
空名先生