注:引入的依赖:
<!-- word、excel转pdf依赖 -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>14.9.0</version>
<classifier>jdk16</classifier>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
一、word转pdf(inPath为原文档路径)
public static String doc2pdf(String inPath) { FileOutputStream os = null; String pdfPath = ""; try { File inputFile = new File(inPath); if (!inputFile.exists()) { LOGGER.error("源文件不存在!path:" + inPath); return null; } //获取pdf路径 pdfPath = getPdfPath(inputFile); // 新建一个空白pdf文档 File file = new File(pdfPath); os = new FileOutputStream(file); Document doc = new Document(inPath); insertWatermarkText(doc); // 保存pdf文件 doc.save(os, SaveFormat.PDF); } catch (Exception e) { e.printStackTrace(); } return pdfPath; } private static void insertWatermarkText(Document doc) throws Exception { Paragraph watermarkPara = new Paragraph(doc); for (Section sect : doc.getSections()) { insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY); insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST); insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN); } } private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception { HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType); if (header == null) { header = new HeaderFooter(sect.getDocument(), headerType); sect.getHeadersFooters().add(header); } header.appendChild(watermarkPara.deepClone(true)); } 二、excel转pdf(inPath为原文档路径) public static String excel2Pdf(String inPath) { //去水印 if (!getLicense()){ return null; } FileOutputStream out = null; InputStream in = null; String pdfPath = ""; try { File inputFile = new File(inPath); if (!inputFile.exists()) { LOGGER.error("源文件不存在!path:" + inPath); return null; } //获取pdf路径 pdfPath = getPdfPath(inputFile); in = new FileInputStream(inputFile); // 新建一个空白pdf文档 File pdfFile = new File(pdfPath); out = new FileOutputStream(pdfFile); Workbook wb = new Workbook(in); PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); //sheet页所有列都在同一页 pdfSaveOptions.setAllColumnsInOnePagePerSheet(true); //是否单页 pdfSaveOptions.setOnePagePerSheet(false); //单页,根据要求选择是否单页 //pdfSaveOptions.setOnePagePerSheet(true); wb.save(out, pdfSaveOptions); out.close(); }catch (Exception e) { e.printStackTrace(); } return pdfPath; } 三、去水印 /** * 设置并验证License,去水印 */ private static boolean getLicense() { boolean result = false; try { InputStream license = FileToPdfUtil.class.getClassLoader().getResourceAsStream("license.xml"); com.aspose.cells.License aposeLic = new com.aspose.cells.License(); aposeLic.setLicense(license); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } 四、公用方法 /** * 获取pdf路径 */ private static String getPdfPath(File file) { String filePath = file.getAbsolutePath(); // 源文件名 String fileName = file.getName(); String suffix = fileName.substring(fileName.lastIndexOf(".")); String pdfName = fileName.replace(suffix, ".pdf"); return filePath.replace(fileName, pdfName); } 五、license.xml文件(文件可放在resources目录下,内容网上找的,稍作修改) <License> <Data> <Products> <Product>Aspose.Total 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>
-----个人实践,亲测有效