gpt4 book ai didi

java poi XWPF word - 在新文档中创建书签

转载 作者:行者123 更新时间:2023-12-01 20:07:40 28 4
gpt4 key购买 nike

XWPF Word 文档中存在许多读取和编辑/替换书签的示例。但我想创建一个文档并创建新书签。创建文档 - 没问题:

private void createWordDoc() throws IOException {
XWPFDocument document = new XWPFDocument();
File tempDocFile = new File(pathName+"\\temp.docx");
FileOutputStream out = new FileOutputStream(tempDocFile);
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("testing string ");
document.write(out);
out.close();
}

如何在文本“测试字符串”上创建书签?

最佳答案

到目前为止,这还没有在 apache poi 的高级类中实现。因此低水平CTPCTBookmark需要。

示例:

import java.io.FileOutputStream;

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

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;

import java.math.BigInteger;

public class CreateWordBookmark {

public static void main(String[] args) throws Exception {

XWPFDocument document = new XWPFDocument();

XWPFParagraph paragraph = document.createParagraph();

//bookmark before the run
CTBookmark bookmark = paragraph.getCTP().addNewBookmarkStart();
bookmark.setName("before_testing_string");
bookmark.setId(BigInteger.valueOf(0));
paragraph.getCTP().addNewBookmarkEnd().setId(BigInteger.valueOf(0));

//bookmark the run
bookmark = paragraph.getCTP().addNewBookmarkStart();
bookmark.setName("testing_string");
bookmark.setId(BigInteger.valueOf(1));

XWPFRun run = paragraph.createRun();
run.setText("testing string ");

paragraph.getCTP().addNewBookmarkEnd().setId(BigInteger.valueOf(1));

//bookmark after the run
bookmark = paragraph.getCTP().addNewBookmarkStart();
bookmark.setName("after_testing_string");
bookmark.setId(BigInteger.valueOf(2));
paragraph.getCTP().addNewBookmarkEnd().setId(BigInteger.valueOf(2));

document.write(new FileOutputStream("CreateWordBookmark.docx"));
document.close();

}
}

关于java poi XWPF word - 在新文档中创建书签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47182971/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com