Apache POI Word书签复制:解决`CTR`类方法变更问题
Apache POI Word书签复制:解决CTR类方法变更问题
本文将指导您如何使用 Apache POI 库来复制 Word 文档中的书签内容,并解决在新版本 Apache POI 中 CTR 类方法变更导致的错误。
问题:
在使用 Apache POI 复制 Word 文档中的书签内容时,可能会遇到以下错误:
java: 找不到符号
符号: 方法 getBookmarkStartArray()
位置: 类型为org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR的变量 ctr
java: 找不到符号
符号: 方法 getBookmarkEndArray()
位置: 类型为org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR的变量 ctr
原因:
在新版本的 Apache POI 中,CTR 类不再具有 getBookmarkStartArray() 和 getBookmarkEndArray() 方法。取而代之的是 getBookmarkStartList() 和 getBookmarkEndList() 方法。
解决方案:
修改 copyBookmarks() 方法中的代码,将 getBookmarkStartArray() 和 getBookmarkEndArray() 替换为 getBookmarkStartList() 和 getBookmarkEndList():
private static void copyBookmarks(XWPFDocument sourceDocument, XWPFDocument targetDocument) {
for (XWPFParagraph paragraph : sourceDocument.getParagraphs()) {
for (XWPFRun run : paragraph.getRuns()) {
CTR ctr = run.getCTR();
// 复制书签的内容
List<CTBookmark> bookmarkStartList = ctr.getBookmarkStartList();
List<CTBookmark> bookmarkEndList = ctr.getBookmarkEndList();
if (!bookmarkStartList.isEmpty() && !bookmarkEndList.isEmpty()) {
String bookmarkName = bookmarkStartList.get(0).getName();
XWPFParagraph targetParagraph = targetDocument.createParagraph();
XWPFRun targetRun = targetParagraph.createRun();
targetRun.setText(run.getText(0));
targetRun.addBreak();
targetRun.setText(bookmarkName);
}
}
}
}
完整代码:
package word;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
public class Word {
public static void main(String[] args) {
String inputFilePath1 = "input01.docx";
String inputFilePath2 = "input02.docx";
String outputFilePath = "input04.docx";
try {
// 打开第一个输入文件
FileInputStream fis1 = new FileInputStream(inputFilePath1);
XWPFDocument document1 = new XWPFDocument(OPCPackage.open(fis1));
// 打开第二个输入文件
FileInputStream fis2 = new FileInputStream(inputFilePath2);
XWPFDocument document2 = new XWPFDocument(OPCPackage.open(fis2));
// 创建新的输出文件
XWPFDocument outputDocument = new XWPFDocument();
// 复制第一个输入文件的内容
copyBookmarks(document1, outputDocument);
// 复制第二个输入文件的内容
copyBookmarks(document2, outputDocument);
// 保存输出文件
FileOutputStream fos = new FileOutputStream(outputFilePath);
outputDocument.write(fos);
fos.close();
System.out.println("书签内容已成功复制到新文件。");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private static void copyBookmarks(XWPFDocument sourceDocument, XWPFDocument targetDocument) {
for (XWPFParagraph paragraph : sourceDocument.getParagraphs()) {
for (XWPFRun run : paragraph.getRuns()) {
CTR ctr = run.getCTR();
// 复制书签的内容
List<CTBookmark> bookmarkStartList = ctr.getBookmarkStartList();
List<CTBookmark> bookmarkEndList = ctr.getBookmarkEndList();
if (!bookmarkStartList.isEmpty() && !bookmarkEndList.isEmpty()) {
String bookmarkName = bookmarkStartList.get(0).getName();
XWPFParagraph targetParagraph = targetDocument.createParagraph();
XWPFRun targetRun = targetParagraph.createRun();
targetRun.setText(run.getText(0));
targetRun.addBreak();
targetRun.setText(bookmarkName);
}
}
}
}
}
注意:
- 确保您使用的是最新版本的 Apache POI 库。
- 这段代码仅复制书签名称。如果您需要复制书签内容,需要修改代码逻辑。
- 在运行代码之前,请确保您已准备好两个包含书签的 Word 文档(
input01.docx和input02.docx)和一个用于保存结果的空文档(input04.docx)。
原文地址: https://www.cveoy.top/t/topic/fLlD 著作权归作者所有。请勿转载和采集!