Java读取Word文本段落格式属性
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java读取Word⽂本段落格式属性
本⽂介绍通过Java后端程序代码来读取Word⽂本和段落格式的⽅法。
本次测试环境如下:
Word版本:2013
编译环境:IntelliJ IDEA2018
Work库:free spire.doc.jar
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
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()
getPageBreakBefore()
getSuppressAutoHyphens()
getTabs()
⽤于测试的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());//输出有背景的⽂本和颜⾊
}
}
}
}
}
运⾏程序,输⼊获取结果:。