使用Apache POI 替换Word文档书签内容

本文将介绍如何使用Apache POI库的最新版本(5.2.2)替换Word文档中的书签内容。示例代码演示了将input01.docx文件里'应变计'书签的内容替换到input03.docx文件里的对应'应变计'书签位置,并将新文件命名为input04.docx保存。

步骤:

  1. 将input01.docx文件里'应变计'书签的内容替换input03.docx文件里对应的'应变计'书签。
  2. 将input02.docx文件里'位移计'书签的内容替换input03.docx文件里对应的'位移计'书签的位置。
  3. 将新文件用input04.docx命名保存。

Java代码:

import org.apache.poi.xwpf.usermodel.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class WordBookmarkReplacement {
    public static void main(String[] args) {
        replaceBookmarkContent("input01.docx", "应变计", "input03.docx", "应变计", "input04.docx");
        replaceBookmarkContent("input02.docx", "位移计", "input04.docx", "位移计", "input04.docx");
    }

    public static void replaceBookmarkContent(String sourceFilePath, String sourceBookmarkName,
                                              String targetFilePath, String targetBookmarkName,
                                              String outputFilePath) {
        try {
            // 读取源文件
            FileInputStream sourceFileInputStream = new FileInputStream(sourceFilePath);
            XWPFDocument sourceDocument = new XWPFDocument(sourceFileInputStream);

            // 读取目标文件
            FileInputStream targetFileInputStream = new FileInputStream(targetFilePath);
            XWPFDocument targetDocument = new XWPFDocument(targetFileInputStream);

            // 获取源文件中指定书签的内容
            XWPFParagraph sourceParagraph = sourceDocument.getParagraphs().stream()
                    .filter(p -> p.getCTP().getBookmarkStartList().stream()
                            .anyMatch(b -> b.getName().equals(sourceBookmarkName)))
                    .findFirst().orElse(null);

            if (sourceParagraph != null) {
                // 获取目标文件中指定书签的位置
                XWPFParagraph targetParagraph = targetDocument.getParagraphs().stream()
                        .filter(p -> p.getCTP().getBookmarkStartList().stream()
                                .anyMatch(b -> b.getName().equals(targetBookmarkName)))
                        .findFirst().orElse(null);

                if (targetParagraph != null) {
                    // 清空目标文件中指定书签位置的内容
                    for (int i = targetParagraph.getRuns().size() - 1; i >= 0; i--) {
                        targetParagraph.removeRun(i);
                    }

                    // 复制源文件中指定书签的内容到目标文件中指定书签的位置
                    for (XWPFRun run : sourceParagraph.getRuns()) {
                        XWPFRun newRun = targetParagraph.createRun();
                        newRun.setText(run.getText(0));
                        newRun.setFontFamily(run.getFontFamily());
                        newRun.setFontSize(run.getFontSize());
                        newRun.setBold(run.isBold());
                        newRun.setItalic(run.isItalic());
                        newRun.setColor(run.getColor());
                    }

                    // 保存目标文件
                    FileOutputStream outputStream = new FileOutputStream(outputFilePath);
                    targetDocument.write(outputStream);
                    outputStream.close();
                } else {
                    System.out.println("目标文件中没有找到指定的书签:" + targetBookmarkName);
                }
            } else {
                System.out.println("源文件中没有找到指定的书签:" + sourceBookmarkName);
            }

            sourceDocument.close();
            targetDocument.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

请确保已将Apache POI的最新版本(5.2.2)添加到项目的依赖中。

使用Apache POI替换Word文档书签内容

原文地址: https://www.cveoy.top/t/topic/fMbi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录