Aspose.Total是Aspose公司旗下的最全的一套office文档管理方案,主要提供.net跟java两个开发语言的控件套包,通过它,我们可以有计划地操纵一些商业中最流行的文件格式:Word, Excel, PowerPoint, Project,等office文档以及PDF文档。如需下载aspose.total官方网址:https://downloads.aspose.com/total/
aspose下载或许需要一些条件。以下附上早期版链接:https://pan.baidu.com/s/1u4Q2DHiw-6GAPo-Jj8rTGQ
提取码:cwk5
package test;
import java.io.File;
import java.io.FileOutputStream;
import com.aspose.pdf.DocSaveOptions;
import com.aspose.pdf.Document;
import com.aspose.pdf.ExcelSaveOptions;
import com.aspose.pdf.PptxSaveOptions;
import com.aspose.pdf.SaveFormat;
public class PdfToOthers {
WordToOthers conv=new WordToOthers();
fileDomainVO vo=new fileDomainVO();
public fileDomainVO PdfToOthers(fileDomainVO vo) {
vo.setStatus(Details.FailStatus);
// if(!conv.getLicense()) {
// System.out.println("验证失败,产生水印!");
// }
Document doc=null;
try {
//统计时间
long old = System.currentTimeMillis();
doc=new Document(vo.getInputfile());
//创建一个空白文件夹
File file=null;
FileOutputStream os=null;
file = new File(vo.getOutputfile());
//创建文件夹
file.mkdirs();
file = new File(vo.getOutputfile()+vo.getFileNameAfter());
os = new FileOutputStream(file);
if(vo.getDetails().equals(Details.PdfToDocx))
{
DocSaveOptions saveOptions = new DocSaveOptions();
saveOptions.setFormat(DocSaveOptions.DocFormat.DocX);
doc.save(os,SaveFormat.DocX);
System.out.println("转换完成!");
}
else if(vo.getDetails().equals(Details.PdfToExcel)) {
ExcelSaveOptions saveOptions = new ExcelSaveOptions();
doc.save(os,saveOptions);
System.out.println("转换完成");
}
else if(vo.getDetails().equals(Details.PdfToPpt)) {
PptxSaveOptions saveoptions=new PptxSaveOptions();
doc.save(os,saveoptions);
System.out.println("转换完成");
}
long now = System.currentTimeMillis();
//System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"+"文件保存在:" + vo.getOutputfile());
//设置时间
vo.setTimeConsuming((((now - old) / 1000.0)+"").trim());
//成功
vo.setStatus(Details.SuccessStatus);
}catch(Exception e) {
e.printStackTrace();
}
return vo;
}
}
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import junit.framework.Test;
public class WordToOthers {
/**
* 验证License
* @return boolean
*/
public static boolean getLicense() {
boolean result = false;
try {
//license.xml
//如果不知道应该放在哪个路径,就在控制台打印一下,然后去放就好啦
// System.out.println(Test.class.getClassLoader().getResource("").getPath());
InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* word转换
* 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
* @param
*/
public fileDomainVO WordToOthers(fileDomainVO vo) {
//默认失败
vo.setStatus(Details.FailStatus);
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
System.out.println("验证失败,会产生水印");
}
//创建一个doc对象
Document doc =null;
try {
//统计时间
long old = System.currentTimeMillis();
//初始化定义
File file =null;
ImageSaveOptions iso = null;
//输出流
FileOutputStream os =null;
//判断是否转换图片
if(vo.getDetails().equals(Details.DocToJPEG)){
iso = new ImageSaveOptions(SaveFormat.JPEG);
}else{
//file.mkdirs();
//新建一个空白文档
file = new File(vo.getOutputfile());
//创建文件夹
file.mkdirs();
file = new File(vo.getOutputfile()+vo.getFileNameAfter());
os = new FileOutputStream(file);
}
//getInputfile是将要被转化文档
doc = new Document(vo.getInputfile());
//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
//------------------------------------------逻辑判断
//>>>>>>DocToPDF
if(vo.getDetails().equals(Details.DocToPDF)){
doc.save(os, SaveFormat.PDF);
}//>>>>>>DocToDocx
else if(vo.getDetails().equals(Details.DocToDocx)){
doc.save(os, SaveFormat.DOCX);
}//>>>>>>DocToTexT
else if(vo.getDetails().equals(Details.DocToTexT)){
doc.save(os, SaveFormat.TEXT);
}//>>>>>>DocToXps
else if(vo.getDetails().equals(Details.DocToXps)){
doc.save(os, SaveFormat.XPS);
}//>>>>>>HtmlToDoc
else if(vo.getDetails().equals(Details.HtmlToDoc)){
doc.save(os, SaveFormat.DOC);
}//>>>>>>DocToJPEG
else if(vo.getDetails().equals(Details.DocxToDoc)) {
doc.save(os,SaveFormat.DOC);
}
else if(vo.getDetails().equals(Details.DocxToPDF)) {
doc.save(os,SaveFormat.PDF);
}
else if(vo.getDetails().equals(Details.DocToJPEG)){
iso.setPrettyFormat(true);
iso.setUseAntiAliasing(true);
for (int i = 0; i < doc.getPageCount(); i++)
{
iso.setPageIndex(i);
doc.save(vo.getOutputfile() + vo.getFileNameAfter() +"---"+(i+1)+ ".jpeg", iso);
}
}
//------------------------------------------逻辑判断
//统计时间
long now = System.currentTimeMillis();
//System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"+"文件保存在:" + vo.getOutputfile());
//设置时间
vo.setTimeConsuming((((now - old) / 1000.0)+"").trim());
//成功
vo.setStatus(Details.SuccessStatus);
} catch (Exception e) {
e.printStackTrace();
}
return vo;
}
}
package test;
import java.sql.Date;
public class fileDomainVO {
//创建fileDomainVO实体类
//复制好后,记得添加set,get方法
private Integer id;
private Date time; //操作时间
private String filename; //原文件名
private String fileNameAfter; //转换后文件名
private String filesize; //原文件大小
private String filetype; //原文件类型
private String filetypeafter; //后文件类型
private String details; //操作详情
private String outputfile; //保存路径
private String inputfile; //原文件路径
private Integer pageno;
private Integer pages;
private Integer rid;
private Integer pageoperation;
private Integer pagestart;
private Integer pageend;
private String fileSizeAfter; //后 文件大小
private Integer status; //操作状态
private Integer afterpages;
private String detail;
private String timeConsuming; //消耗时长
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getFileNameAfter() {
return fileNameAfter;
}
public void setFileNameAfter(String fileNameAfter) {
this.fileNameAfter = fileNameAfter;
}
public String getFilesize() {
return filesize;
}
public void setFilesize(String filesize) {
this.filesize = filesize;
}
public String getFiletype() {
return filetype;
}
public void setFiletype(String filetype) {
this.filetype = filetype;
}
public String getFiletypeafter() {
return filetypeafter;
}
public void setFiletypeafter(String filetypeafter) {
this.filetypeafter = filetypeafter;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
public String getOutputfile() {
return outputfile;
}
public void setOutputfile(String outputfile) {
this.outputfile = outputfile;
}
public String getInputfile() {
return inputfile;
}
public void setInputfile(String inputfile) {
this.inputfile = inputfile;
}
public Integer getPageno() {
return pageno;
}
public void setPageno(Integer pageno) {
this.pageno = pageno;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
public Integer getRid() {
return rid;
}
public void setRid(Integer rid) {
this.rid = rid;
}
public Integer getPageoperation() {
return pageoperation;
}
public void setPageoperation(Integer pageoperation) {
this.pageoperation = pageoperation;
}
public Integer getPagestart() {
return pagestart;
}
public void setPagestart(Integer pagestart) {
this.pagestart = pagestart;
}
public Integer getPageend() {
return pageend;
}
public void setPageend(Integer pageend) {
this.pageend = pageend;
}
public String getFileSizeAfter() {
return fileSizeAfter;
}
public void setFileSizeAfter(String fileSizeAfter) {
this.fileSizeAfter = fileSizeAfter;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getAfterpages() {
return afterpages;
}
public void setAfterpages(Integer afterpages) {
this.afterpages = afterpages;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getTimeConsuming() {
return timeConsuming;
}
public void setTimeConsuming(String timeConsuming) {
this.timeConsuming = timeConsuming;
}
}
package test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import junit.framework.Test;
public class ExcleToPdf {
/**
* 验证License
* @return boolean
*/
public static boolean getLicense() {
boolean result = false;
try {
//license.xml
//如果不知道应该放在哪个路径,就在控制台打印一下,然后去放就好啦
// System.out.println(Test.class.getClassLoader().getResource("").getPath());
InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
*
* @param excel转换
* @return
*/
public fileDomainVO excelToPdf(fileDomainVO vo){
vo.setStatus(Details.FailStatus);
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
System.out.println("验证失败,会产生水印");
return vo;
}try {
//统计时间
long old=System.currentTimeMillis();
//创建一个空白文件夹
File file=null;
Workbook wb=null;
FileOutputStream os=null;
file = new File(vo.getOutputfile());
//创建文件夹
file.mkdirs();
file = new File(vo.getOutputfile()+vo.getFileNameAfter());
os = new FileOutputStream(file);
System.out.println(file);
System.out.println(os);
wb=new Workbook(vo.getInputfile());
if(vo.getDetails().equals(Details.ExcelToPDF)) {
System.out.println(vo.getDetails().equals(Details.ExcelToPDF));
wb.save(os,SaveFormat.PDF);
System.out.println(com.aspose.cells.SaveFormat.PDF);
os.close();
}
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"+"文件保存在:" + vo.getOutputfile()+vo.getFileNameAfter());
//设置时间
vo.setTimeConsuming((((now - old) / 1000.0)+"").trim());
//成功
vo.setStatus(Details.SuccessStatus);
}catch(Exception e){
e.printStackTrace();
}
return vo;
}
}
package test;
public class Main {
public static void main(String[] args) throws Exception {
fileDomainVO vo = new fileDomainVO();
WordToOthers Conv=new WordToOthers();
ExcleToPdf extp=new ExcleToPdf();
PdfToOthers pdfto=new PdfToOthers();
/**
* 进行PDF转PPT格式测试
* 测试通过
*/
// vo.setDetails(Details.PdfToPpt);
// vo.setInputfile("d:/test/2019.pdf");
// vo.setOutputfile("d:/test/");
// vo.setFileNameAfter("exclename.pptx");
// /**
// * 进行PDF转docx格式测试
// * 测试通过
// */
// vo.setDetails(Details.PdfToDocx);
// vo.setInputfile("E:/myfiles/bigdata/个人学习/Hive编程指南.pdf");
// vo.setOutputfile("E:/myfiles/bigdata/个人学习/");
// vo.setFileNameAfter("Hive.docx");
// pdfto.PdfToOthers(vo);
/**
* 进行word转PDF测试
* 测试通过
// */
// vo.setDetails(Details.DocxToPDF); //进行word转pdf
// vo.setInputfile("D:/test/name.doc"); //需要转换的word
// vo.setOutputfile("D:/test/"); //保存路径
// vo.setFileNameAfter("name.pdf");
// Conv.WordToOthers(vo); //开始转换
/**
* 进行excel转PDF测试
* 测试通过
*/
// vo.setDetails(Details.ExcelToPDF);//进行excel转PDF
// vo.setInputfile("D:/test/123.xlsx"); //需要转换的word
// vo.setOutputfile("D:/test/"); //保存路径
// vo.setFileNameAfter("1.pdf");
// extp.excelToPdf(vo);
/**
* 进行PDF转Word测试
* 使用PDFbox包能将PDF上的文字全部转换为Word但是不能保持原有样式
* 测试通过
* 不推荐使用
*/
vo.setInputfile("E:/myfiles/bigdata/个人学习/Hadoop权威指南_第四版_中文版.pdf"); //需要转换的word
vo.setOutputfile("E:/myfiles/bigdata/个人学习/"); //保存路径
vo.setFileNameAfter("HADOOP权威.doc"); //转换后的文件名,自己取
}
}
package test;
public class Details {
public static Integer SuccessStatus=0; //状态:成功
public static Integer FailStatus=1; //状态:失败
public static String DocToPDF="DocToPDF";
public static String DocToDocx="DocToDocx";
public static String DocToTexT="DocToTexT";
public static String DocToXps="DocToXps";
public static String HtmlToDoc="HtmlToDoc";
public static String DocToJPEG="DocToJPEG";
public static String DocxToDoc="DocxToDoc";
public static String DocxToPDF="DocxToPDF";
public static String ExcelToPDF="ExcelToPDF";
// PDF转换为其他的格式定义
public static String PdfToDocx="PdfToDocx";
public static String PdfToExcel="PdfToExcel";
public static String PdfToPpt="PdfToPpt";
public static String PdfTojpg="PdfTojpg";
}
Aspose.Total for Java
Aspose.Words for Java
Enterprise
20991231
20991231
8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7
sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
以上测试基本通过,转换时如是PDF转Word如不需图片之类用PDFbox包即可解决,且效果不错,有其他参数参与的用aspose.Words效果较好。使用转换图片类型的PDF文件效果较差,不推荐。