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

作者:芝麻粒儿 时间:2021-12-05 23:08:16 

实践过程

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://blog.csdn.net/qq_27489007/article/details/130099912

标签:Android,excel,pdf,word,odt,图片
0
投稿

猜你喜欢

  • Spring+SpringMVC+MyBatis深入学习及搭建(三)之MyBatis全局配置文件解析

    2022-03-21 05:25:23
  • Java基础之容器Vector详解

    2023-11-25 13:10:07
  • mybatisplus使用xml的示例详解

    2021-08-21 18:19:27
  • android studio数据存储建立SQLite数据库实现增删查改

    2023-11-29 17:11:42
  • Android 如何本地加载pdf文件

    2023-05-24 02:11:48
  • Spring Cloud Hystrix 服务降级限流策略详解

    2022-05-02 15:20:27
  • IDEA2022版本创建maven web项目的两种方式详解

    2022-04-24 19:34:54
  • Java并发之线程池Executor框架的深入理解

    2022-03-13 10:20:55
  • 你知道jdk竟有4个random吗

    2022-06-14 23:37:08
  • java搭建ftp/sftp进行数据传递的全过程

    2023-11-29 15:08:10
  • C#字符串加密解密方法实例

    2022-01-11 23:30:14
  • 详解Java中使用泛型实现快速排序算法的方法

    2022-04-28 09:47:00
  • 移动端WebApp隐藏地址栏的方法

    2022-09-26 20:26:29
  • Android 检测键盘显示或隐藏键盘的实现代码

    2022-09-08 09:14:05
  • SpringBoot集成MybatisPlus报错的解决方案

    2022-06-28 05:03:20
  • mybatis映射文件操作存储过程的实现

    2023-07-03 22:36:41
  • Android 实现界面刷新的几种方法

    2023-01-19 06:27:50
  • 阿里开源Java诊断工具神器使用及场景详解

    2023-11-06 17:24:21
  • C#泛型的使用及示例详解

    2022-03-14 07:01:59
  • Java线程生命周期的终止与复位

    2022-04-22 01:57:01
  • asp之家 软件编程 m.aspxhome.com