本文介紹通過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());//輸出有背景的文本和顏色 } } } } }
運行程式,輸入獲取結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/270647.html
標籤:其他
