简体   繁体   English

用于docx的Apache POI在特定页面上插入文本

[英]Apache POI for docx Insert Text On Specific Page

I'm trying to make Table of Contents for my Word document docx. 我正在尝试为我的Word文档docx创建目录。

Apache POI is still too buggy. Apache POI仍然存在问题。 The document.createTOC() does not produce anything unless placed at the end. 除非放在末尾,否则document.createTOC()不会产生任何结果。 Sometimes, it doesn't give the correct page numbers. 有时,它没有提供正确的页码。

The document.enforceUpdateFields() doesn't do anything! document.enforceUpdateFields()不执行任何操作!

So I thought I make my own method that creates the Table of Content. 因此,我想我应该使用自己的方法来创建目录。 However, I will call it at the end but I need it to be inserted at the beginning! 但是,我将在结尾处调用它,但需要在开始处插入它!

In other words, suppose my document at some point in my program has some text on the first page and the second page. 换句话说,假设我的文档在程序的某个位置在第一页和第二页上都有一些文本。 And I haven't yet saved it; 而且我还没有保存它; how do I insert at the beginning of first page? 如何在第一页的开头插入?

I haven't tried this yet. 我还没有尝试过。 But, after you write the document. 但是,在编写文档之后。 Reload it again 重新加载

Then try the following: 然后尝试以下操作:

List<XWPFParagraph> paragraphs = document.getParagraphs();
XWPFRun run = paragraphs.get(0).insertNewRun(0); // first paragraph, 0 is the position
run.setText("your data here");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM