Java使用Aspose组件进行多文档间的转换操作
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java使⽤Aspose组件进⾏多⽂档间的转换操作
⾸先,祝⼤家新年快乐,2019诸事顺利,很久没有更新博客,今天要给⼤家说的是 ”Aspose“ 组件,作为2019年第⼀篇博客,希望⼤家能够多多⽀持,2019年要继续加油。
Aspose.Total是Aspose公司旗下的最全的⼀套office⽂档管理⽅案,主要提供.net跟java两个开发语⾔的控件套包,通过它,我们可以有计划地操纵⼀些商业中最流⾏的⽂件格
式:Word, Excel, PowerPoint, Project,等office⽂档以及PDF⽂档。
除了强⼤的⽂件操纵组件之外,Aspose.Total 还提供了⽤于制图、写电⼦邮件、拼写检查、创建条形码、⽣
成ad hoc 查询、重现格式以及⼯作流等组件,运⽤它我们可以整理⼀个完整的⽂档管理⽅案。
包含的功能:
Aspose.Word:
Aspose.Words是⼀款先进的类库,通过它可以直接在各个应⽤程序中执⾏各种⽂档处理任务。
Aspose.Words⽀持DOC,OOXML,RTF,HTML,OpenDocument, PDF,
XPS, EPUB和其他格式。
使⽤Aspose.Words,您可以⽣成,更改,转换,渲染和打印⽂档⽽不使⽤Microsoft Word。
Aspose.Cell:
Aspose.Cells是⼀个⼴受赞誉的电⼦表格组件,⽀持所有Excel格式类型的操作,⽤户⽆需依靠Microsoft Excel也可为其应⽤程序嵌⼊读写和处理Excel数据表格的功能。
Aspose.Cells可以导⼊和导出每⼀个具体的数据,表格和格式,在各个层⾯导⼊图像,应⽤复杂的计算公式,并将Excel的数据保存为各种格式等等---完成所有的这⼀切功
能都⽆需使⽤Microsoft Excel 和Microsoft Office Automation。
Aspose.PDF:
Aspose.Pdf是⼀个PDF⽂档创建组件,可以帮助⽤户⽆需使⽤Adobe Acrobat 即可读写和操作PDF⽂件。
Aspose.Pdf丰富功能:PDF⽂档压缩选项,表格创建与操作,图
表⽀持,图像功能,丰富的超链接功能,扩展的安全性组件以及⾃定义字体处理。
Aspose.BarCode:
Aspose.BarCode是⼀个功能强⼤,且稳健的条形码⽣成和识别组件。
Aspose.Slide:
Aspose.Slides是⼀个独特的可⽤于PowerPoint管理的控件,⽤户⽆需使⽤Microsoft PowerPoint即可在应⽤程序中对Microsoft PowerPoint⽂件进⾏读写以及操作。
Aspose.Slides是第⼀个能在⽤户的应⽤程序中对PowerPoint⽂档进⾏管理的组件。
Aspose.Task:
Aspose.Tasks 是⼀个⾮图形的.NET 项⽬管理组件,使.NET应⽤程序可以阅读以及撰写、管理项⽬⽂档时⽆须使⽤Microsoft Project。
使⽤Aspose.Tasks 你可以阅读和改
变任务,重现任务,资源,资源分配,关系和⽇历。
Aspose.Tasks 是⼀个提供稳定性和灵活性的⾮常成熟的产品。
我们今天重点讲解的是前⾯3个,Aspose.Words、Aspose.Cells、Aspose.Pdf,利⽤这三个组件分别实现Word转Pdf、Excel转Pdf、以及多个Pdf合并为⼀个Pdf的功能,下⾯上
⼲货。
Jar包及License获取⽅式:
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* Created with IntelliJ IDEA
* Created By 顾东城
* Date: 2019/2/21
* Time: 14:01
* Desc: 使⽤Aspose.Words将word⽂件转换为pdf⽂件
*/
public class AsposeWordTest {
/**
* 获取license
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try {
// 凭证
String license =
"<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Total for Java</Product>\n" +
" <Product>Aspose.Words for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>20991231</LicenseExpiry>\n" +
" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
" </Data>\n" +
" <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHY "</License>";
InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
License asposeLic = new License();
asposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* word 转 pdf
* @param docPath 要转换的word⽂件路径
* @param pdfPath 转换完成后输出的pdf⽂件路径
*/
public static void doc2pdf(String docPath,String pdfPath) {
if (!getLicense()) {
return;
}
try {
Document convertDoc = new Document(new FileInputStream(docPath));
convertDoc.save(pdfPath, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//在硬盘直接创建⼀个空⽩pdf即可
AsposeWordTest.doc2pdf("H:/test.docx","H:/testDoc.pdf");
}
}
import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* Created with IntelliJ IDEA
* Created By 顾东城
* Date: 2019/2/22
* Time: 9:01
* Desc: 使⽤Aspose.Cells将excel⽂件转换为pdf⽂件
*/
public class AsposeCellTest {
/**
* 获取license
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try {
// 凭证
String license =
"<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Total for Java</Product>\n" +
" <Product>Aspose.Words for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>20991231</LicenseExpiry>\n" +
" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
" </Data>\n" +
" <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHY "</License>";
InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
License asposeLic = new License();
asposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* excel 转 pdf
* @param excelPath 要转换的excel⽂件路径
* @param pdfPath 转换完成后输出的pdf⽂件路径
*/
public static void excel2pdf(String excelPath,String pdfPath) {
if (!getLicense()) {
return;
}
try {
Workbook convertExcel = new Workbook(new FileInputStream(excelPath));
convertExcel.save(pdfPath, SaveFormat.PDF);
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//在硬盘直接创建⼀个空⽩pdf即可
AsposeCellTest.excel2pdf("H:/test.xlsx","H:/testExcel.pdf");
}
}
import com.aspose.pdf.Document;
import com.aspose.pdf.License;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
/**
* Created with IntelliJ IDEA
* Created By 顾东城
* Date: 2019/2/22
* Time: 9:24
* Desc: 使⽤Aspose.Pdf将多个pdf合并为⼀个pdf⽂件
*/
public class AsposePdfTest {
/**
* 获取license
*
* @return
*/
private static boolean getLicense() {
boolean result = false;
try {
// 凭证
String license =
"<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Total for Java</Product>\n" +
" <Product>Aspose.Words for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>20991231</LicenseExpiry>\n" +
" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
" </Data>\n" +
" <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHY "</License>";
InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
License asposeLic = new License();
asposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
*
* @param pdfPaths 要合并的pdf⽂件路径
* @param mergeSavePath 最终合并完成输出的pdf⽂件路径
*/
public static void pdfMergePdf(List<String> pdfPaths,String mergeSavePath){
try {
Document mergeDocument = new Document();
for (String pdfPath : pdfPaths) {
Document document = new Document(new FileInputStream(pdfPath));
mergeDocument.getPages().add(document.getPages());
}
mergeDocument.save(mergeSavePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//将需要合并的pdf⽂件放⼊list
List<String> pdfPaths = Arrays.asList("H:/testDoc.pdf","H:/testExcel.pdf");
//在硬盘直接创建⼀个空⽩pdf即可
AsposePdfTest.pdfMergePdf(pdfPaths,"H:/merge.pdf");
}
}
注意事项:
1. Aspose.Words、Aspose.Cells、Aspose.Pdf 三个Jar包中都有Document、License、SaveFormat类,如果Idea或者Eclipse控制台没有报错,但是打开PDF提⽰PDF格式
错误或者⽂件损坏的话,请检查⼀下导包是否正确。
2. 获取License⽹上也有通过类加载器获取license.xml⽅式获取license,但是我尝试很多次都是报空指针异常,所以采⽤了直接使⽤字符串拼接的⽅式,如果各位有更好的办
法可以直接留⾔。
3. 博客仅作为博主学习记录,交流使⽤,如果有不正确的地⽅,请在评论区留⾔指正。