话不多说,直接上干货
// 官方文档的要求 无需理会public static boolean getLicense() {boolean result = false;try {String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());License aposeLic = new License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}
doc转PDF
/*** @param inputStream 源文件输入流* @param outputStream pdf文件输出流**/public static boolean doc2pdf(InputStream inputStream, OutputStream outputStream) {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {return false;}try {// 将源文件保存在com.aspose.words.Document中,具体的转换格式依靠里面的save方法com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换doc.save(outputStream, SaveFormat.PDF);} catch (Exception e) {e.printStackTrace();return false;}finally {if (outputStream != null) {try {outputStream.flush();outputStream.close();} catch (IOException e) {e.printStackTrace();}}}return true;}
excel转PDF
/*** @param inputStream 源文件输入流* @param outputStream pdf文件输出流**/public static boolean excelToPdf(InputStream inputStream, OutputStream outputStream) {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {return false;}try {com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(inputStream);// 原始excel路径com.aspose.cells.PdfSaveOptions pdfSaveOptions = new com.aspose.cells.PdfSaveOptions();pdfSaveOptions.setOnePagePerSheet(false);int[] autoDrawSheets={3};//当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放。autoDraw(wb,autoDrawSheets);int[] showSheets={0};//隐藏workbook中不需要的sheet页。printSheetPage(wb,showSheets);wb.save(outputStream, pdfSaveOptions);outputStream.flush();outputStream.close();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return true;}
RTF转PDF
/*** @param inputStream 源文件输入流* @param outputStream pdf文件输出流**/public static boolean rtfToPdf(InputStream inputStream, OutputStream outputStream) {// 验证License 若不验证则转化出的pdf文档会有水印产生if (!getLicense()) {return false;}try {// 将源文件保存在com.aspose.words.Document中,具体的转换格式依靠里面的save方法com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换doc.save(outputStream, SaveFormat.PDF);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return true;}
其中依赖的方法
/*** 设置打印的sheet 自动拉伸比例* @param wb* @param page 自动拉伸的页的sheet数组*/public static void autoDraw(com.aspose.cells.Workbook wb,int[] page){if(null!=page&&page.length>0){for (int i = 0; i < page.length; i++) {wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();wb.getWorksheets().get(i).getVerticalPageBreaks().clear();}}}/*** 隐藏workbook中不需要的sheet页。** @param wb* @param page 显示页的sheet数组*/public static void printSheetPage(com.aspose.cells.Workbook wb, int[] page) {for (int i = 1; i < wb.getWorksheets().getCount(); i++) {wb.getWorksheets().get(i).setVisible(false);}if (null == page || page.length == 0) {wb.getWorksheets().get(0).setVisible(true);} else {for (int i = 0; i < page.length; i++) {wb.getWorksheets().get(i).setVisible(true);}}}
其中需要依赖一些jar包,在这里