public class Font
You do not create instances of the Example: Example: Example:
Document doc = new Document();
Run run = new Run(doc, "Hello world!");
Font font = run.getFont();
font.setName("Courier New");
font.setSize(36.0);
font.setHighlightColor(Color.YELLOW);
doc.getFirstSection().getBody().getFirstParagraph().appendChild(run);
doc.save(getArtifactsDir() + "Font.CreateFormattedRun.docx");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().getBorder().setColor(Color.GREEN);
builder.getFont().getBorder().setLineWidth(2.5);
builder.getFont().getBorder().setLineStyle(LineStyle.DASH_DOT_STROKER);
builder.write("Text surrounded by green border.");
doc.save(getArtifactsDir() + "Border.FontBorder.docx");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a paragraph style and specify some formatting for it
Style style = doc.getStyles().add(StyleType.PARAGRAPH, "MyStyle1");
style.getFont().setSize(24.0);
style.getFont().setName("Verdana");
style.getParagraphFormat().setSpaceAfter(12.0);
// Create a list and make sure the paragraphs that use this style will use this list
style.getListFormat().setList(doc.getLists().add(ListTemplate.BULLET_DEFAULT));
style.getListFormat().setListLevelNumber(0);
// Apply the paragraph style to the current paragraph in the document and add some text
builder.getParagraphFormat().setStyle(style);
builder.writeln("Hello World: MyStyle1, bulleted list.");
// Change to a paragraph style that has no list formatting
builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
builder.writeln("Hello World: Normal.");
builder.getDocument().save(getArtifactsDir() + "Styles.ParagraphStyleBulletedList.docx");
Property Getters/Setters Summary | ||
---|---|---|
boolean | getAllCaps() | |
void | setAllCaps(booleanvalue) | |
True if the font is formatted as all capital letters. | ||
java.awt.Color | getAutoColor() | |
Returns the present calculated color of the text (black or white) to be used for 'auto color'.
If the color is not 'auto' then returns |
||
boolean | getBidi() | |
void | setBidi(booleanvalue) | |
Specifies whether the contents of this run shall have right-to-left characteristics. | ||
boolean | getBold() | |
void | setBold(booleanvalue) | |
True if the font is formatted as bold. | ||
boolean | getBoldBi() | |
void | setBoldBi(booleanvalue) | |
True if the right-to-left text is formatted as bold. | ||
Border | getBorder() | |
Returns a Border object that specifies border for the font.
|
||
java.awt.Color | getColor() | |
void | setColor(java.awt.Colorvalue) | |
Gets or sets the color of the font. | ||
boolean | getComplexScript() | |
void | setComplexScript(booleanvalue) | |
Specifies whether the contents of this run shall be treated as complex script text regardless of their Unicode character values when determining the formatting for this run. | ||
boolean | getDoubleStrikeThrough() | |
void | setDoubleStrikeThrough(booleanvalue) | |
True if the font is formatted as double strikethrough text. | ||
boolean | getEmboss() | |
void | setEmboss(booleanvalue) | |
True if the font is formatted as embossed. | ||
int | getEmphasisMark() | |
void | setEmphasisMark(intvalue) | |
Gets or sets the emphasis mark applied to this formatting. The value of the property is EmphasisMark integer constant. | ||
boolean | getEngrave() | |
void | setEngrave(booleanvalue) | |
True if the font is formatted as engraved. | ||
boolean | getHidden() | |
void | setHidden(booleanvalue) | |
True if the font is formatted as hidden text. | ||
java.awt.Color | getHighlightColor() | |
void | setHighlightColor(java.awt.Colorvalue) | |
Gets or sets the highlight (marker) color. | ||
boolean | getItalic() | |
void | setItalic(booleanvalue) | |
True if the font is formatted as italic. | ||
boolean | getItalicBi() | |
void | setItalicBi(booleanvalue) | |
True if the right-to-left text is formatted as italic. | ||
double | getKerning() | |
void | setKerning(doublevalue) | |
Gets or sets the font size at which kerning starts. | ||
double | getLineSpacing() | |
Returns line spacing of this font (in points).
|
||
int | getLocaleId() | |
void | setLocaleId(intvalue) | |
Gets or sets the locale identifier (language) of the formatted characters. | ||
int | getLocaleIdBi() | |
void | setLocaleIdBi(intvalue) | |
Gets or sets the locale identifier (language) of the formatted right-to-left characters. | ||
int | getLocaleIdFarEast() | |
void | setLocaleIdFarEast(intvalue) | |
Gets or sets the locale identifier (language) of the formatted Asian characters. | ||
java.lang.String | getName() | |
void | setName(java.lang.Stringvalue) | |
Gets or sets the name of the font. | ||
java.lang.String | getNameAscii() | |
void | setNameAscii(java.lang.Stringvalue) | |
Returns or sets the font used for Latin text (characters with character codes from 0 (zero) through 127). | ||
java.lang.String | getNameBi() | |
void | setNameBi(java.lang.Stringvalue) | |
Returns or sets the name of the font in a right-to-left language document. | ||
java.lang.String | getNameFarEast() | |
void | setNameFarEast(java.lang.Stringvalue) | |
Returns or sets an East Asian font name. | ||
java.lang.String | getNameOther() | |
void | setNameOther(java.lang.Stringvalue) | |
Returns or sets the font used for characters with character codes from 128 through 255. | ||
boolean | getNoProofing() | |
void | setNoProofing(booleanvalue) | |
True when the formatted characters are not to be spell checked. | ||
boolean | getOutline() | |
void | setOutline(booleanvalue) | |
True if the font is formatted as outline. | ||
double | getPosition() | |
void | setPosition(doublevalue) | |
Gets or sets the position of text (in points) relative to the base line. A positive number raises the text, and a negative number lowers it. | ||
int | getScaling() | |
void | setScaling(intvalue) | |
Gets or sets character width scaling in percent. | ||
Shading | getShading() | |
Returns a Shading object that refers to the shading formatting for the font.
|
||
boolean | getShadow() | |
void | setShadow(booleanvalue) | |
True if the font is formatted as shadowed. | ||
double | getSize() | |
void | setSize(doublevalue) | |
Gets or sets the font size in points. | ||
double | getSizeBi() | |
void | setSizeBi(doublevalue) | |
Gets or sets the font size in points used in a right-to-left document. | ||
boolean | getSmallCaps() | |
void | setSmallCaps(booleanvalue) | |
True if the font is formatted as small capital letters. | ||
boolean | getSnapToGrid() | |
void | setSnapToGrid(booleanvalue) | |
Specifies whether the current font should use the document grid characters per line settings when laying out. | ||
double | getSpacing() | |
void | setSpacing(doublevalue) | |
Returns or sets the spacing (in points) between characters . | ||
boolean | getStrikeThrough() | |
void | setStrikeThrough(booleanvalue) | |
True if the font is formatted as strikethrough text. | ||
Style | getStyle() | |
void | ||
Gets or sets the character style applied to this formatting. | ||
int | getStyleIdentifier() | |
void | setStyleIdentifier(intvalue) | |
Gets or sets the locale independent style identifier of the character style applied to this formatting. The value of the property is StyleIdentifier integer constant. | ||
java.lang.String | getStyleName() | |
void | setStyleName(java.lang.Stringvalue) | |
Gets or sets the name of the character style applied to this formatting. | ||
boolean | getSubscript() | |
void | setSubscript(booleanvalue) | |
True if the font is formatted as subscript. | ||
boolean | getSuperscript() | |
void | setSuperscript(booleanvalue) | |
True if the font is formatted as superscript. | ||
int | getTextEffect() | |
void | setTextEffect(intvalue) | |
Gets or sets the font animation effect. The value of the property is TextEffect integer constant. | ||
int | getUnderline() | |
void | setUnderline(intvalue) | |
Gets or sets the type of underline applied to the font. The value of the property is Underline integer constant. | ||
java.awt.Color | getUnderlineColor() | |
void | setUnderlineColor(java.awt.Colorvalue) | |
Gets or sets the color of the underline applied to the font. |
Method Summary | ||
---|---|---|
void | clearFormatting() | |
Resets to default font formatting.
|
||
boolean | hasDmlEffect(int dmlEffectType) | |
Checks if particular DrawingML text effect is applied.
|
public boolean getAllCaps() / public void setAllCaps(boolean value)
Example:
Shows how to format a run to display its contents in capitals.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); // There are two ways of getting a run to display its lowercase text in uppercase without changing the contents. // 1 - Set the AllCaps flag to display all characters in regular capitals: Run run = new Run(doc, "all capitals"); run.getFont().setAllCaps(true); para.appendChild(run); para = (Paragraph)para.getParentNode().appendChild(new Paragraph(doc)); // 2 - Set the SmallCaps flag to display all characters in small capitals: // If a character is lower case, it will appear in its upper case form // but will have the same height as the lower case (the font's x-height). // Characters that were in upper case originally will look the same. run = new Run(doc, "Small Capitals"); run.getFont().setSmallCaps(true); para.appendChild(run); doc.save(getArtifactsDir() + "Font.Caps.docx");
public java.awt.Color getAutoColor()
When text has 'automatic color', the actual color of text is calculated automatically so that it is readable against the background color. As you change the background color, the text color will automatically switch to black or white in MS Word to maximize legibility.
public boolean getBidi() / public void setBidi(boolean value)
This property, when on, shall not be used with strongly left-to-right text. Any behavior under that condition is unspecified. This property, when off, shall not be used with strong right-to-left text. Any behavior under that condition is unspecified.
When the contents of this run are displayed, all characters shall be treated as complex script characters for formatting
purposes. This means that
Also, when the contents of this run are displayed, this property acts as a right-to-left override for characters which are classified as "weak types" and "neutral types".
Example:
Shows how to define separate sets of font settings for right-to-left, and right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Define a set of font settings for left-to-right text. builder.getFont().setName("Courier New"); builder.getFont().setSize(16.0); builder.getFont().setItalic(false); builder.getFont().setBold(false); builder.getFont().setLocaleId(1033); // Define another set of font settings for right-to-left text. builder.getFont().setNameBi("Andalus"); builder.getFont().setSizeBi(48.0); // Specify that the right-to-left text in this run is bold and italic builder.getFont().setItalicBi(true); builder.getFont().setBoldBi(true); builder.getFont().setLocaleIdBi(1025); // We can use the Bidi flag to indicate whether the text we are about to add // with the document builder is right-to-left. When we add text with this flag set to true, // it will be formatted using the right-to-left set of font settings. builder.getFont().setBidi(true); builder.write("مرحبًا"); // Set the flag to false, and then add left-to-right text. // The document builder will format these using the left-to-right set of font settings. builder.getFont().setBidi(false); builder.write(" Hello world!"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
public boolean getBold() / public void setBold(boolean value)
Example:
Shows how to insert formatted text using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting, then add text. Font font = builder.getFont(); font.setSize(16.0); font.setBold(true); font.setColor(Color.BLUE); font.setName("Courier New"); font.setUnderline(Underline.DASH); builder.write("Hello world!");
public boolean getBoldBi() / public void setBoldBi(boolean value)
Example:
Shows how to define separate sets of font settings for right-to-left, and right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Define a set of font settings for left-to-right text. builder.getFont().setName("Courier New"); builder.getFont().setSize(16.0); builder.getFont().setItalic(false); builder.getFont().setBold(false); builder.getFont().setLocaleId(1033); // Define another set of font settings for right-to-left text. builder.getFont().setNameBi("Andalus"); builder.getFont().setSizeBi(48.0); // Specify that the right-to-left text in this run is bold and italic builder.getFont().setItalicBi(true); builder.getFont().setBoldBi(true); builder.getFont().setLocaleIdBi(1025); // We can use the Bidi flag to indicate whether the text we are about to add // with the document builder is right-to-left. When we add text with this flag set to true, // it will be formatted using the right-to-left set of font settings. builder.getFont().setBidi(true); builder.write("مرحبًا"); // Set the flag to false, and then add left-to-right text. // The document builder will format these using the left-to-right set of font settings. builder.getFont().setBidi(false); builder.write(" Hello world!"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
public Border getBorder()
Example:
Shows how to insert a string surrounded by a border into a document.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().getBorder().setColor(Color.GREEN); builder.getFont().getBorder().setLineWidth(2.5); builder.getFont().getBorder().setLineStyle(LineStyle.DASH_DOT_STROKER); builder.write("Text surrounded by green border."); doc.save(getArtifactsDir() + "Border.FontBorder.docx");
public java.awt.Color getColor() / public void setColor(java.awt.Color value)
Example:
Shows how to insert formatted text using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting, then add text. Font font = builder.getFont(); font.setSize(16.0); font.setBold(true); font.setColor(Color.BLUE); font.setName("Courier New"); font.setUnderline(Underline.DASH); builder.write("Hello world!");
Example:
Shows how to insert a hyperlink field.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("For more information, please visit the "); // Insert a hyperlink, and apply formatting to emphasize it. // The hyperlink will be a clickable piece of text which will take us to the location specified in the URL. builder.getFont().setColor(Color.BLUE); builder.getFont().setUnderline(Underline.SINGLE); builder.insertHyperlink("Aspose website", "http://www.aspose.com", false); builder.getFont().clearFormatting(); builder.writeln("."); // Ctrl + left clicking the link in the text in Microsoft Word will take us to the URL via a new web browser window. doc.save(getArtifactsDir() + "DocumentBuilder.InsertHyperlink.docx");
public boolean getComplexScript() / public void setComplexScript(boolean value)
Example:
Shows how to add text that is always treated as complex script.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setComplexScript(true); builder.writeln("Text treated as complex script."); doc.save(getArtifactsDir() + "Font.ComplexScript.docx");
public boolean getDoubleStrikeThrough() / public void setDoubleStrikeThrough(boolean value)
Example:
Shows how to add a line strikethrough to text.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); Run run = new Run(doc, "Text with a single-line strikethrough."); run.getFont().setStrikeThrough(true); para.appendChild(run); para = (Paragraph)para.getParentNode().appendChild(new Paragraph(doc)); run = new Run(doc, "Text with a double-line strikethrough."); run.getFont().setDoubleStrikeThrough(true); para.appendChild(run); doc.save(getArtifactsDir() + "Font.StrikeThrough.docx");
public boolean getEmboss() / public void setEmboss(boolean value)
Example:
Shows how to apply engraving/embossing effects to text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setSize(36.0); builder.getFont().setColor(Color.WHITE); // Below are two ways of using shadows to apply a 3D-like effect to the text. // 1 - Engrave text to make it look like the letters are sunken into the page: builder.getFont().setEngrave(true); builder.writeln("This text is engraved."); // 2 - Emboss text to make it look like the letters pop out of the page: builder.getFont().setEngrave(false); builder.getFont().setEmboss(true); builder.writeln("This text is embossed."); doc.save(getArtifactsDir() + "Font.EngraveEmboss.docx");
public int getEmphasisMark() / public void setEmphasisMark(int value)
Example:
Shows how to add additional character rendered above/below the glyph-character.DocumentBuilder builder = new DocumentBuilder(); // Possible types of emphasis mark: // https://apireference.aspose.com/words/net/aspose.words/emphasismark builder.getFont().setEmphasisMark(emphasisMark); builder.write("Emphasis text"); builder.writeln(); builder.getFont().clearFormatting(); builder.write("Simple text"); builder.getDocument().save(getArtifactsDir() + "Fonts.SetEmphasisMark.docx");
public boolean getEngrave() / public void setEngrave(boolean value)
Example:
Shows how to apply engraving/embossing effects to text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setSize(36.0); builder.getFont().setColor(Color.WHITE); // Below are two ways of using shadows to apply a 3D-like effect to the text. // 1 - Engrave text to make it look like the letters are sunken into the page: builder.getFont().setEngrave(true); builder.writeln("This text is engraved."); // 2 - Emboss text to make it look like the letters pop out of the page: builder.getFont().setEngrave(false); builder.getFont().setEmboss(true); builder.writeln("This text is embossed."); doc.save(getArtifactsDir() + "Font.EngraveEmboss.docx");
public boolean getHidden() / public void setHidden(boolean value)
Example:
Shows how to create a run of hidden text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // With the Hidden flag set to true, any text that we create using this Font object will be invisible in the document. // We will not see or highlight hidden text unless we enable the "Hidden text" option // found in Microsoft Word via File -> Options -> Display. The text will still be there, // and we will be able to access this text programmatically. // It is not advised to use this method to hide sensitive information. builder.getFont().setHidden(true); builder.getFont().setSize(36.0); builder.writeln("This text will not be visible in the document."); doc.save(getArtifactsDir() + "Font.Hidden.docx");
public java.awt.Color getHighlightColor() / public void setHighlightColor(java.awt.Color value)
Example:
Shows how to format a run of text using its font property.Document doc = new Document(); Run run = new Run(doc, "Hello world!"); Font font = run.getFont(); font.setName("Courier New"); font.setSize(36.0); font.setHighlightColor(Color.YELLOW); doc.getFirstSection().getBody().getFirstParagraph().appendChild(run); doc.save(getArtifactsDir() + "Font.CreateFormattedRun.docx");
public boolean getItalic() / public void setItalic(boolean value)
Example:
Shows how to write italicized text using a document builder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setSize(36.0); builder.getFont().setItalic(true); builder.writeln("Hello world!"); doc.save(getArtifactsDir() + "Font.Italic.docx");
public boolean getItalicBi() / public void setItalicBi(boolean value)
Example:
Shows how to define separate sets of font settings for right-to-left, and right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Define a set of font settings for left-to-right text. builder.getFont().setName("Courier New"); builder.getFont().setSize(16.0); builder.getFont().setItalic(false); builder.getFont().setBold(false); builder.getFont().setLocaleId(1033); // Define another set of font settings for right-to-left text. builder.getFont().setNameBi("Andalus"); builder.getFont().setSizeBi(48.0); // Specify that the right-to-left text in this run is bold and italic builder.getFont().setItalicBi(true); builder.getFont().setBoldBi(true); builder.getFont().setLocaleIdBi(1025); // We can use the Bidi flag to indicate whether the text we are about to add // with the document builder is right-to-left. When we add text with this flag set to true, // it will be formatted using the right-to-left set of font settings. builder.getFont().setBidi(true); builder.write("مرحبًا"); // Set the flag to false, and then add left-to-right text. // The document builder will format these using the left-to-right set of font settings. builder.getFont().setBidi(false); builder.write(" Hello world!"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
public double getKerning() / public void setKerning(double value)
Example:
Shows how to specify the font size at which kerning begins to take effect.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setName("Arial Black"); // Set the builder's font size, and minimum size at which kerning will take effect. // The font size falls below the kerning threshold, so the run bellow will not have kerning. builder.getFont().setSize(18.0); builder.getFont().setKerning(24.0); builder.writeln("TALLY. (Kerning not applied)"); // Set the kerning threshold so that the builder's current font size is above it. // Any text we add from this point will have kerning applied. The spaces between characters // will be adjusted, normally resulting in a slightly more aesthetically pleasing text run. builder.getFont().setKerning(12.0); builder.writeln("TALLY. (Kerning applied)"); doc.save(getArtifactsDir() + "Font.Kerning.docx");
public double getLineSpacing()
Example:
Shows how to get a font's line spacing, in points.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set different fonts for the DocumentBuilder, and verify their line spacing. builder.getFont().setName("Calibri"); Assert.assertEquals(13.7d, builder.getFont().getLineSpacing(), 1); builder.getFont().setName("Times New Roman"); Assert.assertEquals(13.7d, builder.getFont().getLineSpacing(), 1);
public int getLocaleId() / public void setLocaleId(int value)
Example:
Shows how to set the locale of the text that we are adding with a document builder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // If we set the font's locale to English and insert some Russian text, // the English locale spell checker will not recognize the text and detect it as a spelling error. builder.getFont().setLocaleId(1033); builder.writeln("Привет!"); // Set a matching locale for the text that we are about to add to apply the appropriate spell checker. builder.getFont().setLocaleId(1049); builder.writeln("Привет!"); doc.save(getArtifactsDir() + "Font.LocaleId.docx");
public int getLocaleIdBi() / public void setLocaleIdBi(int value)
Example:
Shows how to define separate sets of font settings for right-to-left, and right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Define a set of font settings for left-to-right text. builder.getFont().setName("Courier New"); builder.getFont().setSize(16.0); builder.getFont().setItalic(false); builder.getFont().setBold(false); builder.getFont().setLocaleId(1033); // Define another set of font settings for right-to-left text. builder.getFont().setNameBi("Andalus"); builder.getFont().setSizeBi(48.0); // Specify that the right-to-left text in this run is bold and italic builder.getFont().setItalicBi(true); builder.getFont().setBoldBi(true); builder.getFont().setLocaleIdBi(1025); // We can use the Bidi flag to indicate whether the text we are about to add // with the document builder is right-to-left. When we add text with this flag set to true, // it will be formatted using the right-to-left set of font settings. builder.getFont().setBidi(true); builder.write("مرحبًا"); // Set the flag to false, and then add left-to-right text. // The document builder will format these using the left-to-right set of font settings. builder.getFont().setBidi(false); builder.write(" Hello world!"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
public int getLocaleIdFarEast() / public void setLocaleIdFarEast(int value)
Example:
Shows how to insert and format text in a Far East language.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font settings that the document builder will apply to any text that it inserts. builder.getFont().setName("Courier New"); builder.getFont().setLocaleId(1033); // Name "FarEast" equivalents for our font and locale. // If the builder inserts Asian characters with this Font configuration, then each run that contains // these characters will display them using the "FarEast" font/locale instead of the default. // This could be useful when a western font does not have ideal representations for Asian characters. builder.getFont().setNameFarEast("SimSun"); builder.getFont().setLocaleIdFarEast(2052); // This text will be displayed in the default font/locale. builder.writeln("Hello world!"); // Since these are Asian characters, this run will apply our "FarEast" font/locale equivalents. builder.writeln("你好世界"); doc.save(getArtifactsDir() + "Font.FarEast.docx");
public java.lang.String getName() / public void setName(java.lang.String value)
When getting, returns
When setting, sets
Example:
Shows how to format a run of text using its font property.Document doc = new Document(); Run run = new Run(doc, "Hello world!"); Font font = run.getFont(); font.setName("Courier New"); font.setSize(36.0); font.setHighlightColor(Color.YELLOW); doc.getFirstSection().getBody().getFirstParagraph().appendChild(run); doc.save(getArtifactsDir() + "Font.CreateFormattedRun.docx");
Example:
Shows how to insert formatted text using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting, then add text. Font font = builder.getFont(); font.setSize(16.0); font.setBold(true); font.setColor(Color.BLUE); font.setName("Courier New"); font.setUnderline(Underline.DASH); builder.write("Hello world!");
public java.lang.String getNameAscii() / public void setNameAscii(java.lang.String value)
Example:
Shows how Microsoft Word can combine two different fonts in one run.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Suppose a run that we use the builder to insert while using this font configuration // contains characters within the ASCII characters' range. In that case, // it will display those characters using this font. builder.getFont().setNameAscii("Calibri"); // With no other font specified, the builder will also apply this font to all characters that it inserts. Assert.assertEquals("Calibri", builder.getFont().getName()); // Specify a font to use for all characters outside of the ASCII range. // Ideally, this font should have a glyph for each required non-ASCII character code. builder.getFont().setNameOther("Courier New"); // Insert a run with one word consisting of ASCII characters, and one word with all characters outside that range. // Each character will be displayed using either of the fonts, depending on. builder.writeln("Hello, Привет"); doc.save(getArtifactsDir() + "Font.NameAscii.docx");
public java.lang.String getNameBi() / public void setNameBi(java.lang.String value)
Example:
Shows how to define separate sets of font settings for right-to-left, and right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Define a set of font settings for left-to-right text. builder.getFont().setName("Courier New"); builder.getFont().setSize(16.0); builder.getFont().setItalic(false); builder.getFont().setBold(false); builder.getFont().setLocaleId(1033); // Define another set of font settings for right-to-left text. builder.getFont().setNameBi("Andalus"); builder.getFont().setSizeBi(48.0); // Specify that the right-to-left text in this run is bold and italic builder.getFont().setItalicBi(true); builder.getFont().setBoldBi(true); builder.getFont().setLocaleIdBi(1025); // We can use the Bidi flag to indicate whether the text we are about to add // with the document builder is right-to-left. When we add text with this flag set to true, // it will be formatted using the right-to-left set of font settings. builder.getFont().setBidi(true); builder.write("مرحبًا"); // Set the flag to false, and then add left-to-right text. // The document builder will format these using the left-to-right set of font settings. builder.getFont().setBidi(false); builder.write(" Hello world!"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
public java.lang.String getNameFarEast() / public void setNameFarEast(java.lang.String value)
Example:
Shows how to insert and format text in a Far East language.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font settings that the document builder will apply to any text that it inserts. builder.getFont().setName("Courier New"); builder.getFont().setLocaleId(1033); // Name "FarEast" equivalents for our font and locale. // If the builder inserts Asian characters with this Font configuration, then each run that contains // these characters will display them using the "FarEast" font/locale instead of the default. // This could be useful when a western font does not have ideal representations for Asian characters. builder.getFont().setNameFarEast("SimSun"); builder.getFont().setLocaleIdFarEast(2052); // This text will be displayed in the default font/locale. builder.writeln("Hello world!"); // Since these are Asian characters, this run will apply our "FarEast" font/locale equivalents. builder.writeln("你好世界"); doc.save(getArtifactsDir() + "Font.FarEast.docx");
public java.lang.String getNameOther() / public void setNameOther(java.lang.String value)
Example:
Shows how Microsoft Word can combine two different fonts in one run.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Suppose a run that we use the builder to insert while using this font configuration // contains characters within the ASCII characters' range. In that case, // it will display those characters using this font. builder.getFont().setNameAscii("Calibri"); // With no other font specified, the builder will also apply this font to all characters that it inserts. Assert.assertEquals("Calibri", builder.getFont().getName()); // Specify a font to use for all characters outside of the ASCII range. // Ideally, this font should have a glyph for each required non-ASCII character code. builder.getFont().setNameOther("Courier New"); // Insert a run with one word consisting of ASCII characters, and one word with all characters outside that range. // Each character will be displayed using either of the fonts, depending on. builder.writeln("Hello, Привет"); doc.save(getArtifactsDir() + "Font.NameAscii.docx");
public boolean getNoProofing() / public void setNoProofing(boolean value)
Example:
Shows how to prevent text from being spell checked by Microsoft Word.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Normally, Microsoft Word emphasizes spelling errors with a jagged red underline. // We can un-set the NoProofing flag to create a portion of text that // bypasses the spell checker while completely disabling it. builder.getFont().setNoProofing(true); builder.writeln("Proofing has been disabled, so these spelking errrs will not display red lines underneath."); doc.save(getArtifactsDir() + "Font.NoProofing.docx");
public boolean getOutline() / public void setOutline(boolean value)
Example:
Shows how to create a run of text formatted as outline.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the Outline flag to change the text's fill color to white and // leave a thin outline around each character in the original color of the text. builder.getFont().setOutline(true); builder.getFont().setColor(Color.BLUE); builder.getFont().setSize(36.0); builder.writeln("This text has an outline."); doc.save(getArtifactsDir() + "Font.Outline.docx");
public double getPosition() / public void setPosition(double value)
Example:
Shows how to format text to offset its position.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); // Raise this run of text 5 points above the baseline. Run run = new Run(doc, "Raised text. "); run.getFont().setPosition(5.0); para.appendChild(run); // Lower this run of text 10 points below the baseline. run = new Run(doc, "Lowered text. "); run.getFont().setPosition(-10); para.appendChild(run); // Add a run of normal text. run = new Run(doc, "Text in its default position. "); para.appendChild(run); // Add a run of text that appears as subscript. run = new Run(doc, "Subscript. "); run.getFont().setSubscript(true); para.appendChild(run); // Add a run of text that appears as superscript. run = new Run(doc, "Superscript."); run.getFont().setSuperscript(true); para.appendChild(run); doc.save(getArtifactsDir() + "Font.PositionSubscript.docx");
public int getScaling() / public void setScaling(int value)
Example:
Shows how to set horizontal scaling and spacing for characters.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Add run of text, and increase character width to 150%. builder.getFont().setScaling(150); builder.writeln("Wide characters"); // Add run of text, and add 1pt of extra horizontal spacing between each character. builder.getFont().setSpacing(1.0); builder.writeln("Expanded by 1pt"); // Add run of text, and bring characters closer together by 1pt. builder.getFont().setSpacing(-1); builder.writeln("Condensed by 1pt"); doc.save(getArtifactsDir() + "Font.ScalingSpacing.docx");
public Shading getShading()
Example:
Shows how to apply shading to text created by a document builder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setColor(Color.WHITE); // One way to make the text created using our white font color visible // is to apply a background shading effect. Shading shading = builder.getFont().getShading(); shading.setTexture(TextureIndex.TEXTURE_DIAGONAL_UP); shading.setBackgroundPatternColor(Color.RED); shading.setForegroundPatternColor(Color.BLUE); builder.writeln("White text on an orange background with a two-tone texture."); doc.save(getArtifactsDir() + "Font.Shading.docx");
public boolean getShadow() / public void setShadow(boolean value)
Example:
Shows how to create a run of text formatted with a shadow.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set the Shadow flag to apply an offset shadow effect, // making it look like the letters are floating above the page. builder.getFont().setShadow(true); builder.getFont().setSize(36.0); builder.writeln("This text has a shadow."); doc.save(getArtifactsDir() + "Font.Shadow.docx");
public double getSize() / public void setSize(double value)
Example:
Shows how to format a run of text using its font property.Document doc = new Document(); Run run = new Run(doc, "Hello world!"); Font font = run.getFont(); font.setName("Courier New"); font.setSize(36.0); font.setHighlightColor(Color.YELLOW); doc.getFirstSection().getBody().getFirstParagraph().appendChild(run); doc.save(getArtifactsDir() + "Font.CreateFormattedRun.docx");
Example:
Shows how to insert formatted text using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting, then add text. Font font = builder.getFont(); font.setSize(16.0); font.setBold(true); font.setColor(Color.BLUE); font.setName("Courier New"); font.setUnderline(Underline.DASH); builder.write("Hello world!");
public double getSizeBi() / public void setSizeBi(double value)
Example:
Shows how to define separate sets of font settings for right-to-left, and right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Define a set of font settings for left-to-right text. builder.getFont().setName("Courier New"); builder.getFont().setSize(16.0); builder.getFont().setItalic(false); builder.getFont().setBold(false); builder.getFont().setLocaleId(1033); // Define another set of font settings for right-to-left text. builder.getFont().setNameBi("Andalus"); builder.getFont().setSizeBi(48.0); // Specify that the right-to-left text in this run is bold and italic builder.getFont().setItalicBi(true); builder.getFont().setBoldBi(true); builder.getFont().setLocaleIdBi(1025); // We can use the Bidi flag to indicate whether the text we are about to add // with the document builder is right-to-left. When we add text with this flag set to true, // it will be formatted using the right-to-left set of font settings. builder.getFont().setBidi(true); builder.write("مرحبًا"); // Set the flag to false, and then add left-to-right text. // The document builder will format these using the left-to-right set of font settings. builder.getFont().setBidi(false); builder.write(" Hello world!"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
public boolean getSmallCaps() / public void setSmallCaps(boolean value)
Example:
Shows how to format a run to display its contents in capitals.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); // There are two ways of getting a run to display its lowercase text in uppercase without changing the contents. // 1 - Set the AllCaps flag to display all characters in regular capitals: Run run = new Run(doc, "all capitals"); run.getFont().setAllCaps(true); para.appendChild(run); para = (Paragraph)para.getParentNode().appendChild(new Paragraph(doc)); // 2 - Set the SmallCaps flag to display all characters in small capitals: // If a character is lower case, it will appear in its upper case form // but will have the same height as the lower case (the font's x-height). // Characters that were in upper case originally will look the same. run = new Run(doc, "Small Capitals"); run.getFont().setSmallCaps(true); para.appendChild(run); doc.save(getArtifactsDir() + "Font.Caps.docx");
public boolean getSnapToGrid() / public void setSnapToGrid(boolean value)
public double getSpacing() / public void setSpacing(double value)
Example:
Shows how to set horizontal scaling and spacing for characters.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Add run of text, and increase character width to 150%. builder.getFont().setScaling(150); builder.writeln("Wide characters"); // Add run of text, and add 1pt of extra horizontal spacing between each character. builder.getFont().setSpacing(1.0); builder.writeln("Expanded by 1pt"); // Add run of text, and bring characters closer together by 1pt. builder.getFont().setSpacing(-1); builder.writeln("Condensed by 1pt"); doc.save(getArtifactsDir() + "Font.ScalingSpacing.docx");
public boolean getStrikeThrough() / public void setStrikeThrough(boolean value)
Example:
Shows how to add a line strikethrough to text.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); Run run = new Run(doc, "Text with a single-line strikethrough."); run.getFont().setStrikeThrough(true); para.appendChild(run); para = (Paragraph)para.getParentNode().appendChild(new Paragraph(doc)); run = new Run(doc, "Text with a double-line strikethrough."); run.getFont().setDoubleStrikeThrough(true); para.appendChild(run); doc.save(getArtifactsDir() + "Font.StrikeThrough.docx");
public Style getStyle() / public void setStyle(Style value)
Example:
Applies a double underline to all runs in a document that are formatted with custom character styles.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a custom style, and apply it to text created using a document builder. Style style = doc.getStyles().add(StyleType.CHARACTER, "MyStyle"); style.getFont().setColor(Color.RED); style.getFont().setName("Courier New"); builder.getFont().setStyleName("MyStyle"); builder.write("This text is in a custom style."); // Iterate over every run, and add a double underline to every custom style. for (Run run : (Iterable<Run>) doc.getChildNodes(NodeType.RUN, true)) { Style charStyle = run.getFont().getStyle(); if (!charStyle.getBuiltIn()) run.getFont().setUnderline(Underline.DOUBLE); } doc.save(getArtifactsDir() + "Font.Style.docx");
public int getStyleIdentifier() / public void setStyleIdentifier(int value)
Example:
Shows how to change the style of existing text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Below are two ways of referencing styles. // 1 - Using the style name: builder.getFont().setStyleName("Emphasis"); builder.writeln("Text originally in \"Emphasis\" style"); // 2 - Using a built-in style identifier: builder.getFont().setStyleIdentifier(StyleIdentifier.INTENSE_EMPHASIS); builder.writeln("Text originally in \"Intense Emphasis\" style"); // Convert all uses of one style to another, // using the above methods to reference old and new styles. for (Run run : (Iterable<Run>)doc.getChildNodes(NodeType.RUN, true)) { if (run.getFont().getStyleName().equals("Emphasis")) run.getFont().setStyleName("Strong"); if (((run.getFont().getStyleIdentifier()) == (StyleIdentifier.INTENSE_EMPHASIS))) run.getFont().setStyleIdentifier(StyleIdentifier.STRONG); } doc.save(getArtifactsDir() + "Font.ChangeStyle.docx");
public java.lang.String getStyleName() / public void setStyleName(java.lang.String value)
Example:
Shows how to change the style of existing text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Below are two ways of referencing styles. // 1 - Using the style name: builder.getFont().setStyleName("Emphasis"); builder.writeln("Text originally in \"Emphasis\" style"); // 2 - Using a built-in style identifier: builder.getFont().setStyleIdentifier(StyleIdentifier.INTENSE_EMPHASIS); builder.writeln("Text originally in \"Intense Emphasis\" style"); // Convert all uses of one style to another, // using the above methods to reference old and new styles. for (Run run : (Iterable<Run>)doc.getChildNodes(NodeType.RUN, true)) { if (run.getFont().getStyleName().equals("Emphasis")) run.getFont().setStyleName("Strong"); if (((run.getFont().getStyleIdentifier()) == (StyleIdentifier.INTENSE_EMPHASIS))) run.getFont().setStyleIdentifier(StyleIdentifier.STRONG); } doc.save(getArtifactsDir() + "Font.ChangeStyle.docx");
public boolean getSubscript() / public void setSubscript(boolean value)
Example:
Shows how to format text to offset its position.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); // Raise this run of text 5 points above the baseline. Run run = new Run(doc, "Raised text. "); run.getFont().setPosition(5.0); para.appendChild(run); // Lower this run of text 10 points below the baseline. run = new Run(doc, "Lowered text. "); run.getFont().setPosition(-10); para.appendChild(run); // Add a run of normal text. run = new Run(doc, "Text in its default position. "); para.appendChild(run); // Add a run of text that appears as subscript. run = new Run(doc, "Subscript. "); run.getFont().setSubscript(true); para.appendChild(run); // Add a run of text that appears as superscript. run = new Run(doc, "Superscript."); run.getFont().setSuperscript(true); para.appendChild(run); doc.save(getArtifactsDir() + "Font.PositionSubscript.docx");
public boolean getSuperscript() / public void setSuperscript(boolean value)
Example:
Shows how to format text to offset its position.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); // Raise this run of text 5 points above the baseline. Run run = new Run(doc, "Raised text. "); run.getFont().setPosition(5.0); para.appendChild(run); // Lower this run of text 10 points below the baseline. run = new Run(doc, "Lowered text. "); run.getFont().setPosition(-10); para.appendChild(run); // Add a run of normal text. run = new Run(doc, "Text in its default position. "); para.appendChild(run); // Add a run of text that appears as subscript. run = new Run(doc, "Subscript. "); run.getFont().setSubscript(true); para.appendChild(run); // Add a run of text that appears as superscript. run = new Run(doc, "Superscript."); run.getFont().setSuperscript(true); para.appendChild(run); doc.save(getArtifactsDir() + "Font.PositionSubscript.docx");
public int getTextEffect() / public void setTextEffect(int value)
Example:
Shows how to apply a visual effect to a run.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setSize(36.0); builder.getFont().setTextEffect(TextEffect.SPARKLE_TEXT); builder.writeln("Text with a sparkle effect."); // Older versions of Microsoft Word only support font animation effects. doc.save(getArtifactsDir() + "Font.SparklingText.doc");
public int getUnderline() / public void setUnderline(int value)
Example:
Shows how to configure the style and color of a text underline.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setUnderline(Underline.DOTTED); builder.getFont().setUnderlineColor(Color.RED); builder.writeln("Underlined text."); doc.save(getArtifactsDir() + "Font.Underlines.docx");
Example:
Shows how to insert formatted text using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting, then add text. Font font = builder.getFont(); font.setSize(16.0); font.setBold(true); font.setColor(Color.BLUE); font.setName("Courier New"); font.setUnderline(Underline.DASH); builder.write("Hello world!");
Example:
Shows how to insert a hyperlink field.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("For more information, please visit the "); // Insert a hyperlink, and apply formatting to emphasize it. // The hyperlink will be a clickable piece of text which will take us to the location specified in the URL. builder.getFont().setColor(Color.BLUE); builder.getFont().setUnderline(Underline.SINGLE); builder.insertHyperlink("Aspose website", "http://www.aspose.com", false); builder.getFont().clearFormatting(); builder.writeln("."); // Ctrl + left clicking the link in the text in Microsoft Word will take us to the URL via a new web browser window. doc.save(getArtifactsDir() + "DocumentBuilder.InsertHyperlink.docx");
public java.awt.Color getUnderlineColor() / public void setUnderlineColor(java.awt.Color value)
Example:
Shows how to configure the style and color of a text underline.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setUnderline(Underline.DOTTED); builder.getFont().setUnderlineColor(Color.RED); builder.writeln("Underlined text."); doc.save(getArtifactsDir() + "Font.Underlines.docx");
public void clearFormatting()
Removes all font formatting specified explicitly on the object from which Font was obtained so the font formatting will be inherited from the appropriate parent.
Example:
Shows how to insert a hyperlink field.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("For more information, please visit the "); // Insert a hyperlink, and apply formatting to emphasize it. // The hyperlink will be a clickable piece of text which will take us to the location specified in the URL. builder.getFont().setColor(Color.BLUE); builder.getFont().setUnderline(Underline.SINGLE); builder.insertHyperlink("Aspose website", "http://www.aspose.com", false); builder.getFont().clearFormatting(); builder.writeln("."); // Ctrl + left clicking the link in the text in Microsoft Word will take us to the URL via a new web browser window. doc.save(getArtifactsDir() + "DocumentBuilder.InsertHyperlink.docx");
public boolean hasDmlEffect(int dmlEffectType)
dmlEffectType
- A Example:
Shows how to check if a run displays a DrawingML text effect.Document doc = new Document(getMyDir() + "DrawingML text effects.docx"); RunCollection runs = doc.getFirstSection().getBody().getFirstParagraph().getRuns(); Assert.assertTrue(runs.get(0).getFont().hasDmlEffect(TextDmlEffect.SHADOW)); Assert.assertTrue(runs.get(1).getFont().hasDmlEffect(TextDmlEffect.SHADOW)); Assert.assertTrue(runs.get(2).getFont().hasDmlEffect(TextDmlEffect.REFLECTION)); Assert.assertTrue(runs.get(3).getFont().hasDmlEffect(TextDmlEffect.EFFECT_3_D)); Assert.assertTrue(runs.get(4).getFont().hasDmlEffect(TextDmlEffect.FILL));