检索和替换 Word 文档中的字体是文档设计中的重要环节。通过调整字体,用户可以为文档增添新风格,提升文档的美观性和可读性。同时,掌握字体修改的方法,也可以增加文档的吸引力。 为了助力高效完成这一任务,本文将以 Spire.Doc for 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.4.1</version>
</dependency>
</dependencies>
获取 Word 文档中使用的字体
要检索 Word 文档中的字体信息,你需要遍历文档的各个部分,包括节、段落以及它们的子对象。然后判断这些子对象是否是 TextRange 的实例。如果是,就可以通过 TextRange 类的方法提取字体相关的信息,如字体名称和字号。
以下是在 Java 中检索 Word 文档字体信息的基本步骤:
- 创建一个 Document 类的实例。
- 通过 Document.loadFromFile() 方法加载一个 Word 文档。
- 遍历每个节、段落和子对象。
- 检查子对象是否为 TextRange 类的实例。
- 如果是,则使用 TextRange.getCharacterFormat().getFontName()、TextRange.getCharacterFormat().getFontSize() 和 TextRange.getCharacterFormat().getTextColor() 方法获取字体的名称、字号和颜色。
- 将字体信息保存为 TXT 文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// 自定义 FontInfo 类用以存储字体信息
class FontInfo {
private String name;
private Float size;
public FontInfo() {
this.name = "";
this.size = null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getSize() {
return size;
}
public void setSize(Float size) {
this.size = size;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof FontInfo)) return false;
FontInfo other = (FontInfo) obj;
return name.equals(other.getName()) && size.equals(other.getSize());
}
}
public class GetFontInfo {
// 将字符串写入到 txt 文件的方法
public static void writeAllText(String filename, List text) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
for (String s : text) {
writer.write(s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
List fontInfos = new ArrayList<>();
StringBuilder fontInformations = new StringBuilder();
// 创建一个 Document 类的对象
Document document = new Document();
// 加载 Word 文档
document.loadFromFile("/示例文档.docx");
// 遍历文档中的所有节
for (int i = 0; i < document.getSections().getCount(); i++) {
Section section = document.getSections().get(i);
// 遍历节中的段落
for (int j = 0; j < section.getBody().getParagraphs().getCount(); j++) {
Paragraph paragraph = section.getBody().getParagraphs().get(j);
// 遍历每一个子对象
for (int k = 0; k < paragraph.getChildObjects().getCount(); k++) {
DocumentObject obj = paragraph.getChildObjects().get(k);
if (obj instanceof TextRange) {
TextRange txtRange = (TextRange) obj;
// 获取字体的名称和字号
String fontName = txtRange.getCharacterFormat().getFontName();
Float fontSize = txtRange.getCharacterFormat().getFontSize();
// 获取字体的颜色
Color color = txtRange.getCharacterFormat().getTextColor();
String textColor = String.format("RGB(%d,%d,%d)", color.getRed(), color.getGreen(), color.getBlue());
// 保存字体信息
FontInfo fontInfo = new FontInfo();
fontInfo.setName(fontName);
fontInfo.setSize(fontSize);
if (!fontInfos.contains(fontInfo)) {
fontInfos.add(fontInfo);
String str = String.format("字体:%s、字号:%.2f、颜色:%s%n", fontInfo.getName(), fontInfo.getSize(), textColor);
fontInformations.append(str);
}
}
}
}
}
// 将字体信息写入到 txt 文件中
writeAllText("/获取字体信息.txt", Arrays.asList(fontInformations.toString().split("\n")));
// 释放资源
document.dispose();
}
}
在 Word 文档中替换指定字体
在获取到指定文本范围的字体名称后,你可以通过调用 TextRange.getCharacterFormat().setFontName() 方法,轻松将其替换为其它字体。此外,你还可以使用 TextRange 类中对应的方法,来调整字体大小和文本颜色。
下方是在 Java 中替换 Word 文档中指定字体的具体步骤:
- 创建一个 Document 类的对象。
- 通过 Document.loadFromFile() 方法加载 Word 文档。
- 遍历每一个节、段落和子对象。
- 检查子对象是否为 TextRange 类的实例。
- 如果是,则使用 TextRange.getCharacterFormat().getFontName() 方法获取该文本范围的字体名称。
- 通过 TextRange.getCharacterFormat().setFontName() 方法为该文本范围设置新的字体。
- 使用 Document.saveToFile() 方法保存修改后的 Word 文档。
- Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
public class ReplaceFont {
public static void main(String[] args) {
// 创建一个 Document 类的实例
Document document = new Document();
// 加载 Word 文档
document.loadFromFile("/示例文档.docx");
// 遍历文档中的节
for (int i = 0; i < document.getSections().getCount(); i++) {
// 获取当前节
Section section = document.getSections().get(i);
// 遍历节中的所有段落
for (int j = 0; j < section.getBody().getParagraphs().getCount(); j++) {
// 获取当前段落
Paragraph paragraph = section.getBody().getParagraphs().get(j);
// 遍历段落中的每一个子对象
for (int k = 0; k < paragraph.getChildObjects().getCount(); k++) {
// 获取子对象
DocumentObject obj = paragraph.getChildObjects().get(k);
// 判断子对象是否为 TextRange
if (obj instanceof TextRange) {
// 获取这个 TextRange
TextRange txtRange = (TextRange) obj;
// 获取字体的名称
String fontName = txtRange.getCharacterFormat().getFontName();
// 判断字体名称是否为 Microsoft JhengHei
if ("Microsoft JhengHei".equals(fontName)) {
// 使用新字体替换该字体
txtRange.getCharacterFormat().setFontName("KaiTi");
}
}
}
}
}
// 保存修改后的文档
document.saveToFile("/替换 Word 字体.docx", FileFormat.Docx);
document.dispose();
}
}
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。