java - 如何使用 apache poi 在文档 .docx 中的表格单元格中创建 TextBox
问题描述
我使用了 Javafx 和
我使用 apache poi 创建了一个表:
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFTable table = document.createTable(4, 3);
并创建了类似于以下段落的段落:
XWPFParagraph p1 = table.getRow(0).getCell(2).getParagraphs().get(0);
XWPFRun r1 = p1.createRun();
r1.setText(category_number.getText() + category.toString());
现在,我想在一行的一个单元格中创建一个文本框,但不知道如何将单元格和行寻址到文本框并设置文本和对齐文本框。
请帮我 ):
解决方案
a 中的文本框*.docx
是文档内容中的一个形状。创建形状尚未在XWPF
. 但是可以使用底层ooxml-schemas
类来完成。
例子:
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTxbxContent;
import com.microsoft.schemas.vml.CTGroup;
import com.microsoft.schemas.vml.CTShape;
import org.w3c.dom.Node;
public class CreateWordTextBoxInTable {
public static void main(String[] args) throws Exception {
XWPFDocument document= new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("The table:");
XWPFTable table = document.createTable(4, 3);
// table header row
for (int c = 0; c < 3; c++ ) {
paragraph = table.getRow(0).getCell(c).getParagraphArray(0);
if (paragraph == null) paragraph = table.getRow(0).getCell(c).addParagraph();
run = paragraph.createRun();
run.setText("Column " + (c+1));
}
// get run in cell for text box
XWPFTableCell cell = table.getRow(1).getCell(1);
paragraph = cell.getParagraphArray(0);
if (paragraph == null) paragraph = cell.addParagraph();
run = paragraph.createRun();
// create inline text box in run
// first crfeate group shape
CTGroup ctGroup = CTGroup.Factory.newInstance();
// now add shape to group shape
CTShape ctShape = ctGroup.addNewShape();
ctShape.setStyle("width:100pt;height:36pt");
// add text box content to shape
CTTxbxContent ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent();
XWPFParagraph textboxparagraph = new XWPFParagraph(ctTxbxContent.addNewP(), (IBody)cell);
textboxparagraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun textboxrun = textboxparagraph.createRun();
textboxrun.setText("The TextBox content...");
textboxrun.setFontSize(10);
// add group shape as picture to the run
Node ctGroupNode = ctGroup.getDomNode();
CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
CTR cTR = run.getCTR();
cTR.addNewPict();
cTR.setPictArray(0, ctPicture);
FileOutputStream out = new FileOutputStream("test.docx");
document.write(out);
out.close();
}
}
此代码已使用apache poi 4.0.1
并需要ooxml-schemas-1.4.jar
类内路径进行测试。
推荐阅读
- amazon-s3 - 使用 Java 应用程序将 Gzip 文件上传到 S3 存储桶时已损坏
- arrays - 将 numpy loadtxt 的输出作为单个数组而不是多个数组
- java - MongoDb 4.2 中的文档级别锁定
- python - Python ::: 带参数捕获异常
- javascript - Vue 将 Undefined 设置为值应为 0 或 false 的属性
- python - 具有双变量颜色图的 Altair 地图
- sql-server - 如何在 SQL Server 中连接两个 ID 不相同的表?
- android - 在模拟器上工作但现在在真实设备(手机)上工作的 API。扑
- python - 根据日期范围(最小值和最大值)绘制多折线图
- php - 在 wordpress cpt 的子分类模板页面上获取父分类术语 ID