本文介绍通过Java后端程序代码来读取Word文本和段落格式的方法。

本次测试环境如下:

  • Word版本:2013
  • 编译环境:IntelliJ IDEA2018
  • Work库:free spire.doc.jar 3.9.0
  • JDK版本:1.8.0

通过textrange.getCharacterFormat()方法读取文本字符串格式,通过paragraph.getFormat()读取段落格式,读取具体文字及段落属性时,可支持读取字体、字号、文字颜色、文字背景、文字是否加粗或倾斜、文字下划线、大小写、边框、上标下标、行距、段落缩进、对齐方式、段落边框、背景等等,下表中罗列了所有可支持读取的样式属性,供参考:

读取文本格式 getCharacterFormat():

方法

类型

getFontName()

String

getFontNameAscii()

String

getFontNameBidi()

String

getFontNameFarEast()

String

getFontNameNonFarEast()

String

getBold()

boolean

getFontSize()

float

getHighlightColor()

Color

getItalic()

boolean

getTextBackgroundColor()

Color

getTextColor()

Color

getAllCaps()

boolean

getAllowContextualAlternates()

boolean

getBidi()

boolean

getBoldBidi()

boolean

getBorder()

Border

getCharacterSpacing()

float

getDoubleStrike()

boolean

getEmboss()

boolean

getEmphasisMark()

Emphasis

getEngrave()

boolean

getFontSizeBidi()

float

getFontTypeHint()

FontTypeHint

getHidden()

boolean

getItalicBidi()

boolean

getLigaturesType()

LigatureType

getLocaleIdASCII()

short

getLocaleIdFarEast()

short

getNumberFormType()

NumberFormType

getNumberSpaceType()

NumberSpaceType

getPosition()

float

getStylisticSetType()

StylisticSetType

getSubSuperScript()

SubSuperScript

getTextScale()

short

getUnderlineStyle()

UnderlineStyle

读取段落格式:getFormat()

方法

类型

getLineSpacing()

float

getFirstLineIndent()

float

getLeftIndent()

float

getAfterSpacing()

float

getBeforeSpacing()

float

getRightIndent()

float

getTextAlignment()

TextAlignmnet

getAfterAutoSpacing()

boolean

getAutoSpaceDE()

boolean

getAutoSpaceDN()

boolean

getBackColor()

Color

getBeforeAutoSpacing()

boolean

getBoders()

Borders

getHorizontalAlignment()

HorizontalAlignmnet

getKeepFollow()

boolean

getKeepLines()

boolean

getLineSpacingRule()

LineSpacingRule

getMirrorIndents()

boolean

getOutlineLevel()

OutlineLevel

getOverflowPunc()

boolean

getPageBreakAfter()

boolean

getPageBreakBefore()

boolean

getSuppressAutoHyphens()

boolean

getTabs()

TabCollection

用于测试的Word文档:

Java示例代码

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange; import java.awt.*; public class GetTextFormat {
public static void main(String[] args) {
//加载Word源文档
Document doc = new Document();
doc.loadFromFile("test.docx"); //获取段落数量
int count = doc.getSections().get(0).getParagraphs().getCount();
System.out.println("总共含有段落数:" + count); //查找指定文本
TextSelection textSelections = doc.findString("东野圭吾", false, true);
//获取字体名称
String fontname = textSelections.getAsOneRange().getCharacterFormat().getFontName();
//获取字体大小
float fontsize = textSelections.getAsOneRange().getCharacterFormat().getFontSize();
System.out.println("字体名称:" + fontname +"\n"
+"字体大小:"+fontsize); //获取第二段
Paragraph paragraph2 = doc.getSections().get(0).getParagraphs().get(1);
//获取段落行距
float linespage = paragraph2.getFormat().getLineSpacing();
System.out.println("段落行距:" + linespage); //遍历段落中的子对象
for (int z = 0; z < paragraph2.getChildObjects().getCount(); z++)
{
Object obj2 = paragraph2.getChildObjects().get(z); //判定是否为文本
if (obj2 instanceof TextRange)
{
TextRange textRange2 = (TextRange) obj2; //获取文本颜色
Color textcolor = textRange2.getCharacterFormat().getTextColor();
if (!(textcolor.getRGB() == 0))
{
System.out.println("文本颜色:" + textRange2.getText() + textcolor.toString());
} //获取字体加粗效果
boolean isbold = textRange2.getCharacterFormat().getBold();
if (isbold == true)
{
System.out.println("加粗文本:" + textRange2.getText());
} //获取字体倾斜效果
boolean isitalic = textRange2.getCharacterFormat().getItalic();
if (isitalic == true)
{
System.out.println("倾斜文本:" + textRange2.getText());
} //获取文本背景
String text = textRange2.getText();
Color highlightcolor = textRange2.getCharacterFormat().getHighlightColor();//获取文本的高亮颜色(即突出显示颜色)
if (!(highlightcolor.getRGB() == 0 ))
{
System.out.println("文本高亮:" + text + highlightcolor.toString());//输出高亮的文本和颜色
} Color textbackgroundcolor = textRange2.getCharacterFormat().getTextBackgroundColor();//获取文字背景(底纹)
if (!(textbackgroundcolor.getRGB()==0))
{
System.out.println("文本背景:" + text + textbackgroundcolor.toString());//输出有背景的文本和颜色
} }
} }
}

