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

其他教程   发布日期:2025年02月22日   浏览次数:206

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

实践过程

pdf转excel

  1. public static long pdfToExcel(String inFile, String outFile) throws Exception {
  2. if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
  3. return 0;
  4. }
  5. try {
  6. long old = System.currentTimeMillis();
  7. Document doc = new Document(inFile);
  8. ExcelSaveOptions options = new ExcelSaveOptions();
  9. options.setFormat(ExcelSaveOptions.ExcelFormat.XLSX);
  10. doc.save(outFile, options);
  11. Out.print(inFile, outFile, System.currentTimeMillis(), old);
  12. return new File(outFile).length();
  13. }catch (Exception e) {
  14. e.printStackTrace();
  15. throw new Exception(e.getMessage());
  16. }
  17. }

excel转pdf

  1. public static long excelToPdf(String inFile, String outFile) throws Exception {
  2. if (!com.yrnet.transfer.business.transfer.file.License.getExcelLicense()) {
  3. return 0;
  4. }
  5. try {
  6. long old = System.currentTimeMillis();
  7. File pdfFile = new File(outFile);
  8. Workbook wb = new Workbook(inFile);
  9. PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
  10. pdfSaveOptions.setOnePagePerSheet(true);
  11. FileOutputStream fileOS = new FileOutputStream(pdfFile);
  12. wb.save(fileOS, SaveFormat.PDF);
  13. fileOS.close();
  14. long now = System.currentTimeMillis();
  15. Out.print(inFile, outFile, now, old);
  16. return pdfFile.length();
  17. }catch (Exception e) {
  18. e.printStackTrace();
  19. throw new Exception(e.getMessage());
  20. }
  21. }

ppt转pdf

  1. public static long pptToPdf(String inFile, String outFile) throws Exception {
  2. if (!com.yrnet.transfer.business.transfer.file.License.getPptLicense()) {
  3. return 0;
  4. }
  5. try {
  6. long old = System.currentTimeMillis();
  7. File pdfFile = new File(outFile);
  8. FileOutputStream os = new FileOutputStream(pdfFile);
  9. Presentation pres = new Presentation(inFile);
  10. pres.save(os, com.aspose.slides.SaveFormat.Pdf);
  11. os.close();
  12. long now = System.currentTimeMillis();
  13. Out.print(inFile, outFile, now, old);
  14. return pdfFile.length();
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. throw new Exception(e.getMessage());
  18. }
  19. }

pdf转ppt

  1. public static long pdfToPpt(String inFile, String outFile) {
  2. if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
  3. return 0;
  4. }
  5. long old = System.currentTimeMillis();
  6. Document pdfDocument = new Document(inFile);
  7. PptxSaveOptions pptxOptions = new PptxSaveOptions();
  8. pptxOptions.setExtractOcrSublayerOnly(true);
  9. pdfDocument.save(outFile, pptxOptions);
  10. long now = System.currentTimeMillis();
  11. Out.print(inFile, outFile, now, old);
  12. return new File(outFile).length();
  13. }

pdf转word

  1. public static long pdfToDoc(String inFile, String outFile) {
  2. if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
  3. return 0;
  4. }
  5. log.info("开始转换...");
  6. long old = System.currentTimeMillis();
  7. Document pdfDocument = new Document(inFile);
  8. DocSaveOptions saveOptions = new DocSaveOptions();
  9. /** 或者DocSaveOptions.DocFormat.DocX*/
  10. saveOptions.setFormat(DocSaveOptions.DocFormat.Doc);
  11. pdfDocument.save(outFile, saveOptions);
  12. long now = System.currentTimeMillis();
  13. Out.print(inFile, outFile, now, old);
  14. log.info("转换结束...");
  15. return new File(outFile).length();
  16. }

