如何通过Java 在 Word 中更改字体颜色

在Word文档中使用不同的字体颜色既可以丰富文档外观,也能突出强调某些重点部分。当Word文档包含大量内容时,可以使用Free Spire.Doc for Java轻松更改Word文档中的字体颜色。接下来我就将分别价绍更改段落及特定文本字体颜色的具体操作和相关代码。

安装Spire.Doc.Jar

下面有两种安装方法,可根据自身情况选择:

1.如果您使用的是 maven,可以直接通过添加以下代码到项目的 pom.xml 文件中,从而将 JAR 文件导入到应用程序中。

<repositories>

    <repository>

        <id>com.e-iceblue</id>

        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>

    </repository>

</repositories>

<dependencies>

    <dependency>

        <groupId>e-iceblue</groupId>

        <artifactId>spire.doc.free</artifactId>

        <version>5.2.0</version>

    </dependency>

</dependencies>

2.如果您没有使用 maven,也可以从此链接下载Free Spire.Doc for Java,下载完成后,将下载包进行解压;然后在IDEA中创建一个新项目,依次点击“文件”(File),“项目结构”(Project Structure),“组件”(Modules),“依赖项”(Dependencies),再点击右方绿色“+”下的第一个选项“JAR文件或路径”(JARs or Directories),找到之前解压好的文件,并将其中的lib文件夹下的Spire.Doc.jar文件导入到项目中。

更改段落的字体颜色

具体操作:

  • 创建一个 Document 实例。
  • 使用 Document.LoadFromFile() 方法加载 Word 文档。
  • 使用 Document.getSections().get(sectionIndex) 方法获取所需的节。
  • 使用 Section.getParagraphs().get(paragraphIndex) 方法获取想要更改颜色的段落。
  • 创建一个 ParagraphStyle 实例。
  • 使用 ParagraphStyle.setName() 和 ParagraphStyle.getCharacterFormat().setTextColor() 方法设置样式名称和字体颜色。
  • 使用 Document.getStyles().add() 方法将样式添加到文档中。
  • 使用 Paragraph.applyStyle() 方法将样式应用于段落。
  • 使用 Document.saveToFile() 方法保存结果文档。

相关代码:

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;

import java.awt.*;

public class ChangeFontColorForParagraph {
    public static void main(String []args){
        //创建一个Document实例
        Document document = new Document();
        //加载Word文档
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.docx");

        //获取第一节
        Section section = document.getSections().get(0);

        //更改第一个段落的文本颜色
        Paragraph p1 = section.getParagraphs().get(0);
        ParagraphStyle s1 = new ParagraphStyle(document);
        s1.setName("Color1");
        s1.getCharacterFormat().setTextColor(new Color(188, 143, 143));
        document.getStyles().add(s1);
        p1.applyStyle(s1.getName());

        //更改第二段的文本颜色
        Paragraph p2 = section.getParagraphs().get(1);
        ParagraphStyle s2 = new ParagraphStyle(document);
        s2.setName("Color2");
        s2.getCharacterFormat().setTextColor(new Color(0, 0, 139));;
        document.getStyles().add(s2);
        p2.applyStyle(s2.getName());

        //保存结果文档
        document.saveToFile("ChangeParagraphTextColor.docx", FileFormat.Docx);
    }
}

更改特定文本的字体颜色

具体步骤:

  • 创建一个 Document实例。
  • 使用 Document.loadFromFile() 方法加载 Word 文档。
  • 使用 Document.findAllString() 方法找到要更改字体颜色的文本。
  • 循环搜索所有出现的文本并使用 TextSelection.getAsOneRange().getCharacterFormat().setTextColor() 方法更改每个出现的字体颜色。
  • 使用 Document.saveToFile() 方法保存结果文档。

 

相关代码:

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;

import java.awt.*;

public class ChangeFontColorForText {
    public static void main(String []args){
        //创建一个Document实例
        Document document = new Document();
        //加载Word文档
        document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.docx");

        //找到要更改字体颜色的文本
        TextSelection[] text = document.findAllString("月", false, true);
        //更改搜索文本的字体颜色
        for (TextSelection seletion : text)
        {
            seletion.getAsOneRange().getCharacterFormat().setTextColor(Color.red);
        }
        //保存结果文档
        document.saveToFile("ChangeCertainTextColor.docx", FileFormat.Docx);

    }
}

posted @ 2022-11-11 13:59  Gia-  阅读(846)  评论(0编辑  收藏  举报