首先, 祝大家新年快乐, 2019 诸事顺利, 很久没有更新博客, 今天要给大家说的是 "Aspose" 组件, 作为 2019 年第一篇博客, 希望大家能够多多支持, 2019 年要继续加油.
什么是 Aspose?
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 获取方式:
链接: https://pan.baidu.com/s/1PwtvfPGLu1MIpMLNHKHTmg
提取码: 5la1
- 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/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
- "</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/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
- "</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/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
- "</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");
- }
- }
注意事项:
Aspose.Words,Aspose.Cells,Aspose.PDF 三个 Jar 包中都有 Document,License,SaveFormat 类, 如果 Idea 或者 Eclipse 控制台没有报错, 但是打开 PDF 提示 PDF 格式错误或者文件损坏的话, 请检查一下导包是否正确.
获取 License 网上也有通过类加载器获取 license.xml 方式获取 license, 但是我尝试很多次都是报空指针异常, 所以采用了直接使用字符串拼接的方式, 如果各位有更好的办法可以直接留言.
博客仅作为博主学习记录, 交流使用, 如果有不正确的地方, 请在评论区留言指正.
来源: https://www.cnblogs.com/gudongcheng/p/10417003.html