Aspose for Java最新版文档转换使用

打印 上一主题 下一主题

主题 532|帖子 532|积分 1596

Aspose简介

Aspose.Total是Aspose公司旗下全套文件格式处理解决方案,提供最完整、最高效的文档处理解决方案集,无需任何其他软件安装和依赖。主要提供.net、java、C++d三个开发语言的工具包,通过它,可以对办公文档格式的转换和文档内容的在线编辑,如:Word, Excel, PowerPoint, PPT,图片,PDF等文档。 另外,Aspose.Total 还提供了用于写邮件、拼写检查、创建条形码、OCR和3D等等。
使用样例(22.3版本)

以下列举几种常用的文档格式转换样例,对于Aspose.Total来说,这几个功能只是它的冰山一角。
1.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.     }
复制代码
2.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.     }
复制代码
3.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.     }
复制代码
4.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.     }
复制代码
5.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.     }
复制代码
6.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.     }
复制代码
7.odt转pdf:
  1. public static long odtToPdf(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.             Document doc = new Document(inFile);
  8.             doc.save(outFile);
  9.             long fileSize = new File(outFile).length();
  10.             long now = System.currentTimeMillis();
  11.             log.info("target file size:{}",fileSize);
  12.             Out.print(inFile, outFile, now, old);
  13.             return fileSize;
  14.         } catch (Exception e) {
  15.             log.error(inFile + "转换失败,请重试",e);
  16.             throw new Exception(e.getMessage());
  17.         }
  18.     }
复制代码
8.pdf转图片:
[code]public static long pdfToPng(String inFile, List 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
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

干翻全岛蛙蛙

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表