在 Word 文档中,内容控件是一种特殊的元素,可用于添加交互性和动态内容, 使文档更具交互性和功能性。通过内容控件,用户可以轻松地插入、删除或修改特定部分的内容,而无需改变文档的整体结构, 这样可以更轻松地创建各种类型的文档,提高效率。本文将介绍如何使用 Spire.Doc for Java 在 Java 项目中修改 Word 文档中的内容控件。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从这个链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.3.0</version>
</dependency>
</dependencies>
Java 修改正文内容控件
在 Spire.Doc 中,要修改正文内容控件,需要操作 StructureDocumentTag 对象类型。通过遍历 Section.getBody() 中的子对象集合,找到类型为 StructureDocumentTag 的对象,然后进行相应的更改。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.loadFromFile() 方法加载一个文档。
- 使用 Section.getBody() 获取文档一个节的正文部分。
- 遍历正文部分的子对象集合 Body.getChildObjects(),获取到类型为 StructureDocumentTag 的子对象。
- 进入 StructureDocumentTag.getChildObjects() 子对象集合,根据子对象的类型执行相应的修改操作。
- 使用 Document.saveToFile() 方法保存到文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.util.*;
public class ModifyContentControlInBody {
public static void main(String[] args) {
// 创建一个新的文档对象
Document doc = new Document();
// 从文件加载文档内容
doc.loadFromFile("示例1.docx");
// 获取文档正文部分
Body body = doc.getSections().get(0).getBody();
// 创建段落列表和表格列表
List<Paragraph> paragraphs =new ArrayList<>();
List<Table>tables = new ArrayList<>();
for (int i = 0; i < body.getChildObjects().getCount(); i++) {
// 获取文档对象
DocumentObject documentObject = body.getChildObjects().get(i);
// 如果是StructureDocumentTag对象
if (documentObject instanceof StructureDocumentTag) {
StructureDocumentTag structureDocumentTag = (StructureDocumentTag)documentObject;
// 如果标签为"c1"或者别名为"c1"
if (structureDocumentTag.getSDTProperties().getTag().equals("c1") || structureDocumentTag.getSDTProperties().getAlias().equals("c1")) {
for (int j = 0; j < structureDocumentTag.getChildObjects().getCount(); j++) {
// 如果是段落对象
if (structureDocumentTag.getChildObjects().get(j) instanceof Paragraph) {
Paragraph paragraph = (Paragraph)structureDocumentTag.getChildObjects().get(j);
paragraphs.add(paragraph);
}
// 如果是表格对象
if (structureDocumentTag.getChildObjects().get(j) instanceof Table) {
Table table = (Table)structureDocumentTag.getChildObjects().get(j);
tables.add(table);
}
}
}
}
}
// 修改第一个段落的文本内容
paragraphs.get(0).setText("成都冰蓝科技有限公司致力于为开发人员提供 JAVA 组件开发产品。");
// 重置第一个表格的单元格为5行4列
tables.get(0).resetCells(5, 4);
// 将修改后的文档保存到文件
doc.saveToFile("修改Word文档正文中的内容控件.docx", FileFormat.Docx_2016);
// 关闭文档及释放文档资源
doc.close();
doc.dispose();
}
}
Java 修改段落中的内容控件
在 Spire.Doc 中,要修改段落中的内容控件,需要使用对象类型为 StructureDocumentTagInline。具体操作是遍历段落的子对象集合,找到类型为 StructureDocumentTagInline 的对象,然后对其进行相应的修改。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.loadFromFile()方法加载一个文档。
- 使用 Section.getBody() 获取文档一个节的正文部分。
- 使用 Body.getParagraphs().get(0) 获取正文部分的第一个段落。
- 遍历段落的子对象集合 Paragraph.getChildObjects(),获取到类型为 StructureDocumentTagInline 的子对象。
- 进入 StructureDocumentTagInline.getChildObjects() 子对象集合,根据子对象的类型执行相应的修改操作。
- 使用 Document.saveToFile() 方法保存到文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
public class ModifyContentControlInParagraph {
public static void main(String[] args) {
// 创建一个新的Document对象
Document doc = new Document();
// 从文件加载文档内容
doc.loadFromFile("示例2.docx");
// 获取文档的主体部分
Body body = doc.getSections().get(0).getBody();
// 获取主体部分的第一个段落
Paragraph paragraph = body.getParagraphs().get(0);
// 遍历段落中的子对象
for (int i = 0; i < paragraph.getChildObjects().getCount(); i++) {
// 检查子对象是否为StructureDocumentTagInline类型
if (paragraph.getChildObjects().get(i) instanceof StructureDocumentTagInline) {
// 将子对象转换为StructureDocumentTagInline类型
StructureDocumentTagInline structureDocumentTagInline = (StructureDocumentTagInline)paragraph.getChildObjects().get(i);
// 检查文档标记的Tag或Alias属性是否为"text1"
if (structureDocumentTagInline.getSDTProperties().getTag().equals("text1") || structureDocumentTagInline.getSDTProperties().getAlias().equals("text1")) {
// 遍历StructureDocumentTagInline对象里的子对象
for (int j = 0; j < structureDocumentTagInline.getChildObjects().getCount(); j++) {
// 检查子对象是否为TextRange对象
if (structureDocumentTagInline.getChildObjects().get(j) instanceof TextRange) {
// 将子对象转换为TextRange类型
TextRange range = (TextRange)structureDocumentTagInline.getChildObjects().get(j);
// 设置文本内容为指定内容
range.setText("Word97-2003、Word2007、Word2010、Word2013、Word2016以及Word2019");
}
}
}
// 检查文档标记的Tag或Alias属性是否为"logo1"
if (structureDocumentTagInline.getSDTProperties().getTag().equals("logo1") || structureDocumentTagInline.getSDTProperties().getAlias().equals("logo1")) {
// 遍历StructureDocumentTagInline对象里的子对象
for (int j = 0; j < structureDocumentTagInline.getChildObjects().getCount(); j++) {
// 检查子对象是否为图片
if (structureDocumentTagInline.getChildObjects().get(j) instanceof DocPicture) {
// 将子对象转换为DocPicture类型
DocPicture docPicture = (DocPicture)structureDocumentTagInline.getChildObjects().get(j);
// 加载指定图片
docPicture.loadImage("Doc-Java.png");
// 设置图片宽度和高度
docPicture.setWidth(100);
docPicture.setHeight(100);
}
}
}
}
}
// 将修改后的文档保存为新的文件
doc.saveToFile("修改Word文档的段落中的内容控件.docx", FileFormat.Docx_2016);
// 关闭文档及释放文档资源
doc.close();
doc.dispose();
}
}
Java 修改表格行内容控件
在 Spire.Doc 中,要修改表格行内容控件,需要遍历表格的子对象集合,找到类型为 StructureDocumentTagRow 的对象,然后进行相应的更改。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.loadFromFile() 方法加载一个文档。
- 使用 Section.getBody() 获取文档一个节的正文部分。
- 使用 Body.getTables().get(0) 获取正文部分的第一个表格。
- 遍历表格的子对象集合 Table.getChildObjects(),获取到类型为 StructureDocumentTagRow 的子对象。
- 进入 StructureDocumentTagRow.getCells() 表格行内容控件的单元格集合,然后对单元格内容执行相应的修改操作。
- 使用 Document.saveToFile() 方法保存到文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;
public class ModifyTextContentControlInTableRow {
public static void main(String[] args) {
// 创建一个新的文档对象
Document doc = new Document();
// 从文件加载文档
doc.loadFromFile("示例3.docx");
// 获取文档正文部分
Body body = doc.getSections().get(0).getBody();
// 获取第一个表格
Table table = body.getTables().get(0);
// 遍历表格中的子对象
for (int i = 0; i < table.getChildObjects().getCount(); i++) {
// 判断子对象是否为StructureDocumentTagRow类型
if (table.getChildObjects().get(i) instanceof StructureDocumentTagRow) {
// 将子对象转换为StructureDocumentTagRow对象
StructureDocumentTagRow structureDocumentTagRow = (StructureDocumentTagRow)table.getChildObjects().get(i);
// 检查StructureDocumentTagRow的Tag或Alias属性是否为"row1"
if (structureDocumentTagRow.getSDTProperties().getTag().equals("row1") || structureDocumentTagRow.getSDTProperties().getAlias().equals("row1")) {
// 清空单元格中的段落
structureDocumentTagRow.getCells().get(0).getParagraphs().clear();
// 在单元格中添加一个段落,并设置文本
TextRange textRange = structureDocumentTagRow.getCells().get(0).addParagraph().appendText("艺术");
textRange.getCharacterFormat().setTextColor(Color.BLUE);
}
}
}
// 将修改后的文档保存到文件
doc.saveToFile("修改表格行内容控件.docx", FileFormat.Docx_2016);
// 关闭文档及释放文档资源
doc.close();
doc.dispose();
}
}
Java 修改表格单元格内容控件
在 Spire.Doc 中,要对表格单元格的内容控件对象进行操作,需要使用名为 StructureDocumentTagCell 的特定类型的对象。这可以通过检查 TableRow.getChildObjects() 中的子对象集合,找到类型为 StructureDocumentTagCell 的对象,然后执行相应的操作。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.loadFromFile() 方法加载一个文档。
- 使用 Section.getBody() 获取文档一个节的正文部分。
- 使用 Body.getTables().get(0) 获取正文部分的第一个表格。
- 遍历表格行集合 Table.getRows(),进入每一个 TableRow 对象。
- 遍历表格行的子对象集合 TableRow.getChildObjects(),获取到类型为 StructureDocumentTagCell 的子对象。
- 进入 StructureDocumentTagCell.getParagraphs() 表格单元格内容控件的段落集合,然后对内容执行相应的修改操作。
- 使用 Document.saveToFile() 方法保存到文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;
public class ModifyTextContentControlInTableCell {
public static void main(String[] args) {
// 创建一个新的文档对象
Document doc = new Document();
// 从文件加载文档
doc.loadFromFile("示例4.docx");
// 获取文档的正文部分
Body body = doc.getSections().get(0).getBody();
// 获取文档中的第一个表格
Table table = body.getTables().get(0);
// 遍历表格的行
for (int i = 0; i < table.getRows().getCount(); i++) {
// 遍历每行中的子对象
for (int j = 0; j < table.getRows().get(i).getChildObjects().getCount(); j++) {
// 检查该子对象是否为StructureDocumentTagCell
if (table.getRows().get(i).getChildObjects().get(j) instanceof StructureDocumentTagCell)
{
// 将子对象转换为StructureDocumentTagCell类型
StructureDocumentTagCell structureDocumentTagCell = (StructureDocumentTagCell)table.getRows().get(i).getChildObjects().get(j);
// 检查structureDocumentTagCell的Tag或Alias属性是否为"cell1"
if (structureDocumentTagCell.getSDTProperties().getTag().equals("cell1") || structureDocumentTagCell.getSDTProperties().getAlias().equals("cell1")) {
// 清空单元格中的段落
structureDocumentTagCell.getParagraphs().clear();
// 添加一个新段落,并在其中添加文本
TextRange textRange = structureDocumentTagCell.addParagraph().appendText("92");
textRange.getCharacterFormat().setTextColor(Color.BLUE);
}
}
}
}
// 将修改后的文档保存为新的文件
doc.saveToFile("修改表格单元格内容控件.docx", FileFormat.Docx_2016);
// 关闭文档及释放文档资源
doc.close();
doc.dispose();
}
}
Java 修改表格单元格中的内容控件
这个案例演示了如何修改表格单元格中段落的内容控件。首先要访问单元格中的段落集合 TableCell.getParagraphs(),然后遍历每个段落对象的子对象集合 Paragraph.getChildObjects(),在其中查找类型为 StructureDocumentTagInline 的对象,最后对其进行修改。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.loadFromFile() 方法加载一个文档。
- 使用 Section.getBody() 获取文档一个节的正文部分。
- 使用 Body.getTables().get(0) 获取正文部分的第一个表格。
- 遍历表格行集合 Table.getRows(),进入每一个 TableRow 对象。
- 遍历单元格集合 TableRow.getCells(),进入每一个 TableCell 对象。
- 遍历单元格里的段落集合 TableCell.getParagraphs(),进入每一个 Paragraph 对象。
- 遍历段落的子对象集合 Paragraph.getChildObjects(),查找到类型为 StructureDocumentTagInline 的对象。
- 进入 StructureDocumentTagInline.getChildObjects() 子对象集合,根据子对象的类型执行相应的修改操作。
- 使用 Document.saveToFile() 方法保存到文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class ModifyTextContentControlInParagraphOfTableCell {
public static void main(String[] args) {
// 创建一个新的Document对象
Document doc = new Document();
// 从文件加载文档内容
doc.loadFromFile("示例5.docx");
// 获取文档正文部分
Body body = doc.getSections().get(0).getBody();
// 获取第一个表格
Table table = body.getTables().get(0);
// 遍历表格的行
for (int r = 0; r < table.getRows().getCount(); r++) {
// 遍历表格行中的单元格
for (int c = 0; c < table.getRows().get(r).getCells().getCount(); c++) {
// 遍历单元格中的段落
for (int p = 0; p < table.getRows().get(r).getCells().get(c).getParagraphs().getCount(); p++) {
// 获取段落对象
Paragraph paragraph = table.getRows().get(r).getCells().get(c).getParagraphs().get(p);
// 遍历段落中的子对象
for (int i = 0; i < paragraph.getChildObjects().getCount(); i++) {
// 判断子对象是否为StructureDocumentTagInline类型
if (paragraph.getChildObjects().get(i) instanceof StructureDocumentTagInline) {
// 转换为StructureDocumentTagInline对象
StructureDocumentTagInline structureDocumentTagInline = (StructureDocumentTagInline)paragraph.getChildObjects().get(i);
// 检查StructureDocumentTagInline的Tag或Alias属性是否为"test1"
if (structureDocumentTagInline.getSDTProperties().getTag().equals("test1") || structureDocumentTagInline.getSDTProperties().getAlias().equals("test1") ) {
// 遍历StructureDocumentTagInline的子对象
for (int j = 0; j < structureDocumentTagInline.getChildObjects().getCount(); j++) {
// 判断子对象是否为TextRange类型
if (structureDocumentTagInline.getChildObjects().get(j) instanceof TextRange) {
// 转换为TextRange对象
TextRange textRange = (TextRange)structureDocumentTagInline.getChildObjects().get(j);
// 设置文本内容
textRange.setText("89");
//设置文本颜色
textRange.getCharacterFormat().setTextColor(Color.BLUE);
}
}
}
}
}
}
}
}
// 将修改后的文档保存为新文件
doc.saveToFile("修改表格单元格的段落中的文本内容控件.docx", FileFormat.Docx_2016);
// 关闭文档及释放文档资源
doc.close();
doc.dispose();
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。