1、导入所需依赖 aspose-words-15.8.0-jdk16.jar 下载
<dependency>
<groupId>com.aspose</groupId>
<artifactId>com-aspose</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
2、编写 license.xml license.xml授权码下载
备注:将license.xml放到resources下
<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>
授权码
</Signature>
</License>
3、所需加载license.xml方法
/**
* <p>
* 服务初始化之后,执行方法
* </p>
*
* @author AJIE
* @description
* @date 2022/03/02
*/
@Slf4j
@Component
public class StartAppRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
log.info("《服务初始化执行处理》 start...");
try {
log.info("实现`aspose-words`授权 -> 去掉头部水印");
/*
实现匹配文件授权 -> 去掉头部水印 `Evaluation Only. Created with Aspose.Words. Copyright 2003-2018 Aspose Pty Ltd.` |
`Evaluation Only. Created with Aspose.Cells for Java. Copyright 2003 - 2020 Aspose Pty Ltd.`
*/
InputStream is = new ClassPathResource("license.xml").getInputStream();
License license = new License();
license.setLicense(is);
} catch (Exception e) {
log.error("《`aspose-words`授权》 失败: {}", e.getMessage());
}
log.info("《服务初始化执行处理》 end...");
}
}
4、word转pdf工具类
/**
* <p>
* word 转 pdf 工具类
* </p>
*/
@Slf4j
public class Word2PdfUtil {
/**
* `word` 转 `pdf`
*
* @param wordBytes: word字节码
* @return 生成的`pdf`字节码
* @author li
* @date 2022/03/02
*/
@SneakyThrows(Exception.class)
public static byte[] wordBytes2PdfBytes(byte[] wordBytes) {
Document document = new Document(new ByteArrayInputStream(wordBytes));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
document.save(outputStream, SaveFormat.PDF);
// 返回生成的`pdf`字节码
return outputStream.toByteArray();
}
/**
* `word` 转 `pdf`
*
* @param wordBytes: word字节码
* @param pdfFilePath: 需转换的`pdf`文件路径
* @return 生成的`pdf`文件数据
* @author li
* @date 2022/03/02
*/
@SneakyThrows(Exception.class)
public static File wordBytes2PdfFile(byte[] wordBytes, String pdfFilePath) {
Document document = new Document(new ByteArrayInputStream(wordBytes));
document.save(pdfFilePath, SaveFormat.PDF);
return new File(pdfFilePath);
}
/**
* 将文件转换成byte数组
* @param filePath 文件File类 通过new File(文件路径)
* @return byte数组
*/
public static byte[] File2byte(File filePath) {
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(filePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
//将文件转换成Byte数组
public static byte[] getBytesByFile(String pathStr) {
File file = new File(pathStr);
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
byte[] data = bos.toByteArray();
bos.close();
return data;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将Byte数组转换成文件
* @param bytes 字节码
* @param filePath 文件路径
* @param fileName 文件名
*/
public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath + "\\" + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
注意:启动后如下图为成功!

制作不易,麻烦给个小赞 (如要转载,请备注出处)
2318

被折叠的 条评论
为什么被折叠?



