Class XWPFRun
- java.lang.Object
-
- org.apache.poi.xwpf.usermodel.XWPFRun
-
- All Implemented Interfaces:
CharacterRun,IRunElement,ISDTContents
- Direct Known Subclasses:
XWPFFieldRun,XWPFHyperlinkRun
public class XWPFRun extends java.lang.Object implements ISDTContents, IRunElement, CharacterRun
XWPFRun object defines a region of text with a common set of properties
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classXWPFRun.FontCharRange
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddBreak()Specifies that a break shall be placed at the current location in the run content.voidaddBreak(BreakClear clear)Specifies that a break shall be placed at the current location in the run content.voidaddBreak(BreakType type)Specifies that a break shall be placed at the current location in the run content.voidaddCarriageReturn()Specifies that a carriage return shall be placed at the current location in the run content.CTInlineaddChart(java.lang.String chartRelId)this method add chart template into documentXWPFPictureaddPicture(java.io.InputStream pictureData, int pictureType, java.lang.String filename, int width, int height)Adds a picture to the run.voidaddTab()Specifies that a tab shall be placed at the current location in the run content.intgetCharacterSpacing()java.lang.StringgetColor()Get text color.CTRgetCTR()Get the currently used CTR objectXWPFDocumentgetDocument()java.util.List<XWPFPicture>getEmbeddedPictures()Returns the embedded pictures of the run.STEm.EnumgetEmphasisMark()Get the emphasis mark value for the run.java.lang.StringgetFontFamily()Gets the fonts which shall be used to display the text contents of this run.java.lang.StringgetFontFamily(XWPFRun.FontCharRange fcr)Gets the font family for the specified font char range.java.lang.StringgetFontName()Alias forgetFontFamily()intgetFontSize()Specifies the font size which shall be applied to all non complex script characters in the contents of this run when displayed.intgetKerning()java.lang.StringgetLang()Get the language tag associated with this run, if any.XWPFParagraphgetParagraph()Deprecated.usegetParent()insteadIRunBodygetParent()Get the currently referenced paragraph/SDT objectjava.lang.StringgetPhonetic()java.lang.StringgetPictureText()Returns text embedded in picturesVerticalAligngetSubscript()Deprecated.useXWPFRun.getVerticalAlignmentjava.lang.StringgetText(int pos)Return the string content of this text runSTHighlightColor.EnumgetTextHightlightColor()Gets the highlight color for the runintgetTextPosition()This element specifies the amount by which text shall be raised or lowered for this run in relation to the default baseline of the surrounding non-positioned text.intgetTextScale()Gets the current text scale value.UnderlinePatternsgetUnderline()Get the underline setting for the run.java.lang.StringgetUnderlineColor()Get the underline color for the run's underline, if any.STThemeColor.EnumgetUnderlineThemeColor()Get the underline theme color for the run's underline, if any.STVerticalAlignRun.EnumgetVerticalAlignment()Get the vertical alignment valuebooleanisBold()Whether the bold property shall be applied to all non-complex script characters in the contents of this run when displayed in a documentbooleanisCapitalized()booleanisDoubleStrikeThrough()Specifies that the contents of this run shall be displayed with a double horizontal line through the center of the line.booleanisEmbossed()booleanisHighlighted()booleanisImprinted()booleanisItalic()Whether the italic property should be applied to all non-complex script characters in the contents of this run when displayed in a document.booleanisShadowed()booleanisSmallCaps()booleanisStrike()Deprecated.booleanisStrikeThrough()Specifies that the contents of this run shall be displayed with a single horizontal line through the center of the line.booleanisVanish()Get the vanish (hidden text) valuevoidremoveBreak()voidremoveCarriageReturn()voidremoveTab()voidsetBold(boolean value)Whether the bold property shall be applied to all non-complex script characters in the contents of this run when displayed in a document.voidsetCapitalized(boolean value)voidsetCharacterSpacing(int twips)voidsetColor(java.lang.String rgbStr)Set text color.voidsetDoubleStrikethrough(boolean value)Specifies that the contents of this run shall be displayed with a double horizontal line through the center of the line.voidsetEmbossed(boolean value)voidsetEmphasisMark(java.lang.String markType)Set the emphasis mark for the run.voidsetFontFamily(java.lang.String fontFamily)Specifies the fonts which shall be used to display the text contents of this run.voidsetFontFamily(java.lang.String fontFamily, XWPFRun.FontCharRange fcr)Specifies the fonts which shall be used to display the text contents of this run.voidsetFontSize(int size)Specifies the font size which shall be applied to all non complex script characters in the contents of this run when displayed.voidsetImprinted(boolean value)voidsetItalic(boolean value)Whether the bold property shall be applied to all non-complex script characters in the contents of this run when displayed in a documentvoidsetKerning(int kern)voidsetShadow(boolean value)voidsetSmallCaps(boolean value)voidsetStrike(boolean value)Deprecated.voidsetStrikeThrough(boolean value)Specifies that the contents of this run shall be displayed with a single horizontal line through the center of the line.voidsetStyle(java.lang.String styleId)Set the style ID for the run.voidsetSubscript(VerticalAlign valign)Specifies the alignment which shall be applied to the contents of this run in relation to the default appearance of the run's text.voidsetText(java.lang.String value)Sets the text of this text runvoidsetText(java.lang.String value, int pos)Sets the text of this text run in thevoidsetTextHighlightColor(java.lang.String colorName)Set the highlight color for the run.voidsetTextPosition(int val)This element specifies the amount by which text shall be raised or lowered for this run in relation to the default baseline of the surrounding non-positioned text.voidsetTextScale(int percentage)Set the text expand/collapse scale value.voidsetUnderline(UnderlinePatterns value)Specifies that the contents of this run should be displayed along with an underline appearing directly below the character height.voidsetUnderlineColor(java.lang.String color)Set the underline color for the run's underline, if any.voidsetUnderlineThemeColor(java.lang.String themeColor)Set the underline theme color for the run's underline, if any.voidsetVanish(boolean value)The vanish (hidden text) property for the run.voidsetVerticalAlignment(java.lang.String verticalAlignment)Set the vertical alignment of the run.java.lang.Stringtext()Returns the string version of the text, with tabs and carriage returns in place of their xml equivalents.java.lang.StringtoString()Returns the string version of the text and the phonetic string
-
-
-
Constructor Detail
-
XWPFRun
public XWPFRun(CTR r, IRunBody p)
- Parameters:
r- the CTR bean which holds the run attributesp- the parent paragraph
-
XWPFRun
@Deprecated public XWPFRun(CTR r, XWPFParagraph p)
Deprecated.
-
-
Method Detail
-
getParent
public IRunBody getParent()
Get the currently referenced paragraph/SDT object- Returns:
- current parent
-
getParagraph
@Deprecated public XWPFParagraph getParagraph()
Deprecated.usegetParent()insteadGet the currently referenced paragraph, or null if a SDT object
-
getDocument
public XWPFDocument getDocument()
- Returns:
- The
XWPFDocumentinstance, this run belongs to, ornullif parent structure (paragraph > document) is not properly set.
-
getLang
public java.lang.String getLang()
Get the language tag associated with this run, if any.- Returns:
- the language tag associated with this run, if any
-
isBold
public boolean isBold()
Whether the bold property shall be applied to all non-complex script characters in the contents of this run when displayed in a document- Specified by:
isBoldin interfaceCharacterRun- Returns:
trueif the bold property is applied
-
setBold
public void setBold(boolean value)
Whether the bold property shall be applied to all non-complex script characters in the contents of this run when displayed in a document.This formatting property is a toggle property, which specifies that its behavior differs between its use within a style definition and its use as direct formatting. When used as part of a style definition, setting this property shall toggle the current state of that property as specified up to this point in the hierarchy (i.e. applied to not applied, and vice versa). Setting it to
false(or an equivalent) shall result in the current setting remaining unchanged. However, when used as direct formatting, setting this property to true or false shall set the absolute state of the resulting property.If this element is not present, the default value is to leave the formatting applied at previous level in the style hierarchy. If this element is never applied in the style hierarchy, then bold shall not be applied to non-complex script characters.
- Specified by:
setBoldin interfaceCharacterRun- Parameters:
value-trueif the bold property is applied to this run
-
getColor
public java.lang.String getColor()
Get text color. The returned value is a string in the hex form "RRGGBB".
-
setColor
public void setColor(java.lang.String rgbStr)
Set text color.- Parameters:
rgbStr- - the desired color, in the hex form "RRGGBB".
-
getText
public java.lang.String getText(int pos)
Return the string content of this text run- Returns:
- the text of this text run or
nullif not set
-
getPictureText
public java.lang.String getPictureText()
Returns text embedded in pictures
-
setText
public void setText(java.lang.String value)
Sets the text of this text run- Parameters:
value- the literal text which shall be displayed in the document
-
setText
public void setText(java.lang.String value, int pos)Sets the text of this text run in the- Parameters:
value- the literal text which shall be displayed in the documentpos- - position in the text array (NB: 0 based)
-
isItalic
public boolean isItalic()
Whether the italic property should be applied to all non-complex script characters in the contents of this run when displayed in a document.- Specified by:
isItalicin interfaceCharacterRun- Returns:
trueif the italic property is applied
-
setItalic
public void setItalic(boolean value)
Whether the bold property shall be applied to all non-complex script characters in the contents of this run when displayed in a documentThis formatting property is a toggle property, which specifies that its behavior differs between its use within a style definition and its use as direct formatting. When used as part of a style definition, setting this property shall toggle the current state of that property as specified up to this point in the hierarchy (i.e. applied to not applied, and vice versa). Setting it to
false(or an equivalent) shall result in the current setting remaining unchanged. However, when used as direct formatting, setting this property to true or false shall set the absolute state of the resulting property.If this element is not present, the default value is to leave the formatting applied at previous level in the style hierarchy. If this element is never applied in the style hierarchy, then bold shall not be applied to non-complex script characters.
- Specified by:
setItalicin interfaceCharacterRun- Parameters:
value-trueif the italic property is applied to this run
-
getUnderline
public UnderlinePatterns getUnderline()
Get the underline setting for the run.- Returns:
- the Underline pattern applied to this run
-
setUnderline
public void setUnderline(UnderlinePatterns value)
Specifies that the contents of this run should be displayed along with an underline appearing directly below the character height.If this element is not present, the default value is to leave the formatting applied at previous level in the style hierarchy. If this element is never applied in the style hierarchy, then an underline shall not be applied to the contents of this run.
- Parameters:
value- - underline type
-
setUnderlineColor
public void setUnderlineColor(java.lang.String color)
Set the underline color for the run's underline, if any.- Parameters:
color- An RGB color value (e.g, "a0C6F3") or "auto".- Since:
- 4.0.0
-
setUnderlineThemeColor
public void setUnderlineThemeColor(java.lang.String themeColor)
Set the underline theme color for the run's underline, if any.- Parameters:
themeColor- A theme color name (seeSTThemeColor.Enum).- Since:
- 4.0.0
-
getUnderlineThemeColor
public STThemeColor.Enum getUnderlineThemeColor()
Get the underline theme color for the run's underline, if any.- Returns:
- The
STThemeColor.Enum. - Since:
- 4.0.0
-
getUnderlineColor
public java.lang.String getUnderlineColor()
Get the underline color for the run's underline, if any.- Returns:
- The RGB color value as as a string of hexadecimal digits (e.g., "A0B2F1") or "auto".
- Since:
- 4.0.0
-
isStrikeThrough
public boolean isStrikeThrough()
Specifies that the contents of this run shall be displayed with a single horizontal line through the center of the line.- Specified by:
isStrikeThroughin interfaceCharacterRun- Returns:
trueif the strike property is applied
-
setStrikeThrough
public void setStrikeThrough(boolean value)
Specifies that the contents of this run shall be displayed with a single horizontal line through the center of the line.This formatting property is a toggle property, which specifies that its behaviour differs between its use within a style definition and its use as direct formatting. When used as part of a style definition, setting this property shall toggle the current state of that property as specified up to this point in the hierarchy (i.e. applied to not applied, and vice versa). Setting it to false (or an equivalent) shall result in the current setting remaining unchanged. However, when used as direct formatting, setting this property to true or false shall set the absolute state of the resulting property.
If this element is not present, the default value is to leave the formatting applied at previous level in the style hierarchy. If this element is never applied in the style hierarchy, then strikethrough shall not be applied to the contents of this run.
- Specified by:
setStrikeThroughin interfaceCharacterRun- Parameters:
value-trueif the strike property is applied to this run
-
isStrike
@Deprecated public boolean isStrike()
Deprecated.
-
setStrike
@Deprecated public void setStrike(boolean value)
Deprecated.
-
isDoubleStrikeThrough
public boolean isDoubleStrikeThrough()
Specifies that the contents of this run shall be displayed with a double horizontal line through the center of the line.- Specified by:
isDoubleStrikeThroughin interfaceCharacterRun- Returns:
trueif the double strike property is applied
-
setDoubleStrikethrough
public void setDoubleStrikethrough(boolean value)
Specifies that the contents of this run shall be displayed with a double horizontal line through the center of the line.- Specified by:
setDoubleStrikethroughin interfaceCharacterRun- See Also:
for the rules about this
-
isSmallCaps
public boolean isSmallCaps()
- Specified by:
isSmallCapsin interfaceCharacterRun
-
setSmallCaps
public void setSmallCaps(boolean value)
- Specified by:
setSmallCapsin interfaceCharacterRun
-
isCapitalized
public boolean isCapitalized()
- Specified by:
isCapitalizedin interfaceCharacterRun
-
setCapitalized
public void setCapitalized(boolean value)
- Specified by:
setCapitalizedin interfaceCharacterRun
-
isShadowed
public boolean isShadowed()
- Specified by:
isShadowedin interfaceCharacterRun
-
setShadow
public void setShadow(boolean value)
- Specified by:
setShadowin interfaceCharacterRun
-
isImprinted
public boolean isImprinted()
- Specified by:
isImprintedin interfaceCharacterRun
-
setImprinted
public void setImprinted(boolean value)
- Specified by:
setImprintedin interfaceCharacterRun
-
isEmbossed
public boolean isEmbossed()
- Specified by:
isEmbossedin interfaceCharacterRun
-
setEmbossed
public void setEmbossed(boolean value)
- Specified by:
setEmbossedin interfaceCharacterRun
-
getSubscript
@Removal(version="4.2") public VerticalAlign getSubscript()
Deprecated.useXWPFRun.getVerticalAlignmentSpecifies the alignment which shall be applied to the contents of this run in relation to the default appearance of the run's text. This allows the text to be repositioned as subscript or superscript without altering the font size of the run properties.- Returns:
- VerticalAlign
-
setSubscript
public void setSubscript(VerticalAlign valign)
Specifies the alignment which shall be applied to the contents of this run in relation to the default appearance of the run's text. This allows the text to be repositioned as subscript or superscript without altering the font size of the run properties.If this element is not present, the default value is to leave the formatting applied at previous level in the style hierarchy. If this element is never applied in the style hierarchy, then the text shall not be subscript or superscript relative to the default baseline location for the contents of this run.
- Parameters:
valign- Type of vertical align to apply- See Also:
VerticalAlign
-
getKerning
public int getKerning()
- Specified by:
getKerningin interfaceCharacterRun
-
setKerning
public void setKerning(int kern)
- Specified by:
setKerningin interfaceCharacterRun
-
isHighlighted
public boolean isHighlighted()
- Specified by:
isHighlightedin interfaceCharacterRun
-
getCharacterSpacing
public int getCharacterSpacing()
- Specified by:
getCharacterSpacingin interfaceCharacterRun
-
setCharacterSpacing
public void setCharacterSpacing(int twips)
- Specified by:
setCharacterSpacingin interfaceCharacterRun
-
getFontFamily
public java.lang.String getFontFamily()
Gets the fonts which shall be used to display the text contents of this run. Specifies a font which shall be used to format all characters in the ASCII range (0 - 127) within the parent run- Returns:
- a string representing the font family
-
setFontFamily
public void setFontFamily(java.lang.String fontFamily)
Specifies the fonts which shall be used to display the text contents of this run. Specifies a font which shall be used to format all characters in the ASCII range (0 - 127) within the parent run.Also sets the other font ranges, if they haven't been set before
- Parameters:
fontFamily- The font family to apply- See Also:
XWPFRun.FontCharRange
-
getFontName
public java.lang.String getFontName()
Alias forgetFontFamily()- Specified by:
getFontNamein interfaceCharacterRun- Returns:
- a string representing the font
-
getFontFamily
public java.lang.String getFontFamily(XWPFRun.FontCharRange fcr)
Gets the font family for the specified font char range. If fcr is null, the font char range "ascii" is used- Parameters:
fcr- the font char range, defaults to "ansi"- Returns:
- a string representing the font famil
-
setFontFamily
public void setFontFamily(java.lang.String fontFamily, XWPFRun.FontCharRange fcr)Specifies the fonts which shall be used to display the text contents of this run. The default handling for fcr == null is to overwrite the ascii font char range with the given font family and also set all not specified font ranges- Parameters:
fontFamily- The font family to applyfcr- FontCharRange or null for default handling
-
getFontSize
public int getFontSize()
Specifies the font size which shall be applied to all non complex script characters in the contents of this run when displayed.- Specified by:
getFontSizein interfaceCharacterRun- Returns:
- value representing the font size
-
setFontSize
public void setFontSize(int size)
Specifies the font size which shall be applied to all non complex script characters in the contents of this run when displayed.If this element is not present, the default value is to leave the value applied at previous level in the style hierarchy. If this element is never applied in the style hierarchy, then any appropriate font size may be used for non complex script characters.
- Specified by:
setFontSizein interfaceCharacterRun- Parameters:
size- The font size as number of point measurements.
-
getTextPosition
public int getTextPosition()
This element specifies the amount by which text shall be raised or lowered for this run in relation to the default baseline of the surrounding non-positioned text. This allows the text to be repositioned without altering the font size of the contents.- Returns:
- a big integer representing the amount of text shall be "moved"
-
setTextPosition
public void setTextPosition(int val)
This element specifies the amount by which text shall be raised or lowered for this run in relation to the default baseline of the surrounding non-positioned text. This allows the text to be repositioned without altering the font size of the contents.If the val attribute is positive, then the parent run shall be raised above the baseline of the surrounding text by the specified number of half-points. If the val attribute is negative, then the parent run shall be lowered below the baseline of the surrounding text by the specified number of half-points.
If this element is not present, the default value is to leave the formatting applied at previous level in the style hierarchy. If this element is never applied in the style hierarchy, then the text shall not be raised or lowered relative to the default baseline location for the contents of this run.
- Parameters:
val- Positive values will raise the baseline of the text, negative values will lower it.
-
removeBreak
public void removeBreak()
-
addBreak
public void addBreak()
Specifies that a break shall be placed at the current location in the run content. A break is a special character which is used to override the normal line breaking that would be performed based on the normal layout of the document's contents.- See Also:
addCarriageReturn()
-
addBreak
public void addBreak(BreakType type)
Specifies that a break shall be placed at the current location in the run content. A break is a special character which is used to override the normal line breaking that would be performed based on the normal layout of the document's contents.The behavior of this break character (the location where text shall be restarted after this break) shall be determined by its type values.
- See Also:
BreakType
-
addBreak
public void addBreak(BreakClear clear)
Specifies that a break shall be placed at the current location in the run content. A break is a special character which is used to override the normal line breaking that would be performed based on the normal layout of the document's contents.The behavior of this break character (the location where text shall be restarted after this break) shall be determined by its type (in this case is BreakType.TEXT_WRAPPING as default) and clear attribute values.
- See Also:
BreakClear
-
addTab
public void addTab()
Specifies that a tab shall be placed at the current location in the run content.
-
removeTab
public void removeTab()
-
addCarriageReturn
public void addCarriageReturn()
Specifies that a carriage return shall be placed at the current location in the run content. A carriage return is used to end the current line of text in Wordprocess. The behavior of a carriage return in run content shall be identical to a break character with null type and clear attributes, which shall end the current line and find the next available line on which to continue. The carriage return character forced the following text to be restarted on the next available line in the document.
-
removeCarriageReturn
public void removeCarriageReturn()
-
addPicture
public XWPFPicture addPicture(java.io.InputStream pictureData, int pictureType, java.lang.String filename, int width, int height) throws InvalidFormatException, java.io.IOException
Adds a picture to the run. This method handles attaching the picture data to the overall file.- Parameters:
pictureData- The raw picture datapictureType- The type of the picture, egDocument.PICTURE_TYPE_JPEGwidth- width in EMUs. To convert to / from points useUnitsheight- height in EMUs. To convert to / from points useUnits- Throws:
InvalidFormatException- If the format of the picture is not known.java.io.IOException- If reading the picture-data from the stream fails.- See Also:
Document.PICTURE_TYPE_EMF,Document.PICTURE_TYPE_WMF,Document.PICTURE_TYPE_PICT,Document.PICTURE_TYPE_JPEG,Document.PICTURE_TYPE_PNG,Document.PICTURE_TYPE_DIB
-
addChart
@Internal public CTInline addChart(java.lang.String chartRelId) throws InvalidFormatException, java.io.IOException
this method add chart template into document- Parameters:
chartRelId- relation id of chart in document relation file- Throws:
InvalidFormatExceptionjava.io.IOException- Since:
- POI 4.0.0
-
getEmbeddedPictures
public java.util.List<XWPFPicture> getEmbeddedPictures()
Returns the embedded pictures of the run. These are pictures which reference an external, embedded picture image such as a .png or .jpg
-
setStyle
public void setStyle(java.lang.String styleId)
Set the style ID for the run.- Parameters:
styleId- ID (not name) of the style to set for the run, e.g. "BoldItalic" (not "Bold Italic").
-
toString
public java.lang.String toString()
Returns the string version of the text and the phonetic string- Overrides:
toStringin classjava.lang.Object
-
text
public java.lang.String text()
Returns the string version of the text, with tabs and carriage returns in place of their xml equivalents.- Specified by:
textin interfaceCharacterRun- Returns:
- The text of the run, including any tabs/spaces/etc
-
getPhonetic
public java.lang.String getPhonetic()
- Returns:
- the phonetic (ruby) string associated with this run or an empty String if none exists
-
setTextScale
public void setTextScale(int percentage)
Set the text expand/collapse scale value.- Parameters:
percentage- The percentage to expand or compress the text- Since:
- 4.0.0
-
getTextScale
public int getTextScale()
Gets the current text scale value.- Returns:
- Value is an integer percentage
- Since:
- 4.0.0
-
setTextHighlightColor
public void setTextHighlightColor(java.lang.String colorName)
Set the highlight color for the run. Silently does nothing of colorName is not a recognized value.- Parameters:
colorName- The name of the color as defined in the ST_HighlightColor simple type (STHightlightColor)- Since:
- 4.0.0
-
getTextHightlightColor
public STHighlightColor.Enum getTextHightlightColor()
Gets the highlight color for the run- Returns:
STHighlightColorfor the run.- Since:
- 4.0.0
-
isVanish
public boolean isVanish()
Get the vanish (hidden text) value- Returns:
- True if the run is hidden text.
- Since:
- 4.0.0
-
setVanish
public void setVanish(boolean value)
The vanish (hidden text) property for the run.- Parameters:
value- Set to true to make the run hidden text.- Since:
- 4.0.0
-
getVerticalAlignment
public STVerticalAlignRun.Enum getVerticalAlignment()
Get the vertical alignment value- Returns:
STVerticalAlignRun.Enumvalue (see 22.9.2.17 ST_VerticalAlignRun (Vertical Positioning Location))- Since:
- 4.0.0
-
setVerticalAlignment
public void setVerticalAlignment(java.lang.String verticalAlignment)
Set the vertical alignment of the run.- Parameters:
verticalAlignment- Vertical alignment value, one of "baseline", "superscript", or "subscript".- Since:
- 4.0.0
-
getEmphasisMark
public STEm.Enum getEmphasisMark()
Get the emphasis mark value for the run.- Returns:
STEm.Enumemphasis mark type enumeration. See 17.18.24 ST_Em (Emphasis Mark Type).- Since:
- 4.0.0
-
setEmphasisMark
public void setEmphasisMark(java.lang.String markType)
Set the emphasis mark for the run. The emphasis mark goes above or below the run text.- Parameters:
markType- Emphasis mark type name, e.g., "dot" or "none". See 17.18.24 ST_Em (Emphasis Mark Type)- Since:
- 4.0.0
-
-