首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >使用 Java 代码比较两个 Word 文档

使用 Java 代码比较两个 Word 文档

原创
作者头像
用户12401097
发布于 2026-10-30 17:34:57
发布于 2026-10-30 17:34:57
1390
举报

在 Word 文档中,内容控件(Content Controls)是一种特殊的元素,可用于添加交互功能和动态内容,使文档更加灵活和实用。借助内容控件,用户可以轻松插入、删除或修改文档特定区域的内容,而无需改变文档的整体结构。这不仅便于创建不同类型的文档,也有助于提高文档处理效率。

本文将介绍如何在 Java 项目中通过代码修改 Word 文档中的内容控件。

配置 Java 项目依赖

首先,需要在 Java 项目中添加相应的 JAR 文件作为依赖,以便调用相关 API 处理 Word 文档。

如果使用 Maven,可以在项目的 pom.xml 文件中添加以下配置,将所需依赖引入应用程序中。

代码语言:java
复制
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>14.10.6</version>
    </dependency>
</dependencies>

使用 Java 修改 Word 文档正文中的内容控件

要修改 Word 文档正文中的内容控件,可以使用 StructureDocumentTag 类型的对象进行操作。通过遍历 Section.getBody() 返回的子对象集合,可以查找 StructureDocumentTag 类型的对象,并根据实际需求修改其内容。

具体操作步骤如下:

  • 创建一个 Document 对象。
  • 使用 Document.loadFromFile() 方法加载 Word 文档。
  • 使用 Section.getBody() 方法获取文档中某个节(Section)的正文。
  • 使用 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("Sample1.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;

                // 判断标签(Tag)或别名(Alias)是否为 "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("Chengdu E-iceblue Co., Ltd. is committed to providing JAVA component development products for developers.");

        // 将第一个表格重置为 5 行 4 列
        tables.get(0).resetCells(5, 4);

        // 将修改后的文档保存到文件
        doc.saveToFile("Modify Content Controls in Word Document Body.docx", FileFormat.Docx_2016);

        // 关闭文档并释放文档资源
        doc.close();
        doc.dispose();
    }
}

使用 Java 修改段落中的内容控件

要修改 Word 段落中的内容控件,可以使用 StructureDocumentTagInline 类型的对象进行操作。具体来说,需要遍历段落中的子对象集合,查找 StructureDocumentTagInline 类型的对象,并根据实际需求对其内容进行修改。

具体操作步骤如下:

  • 创建一个 Document 对象。
  • 使用 Document.loadFromFile() 方法加载 Word 文档。
  • 使用 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("Sample2.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, and 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("Modified Content Controls in Paragraphs of a Word Document.docx", FileFormat.Docx_2016);

        // 关闭文档并释放文档资源
        doc.close();
        doc.dispose();
    }
}

使用 Java 修改表格行中的内容控件

要修改 Word 表格行中的内容控件,可以遍历表格的子对象集合,查找 StructureDocumentTagRow 类型的对象,并根据实际需求进行相应修改。

具体操作步骤如下:

  • 创建一个 Document 对象。
  • 使用 Document.loadFromFile() 方法加载 Word 文档。
  • 使用 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("Sample3.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("Art");
                    textRange.getCharacterFormat().setTextColor(Color.BLUE);
                }
            }
        }

        // 将修改后的文档保存到文件
        doc.saveToFile("ModifiedTableRowContentControl.docx", FileFormat.Docx_2016);

        // 关闭文档并释放文档资源
        doc.close();
        doc.dispose();
    }
}

使用 Java 修改包裹表格单元格的内容控件

在 Spire.Doc 中,要操作表格单元格中的内容控件,需要使用 StructureDocumentTagCell 类型的对象。具体来说,可以遍历 TableRow.getChildObjects() 返回的子对象集合,查找 StructureDocumentTagCell 类型的对象,然后根据实际需求进行相应修改。

具体操作步骤如下:

  1. 创建一个 Document 对象。
  2. 使用 Document.loadFromFile() 方法加载 Word 文档。
  3. 使用 Section.getBody() 方法获取文档中某个节的正文。
  4. 使用 Body.getTables().get(0) 获取正文中的第一个表格。
  5. 使用 Table.getRows() 遍历表格的行集合,获取每个 TableRow 对象。
  6. 使用 TableRow.getChildObjects() 遍历表格行中的子对象集合,查找 StructureDocumentTagCell 类型的对象。
  7. 使用 StructureDocumentTagCell.getParagraphs() 获取单元格内容控件中的段落集合,并根据实际需求修改段落内容。
  8. 使用 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("Sample4.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("ModifiedTableCellContentControl.docx", FileFormat.Docx_2016);

        // 关闭文档并释放文档资源
        doc.close();
        doc.dispose();
    }
}

使用 Java 修改表格单元格中的内容控件

本示例演示如何修改表格单元格内段落中的内容控件。首先,使用 TableCell.getParagraphs() 获取单元格中的段落集合,然后通过 Paragraph.getChildObjects() 遍历每个段落的子对象集合,查找其中 StructureDocumentTagInline 类型的对象,并对其进行修改。

具体操作步骤如下:

  1. 创建一个 Document 对象。
  2. 使用 Document.loadFromFile() 方法加载 Word 文档。
  3. 使用 Section.getBody() 方法获取文档中某个节的正文。
  4. 使用 Body.getTables().get(0) 获取正文中的第一个表格。
  5. 使用 Table.getRows() 遍历表格的行集合,获取每个 TableRow 对象。
  6. 使用 TableRow.getCells() 遍历每一行中的单元格集合,获取每个 TableCell 对象。
  7. 使用 TableCell.getParagraphs() 遍历单元格中的段落集合,获取每个 Paragraph 对象。
  8. 使用 Paragraph.getChildObjects() 遍历段落中的子对象集合,查找 StructureDocumentTagInline 类型的对象。
  9. 使用 StructureDocumentTagInline.getChildObjects() 获取内容控件中的子对象集合,并根据子对象的类型执行相应的修改操作。
  10. 使用 Document.saveToFile() 方法保存修改后的文档。

完整示例代码如下:

代码语言:java
复制
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.awt.*;

public class ModifyTextContentControlInParagraphOfTableCell {
    public static void main(String[] args) {
        // 创建一个新的 Document 对象
        Document doc = new Document();

        // 从文件中加载文档内容
        doc.loadFromFile("Sample5.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("ModifiedContentControlInParagraphsOfTableCell.docx", FileFormat.Docx_2016);

        // 关闭文档并释放文档资源
        doc.close();
        doc.dispose();
    }
}

总结

本文介绍了如何使用 Java 修改 Word 表格单元格段落中的内容控件。通过遍历表格的行、单元格、段落及其子对象,可以定位指定的 StructureDocumentTagInline 内容控件,并通过 TextRange.setText() 修改文本内容,使用 CharacterFormat.setTextColor() 设置字体颜色,最后保存修改后的文档。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

目录
  • 配置 Java 项目依赖
  • 使用 Java 修改 Word 文档正文中的内容控件
  • 使用 Java 修改段落中的内容控件
  • 使用 Java 修改表格行中的内容控件
  • 使用 Java 修改包裹表格单元格的内容控件
  • 使用 Java 修改表格单元格中的内容控件
  • 总结
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档