Android實(shí)現(xiàn)excel/pdf/word/odt/圖片相互轉(zhuǎn)換
實(shí)踐過(guò)程
pdf轉(zhuǎn)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轉(zhuǎn)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轉(zhuǎn)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轉(zhuǎn)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轉(zhuǎn)word
public static long pdfToDoc(String inFile, String outFile) { if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) { return 0; } log.info("開始轉(zhuǎn)換..."); 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("轉(zhuǎn)換結(jié)束..."); return new File(outFile).length(); }
word轉(zhuǎn)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 + "轉(zhuǎn)換失敗,請(qǐng)重試",e); throw new Exception(e.getMessage()); } }
excel轉(zhuǎn)圖片
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轉(zhuǎn)圖片
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轉(zhuǎn)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()); } }
以上就是Android實(shí)現(xiàn)excel/pdf/word/odt/圖片相互轉(zhuǎn)換的詳細(xì)內(nèi)容,更多關(guān)于Android excel pdf word odt圖片轉(zhuǎn)換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android動(dòng)畫 實(shí)現(xiàn)開關(guān)按鈕動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)實(shí)例代碼
這篇文章主要介紹了Android動(dòng)畫 實(shí)現(xiàn)開關(guān)按鈕動(dòng)畫(屬性動(dòng)畫之平移動(dòng)畫)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11Android?內(nèi)存優(yōu)化知識(shí)點(diǎn)梳理總結(jié)
這篇文章主要介紹了Android?內(nèi)存優(yōu)化知識(shí)點(diǎn)梳理總結(jié),Android?操作系統(tǒng)給每個(gè)進(jìn)程都會(huì)分配指定額度的內(nèi)存空間,App?使用內(nèi)存來(lái)進(jìn)行快速的文件訪問(wèn)交互,長(zhǎng)時(shí)間如此便需要優(yōu)化策略,文章分享優(yōu)化知識(shí)點(diǎn)總結(jié),需要的朋友可以參考一下2022-06-06使用DrawerLayout完成滑動(dòng)菜單的示例代碼
這篇文章主要介紹了使用DrawerLayout完成滑動(dòng)菜單的示例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Android local.properties 文件讀取實(shí)例詳解
這篇文章主要介紹了Android local.properties 文件讀取實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05詳解Android Service與Activity之間通信的幾種方式
這篇文章主要介紹了詳解Android Service與Activity之間通信的幾種方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Kotlin新手基礎(chǔ)學(xué)習(xí)之Elvis操作符
Kotlin 是一種在 Java 虛擬機(jī)上運(yùn)行的靜態(tài)類型編程語(yǔ)言,被稱之為 Android 世界的Swift,由 JetBrains 設(shè)計(jì)開發(fā)并開源,下面這篇文章主要給大家介紹了關(guān)于Kotlin新手基礎(chǔ)學(xué)習(xí)之Elvis操作符的相關(guān)資料,需要的朋友可以參考下。2017-12-12Android使用GridView實(shí)現(xiàn)表格分割線效果
這篇文章主要為大家詳細(xì)介紹了Android使用GridView實(shí)現(xiàn)表格分割線效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Android ProgressDialog進(jìn)度條使用詳解
這篇文章主要對(duì)Android開發(fā)之ProgressDialog讀取文件進(jìn)度進(jìn)行解析,感興趣的朋友可以參考一下2016-02-02