运行程序,输入获取结果:

Java 读取Word文本/段落格式属性的更多相关文章

  1. Java 读取Word文本框中的文本/图片/表格

    Word可插入文本框,文本框中可嵌入文本.图片.表格等内容.对文档中的已有文本框,也可以读取其中的内容.本文以Java程序代码来展示如何读取文本框,包括读取文本框中的文本.图片以及表格等. [程序环境 ...

  2. C# 读取Word文本框中的文本、图片和表格(附VB.NET代码)

    [概述] Word中可插入文本框,在文本框中可添加文本.图片.表格等内容.本篇文章通过C#程序代码介绍如何来读取文本框中的文本.图片和表格等内容.附VB.NET代码,有需要可作参考. [程序环境] 程 ...

  3. [Java] Java读取Word文档

    前言 最近需要做一些NLP 方面的工作,使用的是Java,在此总结一下使用Java读取Word(.doc)格式文件的方法. Apache基金会非常厉害,开源工具包POI就可以处理微软家的文档,甚至包括 ...

  4. Java读取word中表格

    因为要新建一个站,公司要把word表格的部分行列存到数据库中.之前用java操作过excel,本来打算用java从word表格中读取数据,再存到数据库中,结果因为权限不够,无法访问公司要写的那个数据库 ...

  5. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  6. Java 读取Word批注中的文本和图片

    本文将介绍读取Word批注的方法,包括读取Word批注中的文本及图片.关于操作Word批注的方法还可以参考这两篇文章:Java 添加.回复.修改.删除Word批注:Java 给Word指定字符串添加批 ...

  7. Java 读取Word表格中的文本和图片

    本文通过Java程序来展示如何读取Word表格,包括读取表格中的文本和图片.下面是具体实现的步骤和方法. 1. 程序环境准备 代码编译工具:IntelliJ IDEA Jdk版本:1.8.0 测试文档 ...

  8. java通过jacob来读取word转换为htm格式

    转自:http://blog.csdn.net/chinapi_hzh/article/details/5798689 因为微软没有公开word源代码,所以直接用java流来读取word的后果是读出来 ...

  9. Java 读取Word中的脚注、尾注

    本文介绍读取Word中的脚注及尾注的方法,添加脚注.尾注可以参考这篇文章. 注:本文使用了Word类库(Free Spire.Doc for Java 免费版)来读取,获取该类库可通过官网下载,并解压 ...

随机推荐

  1. vue & vue router & dynamic router

    vue & vue router & dynamic router https://router.vuejs.org/guide/essentials/dynamic-matching ...

  2. Apache HTTP Server & WS (websockets)

    Apache HTTP Server & WS (websockets) Apache HTTP Server Version 2.4 https://httpd.apache.org/doc ...

  3. npm & cli & node.js

    npm & cli & node.js https://www.npmjs.com/ https://www.npmjs.com/settings/xgqfrms/packages h ...

  4. WebAssembly in Action

    WebAssembly in Action 数据加密,反爬虫,防盗链,版权保护,数据追踪,埋点 blogs 加密,js 禁用检测,权限控制 WebAssembly 防盗链 wasm online id ...

  5. BGV币与YFI币、YFII币存在着怎样的相关性?

    大多数的玩家并没有长期的打算,而是更倾向于关注短期利好的币种.比如最近在圈内赚足眼球的YFI,之所以能够成为明星角色,并非它的技术和平台,而是因为它在短期就创造了86倍的暴涨.YFI币的暴涨在某种程度 ...

  6. ROS 安装完成后运行小乌龟示例程序

    安装ROS成功后,在Beginner Tutorials中有一个简单的示例程序. 在Terminal中运行以下命令: $ roscore 新开一个terminal,运行以下命令,打开小乌龟窗口: $ ...

  7. C++算法代码——扫雷游戏

    题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1685 题目描述 扫雷游戏是一款十分经典的单机小游戏. 在 n 行 m 列的雷区中有一 ...

  8. Mysql之用户认证授权管理

    概述 Mysql的认证采用账号密码方式,其中账号由两个部分组成:Host和User:Host为允许登录的客户端Ip,User为当前登录的用户名. 授权没有采用典型的RBAC(基于角色的访问控制),而是 ...

  9. Elasticsearch简介、倒排索引、文档基本操作、分词器

    lucene.Solr.Elasticsearch 1.倒排序索引 2.Lucene是类库 3.solr基于lucene 4.ES基于lucene 一.Elasticsearch 核心术语 特点: 1 ...

  10. 推荐一款好用的免费远程控制软件——ToDesk

    创作立场声明:我在本文中评测的软件为自用,感觉不错并且全免费,第一时间发出来和大家分享,欢迎理性观点交流碰撞. 疫情刚开始的时候,待在家里不能上班,但是还是有很多工作需要在线完成,常常需要跑回办公室拿 ...