😊 @ 作者: 一恍过去
🎊 @ 社区: Java技术栈交流
🎉 @ 主题: Java-Aspose实现Word文字替换(本地储存或浏览器下载)
⏱️ @ 创作时间: 2022年04月23日
目录
- 前言
- 1、加载Aspose包
- 2、配置license
- 3、实现
前言
Aspose.Words 是一个用于 Java 平台的强大的文档处理库,它允许开发人员在他们的 Java 应用程序中创建、编辑、转换和呈现 Word 文档。以下是 Aspose.Words 的一些主要功能和用途:
创建和编辑 Word 文档:
Aspose.Words 提供了一套丰富的 API,可以创建新的 Word 文档并对其进行编辑。您可以添加和格式化文本、插入表格、插入图片和其他图形,设置页面布局和格式等。文档转换:
Aspose.Words 允许您将 Word 文档转换为其他常见的文件格式,如 PDF、HTML、纯文本、图像等。同样,您也可以将其他格式的文档转换为 Word 格式。模板处理:
Aspose.Words 支持使用模板来创建和填充 Word 文档。您可以将模板与数据源结合使用,动态生成包含可变内容的文档,例如合同、报告和邮件。文档操作:
Aspose.Words 提供了丰富的功能来处理文档,如提取文本、插入页眉和页脚、合并和拆分文档、查找和替换文本等。格式化和样式:
您可以使用 Aspose.Words 设置字体、段落和表格的格式,应用样式和主题,控制页眉和页脚,创建目录和索引等。
本文使用 Aspose
来实现 Word 文档中文字替换,基本流程如下:
- 加载 Word 文档。
Document doc = new Document(inputFilePath);
- 执行文字替换操作。
doc.getRange().replace("要替换的文本", "替换后的文本", new FindReplaceOptions());
- 保存修改后的 Word 文档。
doc.save(outputFilePath);
1、加载Aspose包
1、下载:
Aspose官网没有提供相应的maven地址,所有手动引入jar包:
aspose-words-18.10-jdk16.jar
下载地址:
2、配置lib目录:
在项目的resources
目录下,创建lib
目录,并且将下载的jar包
放入其中
3、引入pom:
引入自定义配置的maven坐标:
<dependencys>
<dependency>
<groupId>com.aspose.words</groupId>
<artifactId>aspose-words</artifactId>
<version>words-18.10-jdk16</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/aspose-words-18.10-jdk16.jar</systemPath>
</dependency>
</dependencys>
2、配置license
在resources
目录下创建license.xml
文件,代码如下:
<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>
3、实现
代码:
@Controller
@RequestMapping("/aspose")
public class AsposeController {
@RequestMapping("/replaceByString")
@ResponseBody
public static void replaceByString() {
try {
if (!getLicense()) {
throw new RuntimeException("文件替换失败!");
}
FindReplaceOptions options = new FindReplaceOptions();
// 是否区分大小写
options.setMatchCase(false);
// 方式一:直接指定原word位置
Document document = new Document("C:\\Users\\LiGezZ\\Desktop\\test.docx");
/*方式二:
*如果原word在项目的resources目录下,可以使用ClassPathResource进行加载
String path = "/word/test.docx";
FileInputStream inputStream = new FileInputStream(new ClassPathResource(path).getFile());
Document document = new Document(inputStream);
*/
document.getRange().replace("${name}", "文件替换有限公司", options);
// 存储方式一:将提交后的文件输出到指定目录,也可以直接在浏览器进行下载不进行本地储存
File outFile = new File("C:\\Users\\LiGezZ\\Desktop\\testReplace.docx");
try (FileOutputStream fos = new FileOutputStream(outFile )) {
// 输出方式一:输出为word
document.save(fos, SaveFormat.WORD_ML);
// 输出方式二:输出为PDF,需要将输出文件的后缀修改为.pdf,比如:testReplace.pdf
// document.save(fos, SaveFormat.PDF);
/* 存储方式二:
* 如果是输出到浏览器直接下载
* 那么将FileOutputStream fos = new FileOutputStream(pdfFile)替换为
* ByteArrayOutputStream fos = new ByteArrayOutputStream()
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
byte[] buffer = fos.toByteArray();
InputStream arrayInputStream = new ByteArrayInputStream(buffer);
byte[] buf = new byte[4096];
int len = -1;
while ((len = arrayInputStream.read(buf)) != -1) {
response.getOutputStream().write(buf, 0, len);
}
*/
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("文件替换失败!");
}
}
// 验证aspose文件
private static boolean getLicense() {
boolean result = false;
try (InputStream in = Doc2PdfUtil.class.getClassLoader()
.getResourceAsStream("license.xml")) {
// 需要引入com.aspose.words.License的包;
License license = new License();
license.setLicense(in);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
测试:
比如浏览器请求接口:http://localhost:9898/aspose/replaceByString