由于word是流格式(flow format),所以利用传统的api比如poi,无法直接操控页面。
经过俺的一番折腾,终于找到一个方法——利用ASPOSE.WORD这个接口。
然而,这个接口不开源,需要license。因此,建议有条件的还是支持正版。
如果需要该jar包请留下邮箱,随缘发送,某鱼也能获取。
1.导包。由于我是外部获取的第三方jar包,maven打包需要注意配置系统路径<systemPath>,记得帮第三方jar包放在工程的src/main/resources下。
<dependency>
<groupId>com.liam</groupId>
<artifactId>aspose-word</artifactId>
<!--依赖范围-->
<scope>system</scope>
<version>1.0-SNAPSHOT</version>
<!--依赖所在位置-->
<systemPath>${project.basedir}/src/main/resources/aspose-words-21.1-jdk17.jar</systemPath>
</dependency>
同时,springboot打包插件,也要注意配置:
<build>
<!-- 打出来的jar包名 -->
<finalName>Facepage-Tool</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<!-- 主启动类全类名 -->
<mainClass>com.Liam.FxDemoMain</mainClass>
<!-- 这个配置很重要,是将外部依赖打包进来的,如果没有该配置,打出来的jar包是不包含外部jar包的 -->
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<!--自定义 id -->
<id>repackage</id>
<!--插件目标 -->
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
以下是主要代码,实现“封面多文件替换”:
思路:
1.将一个.docx的文件封面提取
2.对被需要替换的多个.docx文件的第一页删除
3.将两个文件拼接
#注意,Document类用的是aspose的,别用成w3c的或者poi的
package com.Liam;
//关键Jar包
import com.aspose.words.Document;
import com.aspose.words.ImportFormatMode;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
//该代码主要实现封面的替换
//1.将一个.docx的文件封面提取
//2.对待替换的多个.docx文件的第一页删除
//3.将两个文件拼接
public class AsposeDemo {
public static HashSet<String> errorMsg = new HashSet<>();
public static HashSet<String> infoMsg = new LinkedHashSet<>();
private File faceFile;
private List<File> tagetFiles=new ArrayList<>();
public static void main(String[] args) throws Exception {
String path = "C:\\Users\\kl\\Desktop\\20240412 wordTool\\srcMulti";
new AsposeDemo().replaceFacePage(path);
}
public void replaceFacePage(String path) throws Exception {
//collect files
collectFiles(path);
//replacing start
replaceFaceHandle(path);
}
public void replaceFaceHandle(String path) throws Exception {
infoMsg.add("start replacing...");
for (int i=0;i<tagetFiles.size();i++){
File targetFile = tagetFiles.get(i);
//get Face
Document facePageDoc = new Document(faceFile.getAbsolutePath());
Document facePage = facePageDoc.extractPages(0, 1);
int pageCount1 = facePage.getPageCount();
//peel target face
Document target = new Document(targetFile.getAbsolutePath());
int pageCount = target.getPageCount();
Document tailPages = target.extractPages(1,pageCount-1);
//merge
File outputDir = new File(path+"\\Output Files");
if (!outputDir.exists())outputDir.mkdir();
facePage.appendDocument(tailPages , ImportFormatMode.KEEP_DIFFERENT_STYLES);
facePage.save(outputDir.getAbsolutePath()+"\\"+targetFile.getName());
// msg
infoMsg.add("meger success:"+targetFile.getName());
}
}
private void collectFiles(String path) {
File file = new File(path);
for (File dir : file.listFiles()) {
if(dir.isDirectory()){
String dirName = dir.getName();
infoMsg.add("start collecting...");
for (File taget : dir.listFiles()) {
String filename = taget.getName();
if (filename.endsWith(".docx")||filename.endsWith(".doc")){
if ("Face-page file".equals(dirName)){
faceFile = taget;
infoMsg.add("Face page is:"+faceFile.getName());
}else if("Taget-page files".equals(dirName)){
tagetFiles.add(taget);
infoMsg.add("target file is:"+taget.getName());
}
}
}
}
}
infoMsg.add("collecting finished...\r\n");
}
}
最终,还做了个基于javaFX的UI,需要的话可以去git上参考:github