Aspose简介
Aspose.Total是Aspose公司旗下全套文件格式处理解决方案,提供最完整、最高效的文档处理解决方案集,无需任何其他软件安装和依赖。主要提供.net、java、C++d三个开发语言的工具包,通过它,可以对办公文档格式的转换和文档内容的在线编辑,如:Word, Excel, PowerPoint, PPT,图片,PDF等文档。 另外,Aspose.Total 还提供了用于写邮件、拼写检查、创建条形码、OCR和3D等等。
使用样例(22.3版本)
以下列举几种常用的文档格式转换样例,对于Aspose.Total来说,这几个功能只是它的冰山一角。
1.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());
- }
- }
复制代码 2.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());
- }
- }
复制代码 3.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());
- }
- }
复制代码 4.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();
- }
复制代码 5.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());
- }
- }
复制代码 6.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();
- }
复制代码 7.odt转pdf:- public static long odtToPdf(String inFile, String outFile) throws Exception {
- if (!com.yrnet.transfer.business.transfer.file.License.getWordLicense()) {
- return 0;
- }
- try {
- long old = System.currentTimeMillis();
- Document doc = new Document(inFile);
- doc.save(outFile);
- long fileSize = new File(outFile).length();
- long now = System.currentTimeMillis();
- log.info("target file size:{}",fileSize);
- Out.print(inFile, outFile, now, old);
- return fileSize;
- } catch (Exception e) {
- log.error(inFile + "转换失败,请重试",e);
- throw new Exception(e.getMessage());
- }
- }
复制代码 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 |