Android实现excel/pdf/word/odt/图片相互转换
作者:芝麻粒儿 发布时间:2021-12-05 23:08:16
标签:Android,excel,pdf,word,odt,图片
实践过程
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


猜你喜欢
- pom.xml文件中添加如下配置项创建maven项目后,在pom.xml文件中添加如下配置项:<properties> &nbs
- 第一步:配置管理器中新建解决方案配置第二步:定义条件编译符号:第三步:在代码中使用自定义的条件编译#if CustomDebugConsol
- 本篇给大家详细讲解了MTKAndroid平台开发流程,大致分为44个步骤,我们把每个步骤的命令详细讲解了下,一起来学习下。1.拷贝代码仓库从
- private void txtBarCode_KeyPress(object sender, KeyPressEventArg
- 绑定多个按钮到同一个事件1.添加代码private void clauseElementClicked(object sender, Eve
- 前言Flutter 的画笔类 Paint 提供了很多图形绘制的配置属性,来供我们绘制更丰富多彩的图形。前面几篇我们介绍了 shader 属性
- FTPS:一种多传输协议,相当于加密版的FTP。当你在FTP服务器上收发文件的时候,你面临两个风险。第一个风险是在上载文件的时候为文件加密。
- 前言:在日常的Android开发中会经常和控件打交道,有时Android提供的控件未必能满足业务的需求,这个时候就需要我们实现自定义一些控件
- 近来关于 Kotlin 的文章着实不少,Google 官方的支持让越来越多的开发者开始关注 Kotlin。不久前加入的项目用的是 Kotli
- C# SynchronizationContext及Send和Post使用1、(SynchronizationContext)同步上下文的作
- bufferedReader.readLine()读到最后发生阻塞最近在做一个imageserver,需求简化后就是使用socket响应HT
- 本文实例讲述了spring mvc 实现获取后端传递的值。分享给大家供大家参考,具体如下:jsp页面怎么获取从后端传递过来的值?JSTL 方
- java中next与nextLine用法区别:next()一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab键或E
- 同一个service调用service本身如果同一个service调用service本身的方法,出现了事务不能控制。解决方案1.在sprin
- 1.背景 SpringBoot项目中,之前都是在controller方法的第一行手动打印 log,return之前再
- 前言最近有项目需要开发档案打包下载功能,其中包含很多大附件,项目使用minio存储且不在同一台服务器上,为了优化速度决定使用windows共
- 对于java开发人员来说,Idea的普及率已经很高了。但是还是很多好用的技巧没有用到,只是用到一些基本的功能,蛮浪费IDEA这个优秀的IDE
- 对一个对象进行属性分析,并得到相应的属性值,并判断属性的默认值以及空值 public class People
- 随着时间的推移现在的软件要求显示的内容越来越多,所以要在小的屏幕上能够更好的显示更多的内容,首先我们会想到底部菜单栏,但是有时候想网易新闻要
- 什么是继承面向对象的特征:封装:不必要公开的数据成员和方法,使用private关键字进行修饰。意义:安全性。背景代码中创建的类, 主要是为了