Java 读取Word文本/段落格式属性

摘要:
本文介绍了通过Java后端程序代码读取Word文本和段落格式的方法。1.8.0通过文本范围读取文本字符串格式。getCharacterFormat()方法。

本文介绍通过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 读取Word文本/段落格式属性第1张

 

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 +"
"
            +"字体大小:"+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文本/段落格式属性第2张

 

 

 

 

免责声明:文章转载自《Java 读取Word文本/段落格式属性》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇redis的基本用法CSS 垂直居中下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

自己动手写中文分词解析器完整教程,并对出现的问题进行探讨和解决(附完整c#代码和相关dll文件、txt文件下载)

中文分词插件很多,当然都有各自的优缺点,近日刚接触自然语言处理这方面的,初步体验中文分词。 首先感谢harry.guo楼主提供的学习资源,博文链接http://www.cnblogs.com/harryguo/archive/2007/09/26/906965.html,在此基础上进行深入学习和探讨。 接下来进入正文。。。大牛路过别喷,菜鸟有空练练手~~完...

转载 java枚举类型enum的使用 (原文地址:http://blog.csdn.net/wgw335363240/article/details/6359614)

java枚举类型enum的使用 最近跟同事讨论问题的时候,突然同事提到我们为什么java中定义的常量值不采用enmu枚举类型,而采用public final static 类型来定义呢?以前我们都是采用这种方式定义的,很少采用enum定义,所以也都没有注意过,面对突入起来的问题,还真有点不太清楚为什么有这样的定义。既然不明白就抽时间研究下吧。 Java中的...

使用腾讯语音合成技术生成有声书

  背景:不知是否在博客园看到的腾讯云平台广告,被AI接口几个项目吸引住了,其中有个   语音合成  接口在这里安利一下,还挺好玩。这个接口提供将一段文字转换成语音的功能,支持中文、英文,遗憾的是暂时无法通过自己的声音进行训练,推出自己独有声音的音频文件:) 不过总体来说,还是相当不错啦,附件中是我用这个接口转换的样例音频文件。 DEMO实测,代码案例简单...

Windows-快速预览文件-QuickLook

开源、免费的文件快速预览工具, 支持图片、文档、音视频、代码文本、压缩包等多种格式。 获得 Mac OS 空格键快速预览文件相同的体验 效果图 文件夹 音视频 浏览 压缩包,文本 支持的格式: 图片:.png, .jpg, .bmp, .gif, .psd, .apng,RAW; 压缩包:.zip, .rar, .tar.gz, .7z;...

远程Get,Post请求工具类

1.远程请求工具类   import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public class ExoticHttpRequest { /** * 向指定URL...

JXL操作Excel

      jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI。其中功能相对POI比较弱一点。但jExcelAPI对中文支持非常好,API是纯Java的, 并不 依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。 另外需要说明的是,...