word转pdf

  1. public static long wordToPdf(String inFile, String outFile) throws Exception {
  2. if (!com.yrnet.transfer.business.transfer.file.License.getWordLicense()) {
  3. return 0;
  4. }
  5. try {
  6. long old = System.currentTimeMillis();
  7. File file = new File(outFile);
  8. FileOutputStream os = new FileOutputStream(file);
  9. Document doc = new Document(inFile);
  10. Document tmp = new Document();
  11. tmp.removeAllChildren();
  12. tmp.appendDocument(doc, ImportFormatMode.USE_DESTINATION_STYLES);
  13. System.out.println("开始解析word文档" + inFile);
  14. doc.save(os, SaveFormat.PDF);
  15. long now = System.currentTimeMillis();
  16. log.info("target file size:{}",file.length());
  17. os.close();
  18. Out.print(inFile, outFile, now, old);
  19. return file.length();
  20. } catch (Exception e) {
  21. log.error(inFile + "转换失败,请重试",e);
  22. throw new Exception(e.getMessage());
  23. }
  24. }

excel转图片

  1. public static long excelToPic(String inFile, String outFile) throws Exception {
  2. if (!com.yrnet.transfer.business.transfer.file.License.getExcelLicense()) {
  3. return 0;
  4. }
  5. try {
  6. long old = System.currentTimeMillis();
  7. Workbook wb = new Workbook(inFile);
  8. Worksheet sheet = wb.getWorksheets().get(0);
  9. ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
  10. imgOptions.setImageFormat(ImageFormat.getPng());
  11. imgOptions.setCellAutoFit(true);
  12. imgOptions.setOnePagePerSheet(true);
  13. SheetRender render = new SheetRender(sheet, imgOptions);
  14. render.toImage(0, outFile);
  15. long now = System.currentTimeMillis();
  16. Out.print(inFile, outFile, now, old);
  17. return new File(outFile).length();
  18. }catch (Exception e) {
  19. e.printStackTrace();
  20. throw new Exception(e.getMessage());
  21. }
  22. }

pdf转图片

  1. public static long pdfToPng(String inFile, List<String> outFile) throws Exception {
  2. long size = 0;
  3. if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
  4. return size;
  5. }
  6. try {
  7. long old = System.currentTimeMillis();
  8. Document pdfDocument = new Document(inFile);
  9. Resolution resolution = new Resolution(960);
  10. JpegDevice jpegDevice = new JpegDevice(resolution);
  11. for (int index=1;index<=pdfDocument.getPages().size();index++) {
  12. String path = inFile.substring(0,inFile.lastIndexOf(".")) + "_"+index+".png";
  13. File file = new File(path);
  14. size += file.length();
  15. FileOutputStream fileOs = new FileOutputStream(file);
  16. jpegDevice.process(pdfDocument.getPages().get_Item(index), fileOs);
  17. outFile.add(path);
  18. fileOs.close();
  19. long now = System.currentTimeMillis();
  20. Out.print(inFile, path, now, old);
  21. }
  22. return size;
  23. }catch (Exception e){
  24. log.error(e.getMessage(),e);
  25. throw new Exception(e.getMessage());
  26. }
  27. }

odt转pdf

  1. public static long pdfToPng(String inFile, List<String> outFile) throws Exception {
  2. long size = 0;
  3. if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
  4. return size;
  5. }
  6. try {
  7. long old = System.currentTimeMillis();
  8. Document pdfDocument = new Document(inFile);
  9. Resolution resolution = new Resolution(960);
  10. JpegDevice jpegDevice = new JpegDevice(resolution);
  11. for (int index=1;index<=pdfDocument.getPages().size();index++) {
  12. String path = inFile.substring(0,inFile.lastIndexOf(".")) + "_"+index+".png";
  13. File file = new File(path);
  14. size += file.length();
  15. FileOutputStream fileOs = new FileOutputStream(file);
  16. jpegDevice.process(pdfDocument.getPages().get_Item(index), fileOs);
  17. outFile.add(path);
  18. fileOs.close();
  19. long now = System.currentTimeMillis();
  20. Out.print(inFile, path, now, old);
  21. }
  22. return size;
  23. }catch (Exception e){
  24. log.error(e.getMessage(),e);
  25. throw new Exception(e.getMessage());
  26. }
  27. }

以上就是Android怎么实现excel/pdf/word/odt/图片相互转换的详细内容,更多关于Android怎么实现excel/pdf/word/odt/图片相互转换的资料请关注九品源码其它相关文章!