java.lang.Objectcom.aspose.words.Font
public class Font
You do not create instances of the Example: Example: Example:
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();
// Create a new run of text
Run run = new Run(doc, "Hello");
// Specify character formatting for the run of text
Font f = run.getFont();
f.setName("Courier New");
f.setSize(36.0);
f.setHighlightColor(Color.YELLOW);
// Append the run of text to the end of the first paragraph
// in the body of the first section of the document
doc.getFirstSection().getBody().getFirstParagraph().appendChild(run);
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(boolean value) | |
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(boolean value) | |
Specifies whether the contents of this run shall have right-to-left characteristics. | ||
boolean | getBold() | |
void | setBold(boolean value) | |
True if the font is formatted as bold. | ||
boolean | getBoldBi() | |
void | setBoldBi(boolean value) | |
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.Color value) | |
Gets or sets the color of the font. | ||
boolean | getComplexScript() | |
void | setComplexScript(boolean value) | |
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(boolean value) | |
True if the font is formatted as double strikethrough text. | ||
boolean | getEmboss() | |
void | setEmboss(boolean value) | |
True if the font is formatted as embossed. | ||
int | getEmphasisMark() | |
void | setEmphasisMark(int value) | |
Gets or sets the emphasis mark applied to this formatting. The value of the property is EmphasisMark integer constant. | ||
boolean | getEngrave() | |
void | setEngrave(boolean value) | |
True if the font is formatted as engraved. | ||
boolean | getHidden() | |
void | setHidden(boolean value) | |
True if the font is formatted as hidden text. | ||
java.awt.Color | getHighlightColor() | |
void | setHighlightColor(java.awt.Color value) | |
Gets or sets the highlight (marker) color. | ||
boolean | getItalic() | |
void | setItalic(boolean value) | |
True if the font is formatted as italic. | ||
boolean | getItalicBi() | |
void | setItalicBi(boolean value) | |
True if the right-to-left text is formatted as italic. | ||
double | getKerning() | |
void | setKerning(double value) | |
Gets or sets the font size at which kerning starts. | ||
double | getLineSpacing() | |
Returns line spacing of this font (in points). | ||
int | getLocaleId() | |
void | setLocaleId(int value) | |
Gets or sets the locale identifier (language) of the formatted characters. | ||
int | getLocaleIdBi() | |
void | setLocaleIdBi(int value) | |
Gets or sets the locale identifier (language) of the formatted right-to-left characters. | ||
int | getLocaleIdFarEast() | |
void | setLocaleIdFarEast(int value) | |
Gets or sets the locale identifier (language) of the formatted Asian characters. | ||
java.lang.String | getName() | |
void | setName(java.lang.String value) | |
Gets or sets the name of the font. | ||
java.lang.String | getNameAscii() | |
void | setNameAscii(java.lang.String value) | |
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.String value) | |
Returns or sets the name of the font in a right-to-left language document. | ||
java.lang.String | getNameFarEast() | |
void | setNameFarEast(java.lang.String value) | |
Returns or sets an East Asian font name. | ||
java.lang.String | getNameOther() | |
void | setNameOther(java.lang.String value) | |
Returns or sets the font used for characters with character codes from 128 through 255. | ||
boolean | getNoProofing() | |
void | setNoProofing(boolean value) | |
True when the formatted characters are not to be spell checked. | ||
boolean | getOutline() | |
void | setOutline(boolean value) | |
True if the font is formatted as outline. | ||
double | getPosition() | |
void | setPosition(double value) | |
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(int value) | |
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(boolean value) | |
True if the font is formatted as shadowed. | ||
double | getSize() | |
void | setSize(double value) | |
Gets or sets the font size in points. | ||
double | getSizeBi() | |
void | setSizeBi(double value) | |
Gets or sets the font size in points used in a right-to-left document. | ||
boolean | getSmallCaps() | |
void | setSmallCaps(boolean value) | |
True if the font is formatted as small capital letters. | ||
boolean | getSnapToGrid() | |
void | setSnapToGrid(boolean value) | |
Specifies whether the current font should use the document grid characters per line settings when laying out. | ||
double | getSpacing() | |
void | setSpacing(double value) | |
Returns or sets the spacing (in points) between characters . | ||
boolean | getStrikeThrough() | |
void | setStrikeThrough(boolean value) | |
True if the font is formatted as strikethrough text. | ||
Style | getStyle() | |
void | setStyle(Style value) | |
Gets or sets the character style applied to this formatting. | ||
int | getStyleIdentifier() | |
void | setStyleIdentifier(int value) | |
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.String value) | |
Gets or sets the name of the character style applied to this formatting. | ||
boolean | getSubscript() | |
void | setSubscript(boolean value) | |
True if the font is formatted as subscript. | ||
boolean | getSuperscript() | |
void | setSuperscript(boolean value) | |
True if the font is formatted as superscript. | ||
int | getTextEffect() | |
void | setTextEffect(int value) | |
Gets or sets the font animation effect. The value of the property is TextEffect integer constant. | ||
int | getUnderline() | |
void | setUnderline(int value) | |
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.Color value) | |
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. |
Property Getters/Setters Detail |
---|
getAllCaps/setAllCaps | |
public boolean getAllCaps() / public void setAllCaps(boolean value) |
Example:
Shows how to use all capitals and small capitals character formatting properties.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); Run run = new Run(doc, "All capitals"); run.getFont().setAllCaps(true); para.appendChild(run); run = new Run(doc, "SMALL CAPITALS"); run.getFont().setSmallCaps(true); para.appendChild(run); doc.save(getArtifactsDir() + "Font.Caps.docx");
getAutoColor | |
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.
Example:
Shows how calculated color of the text (black or white) to be used for 'auto color'Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Remove direct color, so it can be calculated automatically with Font.AutoColor builder.getFont().setColor(new Color(0, 0, 0, 0)); // When we set black color for background, autocolor for font must be white builder.getFont().getShading().setBackgroundPatternColor(Color.BLACK); builder.writeln("The text color automatically chosen for this run is white."); // When we set white color for background, autocolor for font must be black builder.getFont().getShading().setBackgroundPatternColor(Color.WHITE); builder.writeln("The text color automatically chosen for this run is black."); doc.save(getArtifactsDir() + "Font.SetFontAutoColor.docx");
getBidi/setBidi | |
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 insert and format right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Signal to Microsoft Word that this run of text contains right-to-left text builder.getFont().setBidi(true); // Specify the font and font size to be used for the 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); // Specify the locale so Microsoft Word recognizes this text as Arabic - Saudi Arabia builder.getFont().setLocaleIdBi(1025); // Insert some Arabic text builder.writeln("مرحبًا"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
getBold/setBold | |
public boolean getBold() / public void setBold(boolean value) |
Example:
Inserts formatted text using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting before adding text Font font = builder.getFont(); font.setSize(16); font.setBold(true); font.setColor(Color.BLUE); font.setName("Courier New"); font.setUnderline(Underline.DASH); builder.write("Hello world!");
getBoldBi/setBoldBi | |
public boolean getBoldBi() / public void setBoldBi(boolean value) |
Example:
Shows how to insert and format right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Signal to Microsoft Word that this run of text contains right-to-left text builder.getFont().setBidi(true); // Specify the font and font size to be used for the 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); // Specify the locale so Microsoft Word recognizes this text as Arabic - Saudi Arabia builder.getFont().setLocaleIdBi(1025); // Insert some Arabic text builder.writeln("مرحبًا"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
getBorder | |
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");
getColor/setColor | |
public java.awt.Color getColor() / public void setColor(java.awt.Color value) |
Example:
Inserts formatted text using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting before adding text Font font = builder.getFont(); font.setSize(16); 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 into a document using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Please make sure to visit "); // Specify font formatting for the hyperlink builder.getFont().setColor(Color.BLUE); builder.getFont().setUnderline(Underline.SINGLE); // Insert the link builder.insertHyperlink("Aspose Website", "http://www.aspose.com", false); // Revert to default formatting builder.getFont().clearFormatting(); builder.write(" for more information."); // Holding Ctrl and left clicking on the field in Microsoft Word will take you to the link's address in a web browser doc.save(getArtifactsDir() + "DocumentBuilder.InsertHyperlink.docx");
getComplexScript/setComplexScript | |
public boolean getComplexScript() / public void setComplexScript(boolean value) |
Example:
Shows how to make a run that's 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");
getDoubleStrikeThrough/setDoubleStrikeThrough | |
public boolean getDoubleStrikeThrough() / public void setDoubleStrikeThrough(boolean value) |
Example:
Shows how to use strike-through character formatting properties.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); Run run = new Run(doc, "Double strike through text"); run.getFont().setDoubleStrikeThrough(true); para.appendChild(run); run = new Run(doc, "Single strike through text"); run.getFont().setStrikeThrough(true); para.appendChild(run); doc.save(getArtifactsDir() + "Font.StrikeThrough.docx");
getEmboss/setEmboss | |
public boolean getEmboss() / public void setEmboss(boolean value) |
Example:
Shows the difference between embossing and engraving text via font formatting.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setSize(36.0); builder.getFont().setColor(Color.WHITE); builder.getFont().setEngrave(true); builder.writeln("This text is engraved."); builder.getFont().setEngrave(false); builder.getFont().setEmboss(true); builder.writeln("This text is embossed."); doc.save(getArtifactsDir() + "Font.EngraveEmboss.docx");
getEmphasisMark/setEmphasisMark | |
public int getEmphasisMark() / public void setEmphasisMark(int value) |
getEngrave/setEngrave | |
public boolean getEngrave() / public void setEngrave(boolean value) |
Example:
Shows the difference between embossing and engraving text via font formatting.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setSize(36.0); builder.getFont().setColor(Color.WHITE); builder.getFont().setEngrave(true); builder.writeln("This text is engraved."); builder.getFont().setEngrave(false); builder.getFont().setEmboss(true); builder.writeln("This text is embossed."); doc.save(getArtifactsDir() + "Font.EngraveEmboss.docx");
getHidden/setHidden | |
public boolean getHidden() / public void setHidden(boolean value) |
Example:
Shows how to create a hidden run of text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setSize(36.0); builder.getFont().setHidden(true); // With the Hidden flag set to true, we can add text that will be present but invisible in the document // It is not recommended to use this as a way of hiding sensitive information as the text is still easily reachable builder.writeln("This text won't be visible in the document."); doc.save(getArtifactsDir() + "Font.Hidden.docx");
Example:
Implements the Visitor Pattern to remove all content formatted as hidden from the document.public void removeHiddenContentFromDocument() throws Exception { // Open the document we want to remove hidden content from Document doc = new Document(getMyDir() + "Hidden content.docx"); // Create an object that inherits from the DocumentVisitor class RemoveHiddenContentVisitor hiddenContentRemover = new RemoveHiddenContentVisitor(); // This is the well known Visitor pattern. Get the model to accept a visitor // The model will iterate through itself by calling the corresponding methods // on the visitor object (this is called visiting) // We can run it over the entire the document like so doc.accept(hiddenContentRemover); // Or we can run it on only a specific node Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 4, true); para.accept(hiddenContentRemover); // Or over a different type of node like below Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); table.accept(hiddenContentRemover); doc.save(getArtifactsDir() + "Font.RemoveHiddenContentFromDocument.docx"); } /** * This class when executed will remove all hidden content from the Document. Implemented as a Visitor. */ private class RemoveHiddenContentVisitor extends DocumentVisitor { /** * Called when a FieldStart node is encountered in the document. */ public int visitFieldStart(final FieldStart fieldStart) throws Exception { // If this node is hidden, then remove it if (fieldStart.getFont().getHidden()) { fieldStart.remove(); } return VisitorAction.CONTINUE; } /** * Called when a FieldEnd node is encountered in the document. */ public int visitFieldEnd(final FieldEnd fieldEnd) throws Exception { if (fieldEnd.getFont().getHidden()) { fieldEnd.remove(); } return VisitorAction.CONTINUE; } /** * Called when a FieldSeparator node is encountered in the document. */ public int visitFieldSeparator(final FieldSeparator fieldSeparator) throws Exception { if (fieldSeparator.getFont().getHidden()) { fieldSeparator.remove(); } return VisitorAction.CONTINUE; } /** * Called when a Run node is encountered in the document. */ public int visitRun(final Run run) throws Exception { if (run.getFont().getHidden()) { run.remove(); } return VisitorAction.CONTINUE; } /** * Called when a Paragraph node is encountered in the document. */ public int visitParagraphStart(final Paragraph paragraph) throws Exception { if (paragraph.getParagraphBreakFont().getHidden()) { paragraph.remove(); } return VisitorAction.CONTINUE; } /** * Called when a FormField is encountered in the document. */ public int visitFormField(final FormField field) throws Exception { if (field.getFont().getHidden()) { field.remove(); } return VisitorAction.CONTINUE; } /** * Called when a GroupShape is encountered in the document. */ public int visitGroupShapeStart(final GroupShape groupShape) throws Exception { if (groupShape.getFont().getHidden()) { groupShape.remove(); } return VisitorAction.CONTINUE; } /** * Called when a Shape is encountered in the document. */ public int visitShapeStart(final Shape shape) throws Exception { if (shape.getFont().getHidden()) { shape.remove(); } return VisitorAction.CONTINUE; } /** * Called when a Comment is encountered in the document. */ public int visitCommentStart(final Comment comment) throws Exception { if (comment.getFont().getHidden()) { comment.remove(); } return VisitorAction.CONTINUE; } /** * Called when a Footnote is encountered in the document. */ public int visitFootnoteStart(final Footnote footnote) throws Exception { if (footnote.getFont().getHidden()) { footnote.remove(); } return VisitorAction.CONTINUE; } /// <summary> /// Called when a SpecialCharacter is encountered in the document. /// </summary> public /*override*/ /*VisitorAction*/int visitSpecialChar(SpecialChar specialChar) { if (specialChar.getFont().getHidden()) specialChar.remove(); return VisitorAction.CONTINUE; } /** * Called when visiting of a Table node is ended in the document. */ public int visitTableEnd(final Table table) { // At the moment there is no way to tell if a particular Table/Row/Cell is hidden. // Instead, if the content of a table is hidden, then all inline child nodes of the table should be // hidden and thus removed by previous visits as well. This will result in the container being empty // so if this is the case we know to remove the table node. // // Note that a table which is not hidden but simply has no content will not be affected by this algorithm, // as technically they are not completely empty (for example a properly formed Cell will have at least // an empty paragraph in it) if (!table.hasChildNodes()) { table.remove(); } return VisitorAction.CONTINUE; } /** * Called when visiting of a Cell node is ended in the document. */ public int visitCellEnd(final Cell cell) { if (!cell.hasChildNodes() && cell.getParentNode() != null) { cell.remove(); } return VisitorAction.CONTINUE; } /** * Called when visiting of a Row node is ended in the document. */ public int visitRowEnd(final Row row) { if (!row.hasChildNodes() && row.getParentNode() != null) { row.remove(); } return VisitorAction.CONTINUE; }
getHighlightColor/setHighlightColor | |
public java.awt.Color getHighlightColor() / public void setHighlightColor(java.awt.Color value) |
Example:
Shows how to add a formatted run of text to a document using the object model.Document doc = new Document(); // Create a new run of text Run run = new Run(doc, "Hello"); // Specify character formatting for the run of text Font f = run.getFont(); f.setName("Courier New"); f.setSize(36.0); f.setHighlightColor(Color.YELLOW); // Append the run of text to the end of the first paragraph // in the body of the first section of the document doc.getFirstSection().getBody().getFirstParagraph().appendChild(run);
getItalic/setItalic | |
public boolean getItalic() / public void setItalic(boolean value) |
Example:
Shows how to italicize a run of text.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");
getItalicBi/setItalicBi | |
public boolean getItalicBi() / public void setItalicBi(boolean value) |
Example:
Shows how to insert and format right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Signal to Microsoft Word that this run of text contains right-to-left text builder.getFont().setBidi(true); // Specify the font and font size to be used for the 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); // Specify the locale so Microsoft Word recognizes this text as Arabic - Saudi Arabia builder.getFont().setLocaleIdBi(1025); // Insert some Arabic text builder.writeln("مرحبًا"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
getKerning/setKerning | |
public double getKerning() / public void setKerning(double value) |
Example:
Shows how to specify the font size at which kerning starts.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setName("Arial Black"); // Set the font's kerning size threshold and font size builder.getFont().setKerning(24.0); builder.getFont().setSize(18.0); // The font size falls below the kerning threshold so kerning will not be applied builder.writeln("TALLY. (Kerning not applied)"); // If we add runs of text with a document builder's writing methods, // the Font attributes of any new runs will inherit the values from the Font attributes of the previous runs // The font size is still 18, and we will change the kerning threshold to a value below that builder.getFont().setKerning(12.0); // Kerning has now been applied to this run builder.writeln("TALLY. (Kerning applied)"); doc.save(getArtifactsDir() + "Font.Kerning.docx");
getLineSpacing | |
public double getLineSpacing() |
Example:
Shows how to get line spacing of current font (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);
getLocaleId/setLocaleId | |
public int getLocaleId() / public void setLocaleId(int value) |
Example:
Shows how to specify the language of a text run so Microsoft Word can use a proper spell checker.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify the locale so Microsoft Word recognizes this text as Russian builder.getFont().setLocaleId(1049); builder.writeln("Привет!"); doc.save(getArtifactsDir() + "Font.LocaleId.docx");
getLocaleIdBi/setLocaleIdBi | |
public int getLocaleIdBi() / public void setLocaleIdBi(int value) |
Example:
Shows how to insert and format right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Signal to Microsoft Word that this run of text contains right-to-left text builder.getFont().setBidi(true); // Specify the font and font size to be used for the 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); // Specify the locale so Microsoft Word recognizes this text as Arabic - Saudi Arabia builder.getFont().setLocaleIdBi(1025); // Insert some Arabic text builder.writeln("مرحبًا"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
getLocaleIdFarEast/setLocaleIdFarEast | |
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 the font name builder.getFont().setNameFarEast("SimSun"); // Specify the locale so Microsoft Word recognizes this text as Chinese builder.getFont().setLocaleIdFarEast(2052); // Insert some Chinese text builder.writeln("你好世界"); doc.save(getArtifactsDir() + "Font.FarEast.docx");
getName/setName | |
public java.lang.String getName() / public void setName(java.lang.String value) |
When getting, returns
When setting, sets
Example:
Inserts formatted text using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting before adding text Font font = builder.getFont(); font.setSize(16); font.setBold(true); font.setColor(Color.BLUE); font.setName("Courier New"); font.setUnderline(Underline.DASH); builder.write("Hello world!");
Example:
Shows how to add a formatted run of text to a document using the object model.Document doc = new Document(); // Create a new run of text Run run = new Run(doc, "Hello"); // Specify character formatting for the run of text Font f = run.getFont(); f.setName("Courier New"); f.setSize(36.0); f.setHighlightColor(Color.YELLOW); // Append the run of text to the end of the first paragraph // in the body of the first section of the document doc.getFirstSection().getBody().getFirstParagraph().appendChild(run);
getNameAscii/setNameAscii | |
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); // Specify a font to use for all characters that fall within the ASCII character set builder.getFont().setNameAscii("Calibri"); // Specify a font to use for all other characters // This font should have a glyph for every other required character code builder.getFont().setNameOther("Courier New"); // The builder's font is the ASCII font Assert.assertEquals("Calibri", builder.getFont().getName()); // Insert a run with one word consisting of ASCII characters, and one word with all characters outside that range // This will create a run with two fonts builder.writeln("Hello, Привет"); doc.save(getArtifactsDir() + "Font.Names.docx");
getNameBi/setNameBi | |
public java.lang.String getNameBi() / public void setNameBi(java.lang.String value) |
Example:
Shows how to insert and format right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Signal to Microsoft Word that this run of text contains right-to-left text builder.getFont().setBidi(true); // Specify the font and font size to be used for the 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); // Specify the locale so Microsoft Word recognizes this text as Arabic - Saudi Arabia builder.getFont().setLocaleIdBi(1025); // Insert some Arabic text builder.writeln("مرحبًا"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
getNameFarEast/setNameFarEast | |
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 the font name builder.getFont().setNameFarEast("SimSun"); // Specify the locale so Microsoft Word recognizes this text as Chinese builder.getFont().setLocaleIdFarEast(2052); // Insert some Chinese text builder.writeln("你好世界"); doc.save(getArtifactsDir() + "Font.FarEast.docx");
getNameOther/setNameOther | |
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); // Specify a font to use for all characters that fall within the ASCII character set builder.getFont().setNameAscii("Calibri"); // Specify a font to use for all other characters // This font should have a glyph for every other required character code builder.getFont().setNameOther("Courier New"); // The builder's font is the ASCII font Assert.assertEquals("Calibri", builder.getFont().getName()); // Insert a run with one word consisting of ASCII characters, and one word with all characters outside that range // This will create a run with two fonts builder.writeln("Hello, Привет"); doc.save(getArtifactsDir() + "Font.Names.docx");
getNoProofing/setNoProofing | |
public boolean getNoProofing() / public void setNoProofing(boolean value) |
Example:
Shows how to specify that the run of text is not to be spell checked by Microsoft Word.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.getFont().setNoProofing(true); builder.writeln("Proofing has been disabled for this run, so these spelking errrs will not display red lines underneath."); doc.save(getArtifactsDir() + "Font.NoProofing.docx");
getOutline/setOutline | |
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); builder.getFont().setSize(36.0); builder.getFont().setOutline(true); builder.writeln("This text has an outline."); doc.save(getArtifactsDir() + "Font.Outline.docx");
getPosition/setPosition | |
public double getPosition() / public void setPosition(double value) |
Example:
Shows how to use subscript, superscript, complex script, text effects, and baseline text position properties.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); // Add a run of text that is raised 5 points above the baseline Run run = new Run(doc, "Raised text"); run.getFont().setPosition(5.0); para.appendChild(run); // Add a run of normal text run = new Run(doc, "Normal text"); 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");
getScaling/setScaling | |
public int getScaling() / public void setScaling(int value) |
Example:
Shows how to use character scaling and spacing properties.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Add a run of text with characters 150% width of normal characters builder.getFont().setScaling(150); builder.writeln("Wide characters"); // Add a run of text with extra 1pt space between characters builder.getFont().setSpacing(1.0); builder.writeln("Expanded by 1pt"); // Add a run of text with space between characters reduced by 1pt builder.getFont().setSpacing(-1); builder.writeln("Condensed by 1pt"); doc.save(getArtifactsDir() + "Font.ScalingSpacing.docx");
getShading | |
public Shading getShading() |
Example:
Shows how to apply shading for a run of text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Shading shd = builder.getFont().getShading(); shd.setTexture(TextureIndex.TEXTURE_DIAGONAL_UP); shd.setBackgroundPatternColor(Color.RED); shd.setForegroundPatternColor(Color.BLUE); builder.getFont().setColor(Color.WHITE); builder.writeln("White text on an orange background with texture."); doc.save(getArtifactsDir() + "Font.Shading.docx");
getShadow/setShadow | |
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); builder.getFont().setSize(36.0); builder.getFont().setShadow(true); builder.writeln("This text has a shadow."); doc.save(getArtifactsDir() + "Font.Shadow.docx");
getSize/setSize | |
public double getSize() / public void setSize(double value) |
Example:
Inserts formatted text using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting before adding text Font font = builder.getFont(); font.setSize(16); font.setBold(true); font.setColor(Color.BLUE); font.setName("Courier New"); font.setUnderline(Underline.DASH); builder.write("Hello world!");
Example:
Shows how to add a formatted run of text to a document using the object model.Document doc = new Document(); // Create a new run of text Run run = new Run(doc, "Hello"); // Specify character formatting for the run of text Font f = run.getFont(); f.setName("Courier New"); f.setSize(36.0); f.setHighlightColor(Color.YELLOW); // Append the run of text to the end of the first paragraph // in the body of the first section of the document doc.getFirstSection().getBody().getFirstParagraph().appendChild(run);
getSizeBi/setSizeBi | |
public double getSizeBi() / public void setSizeBi(double value) |
Example:
Shows how to insert and format right-to-left text.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Signal to Microsoft Word that this run of text contains right-to-left text builder.getFont().setBidi(true); // Specify the font and font size to be used for the 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); // Specify the locale so Microsoft Word recognizes this text as Arabic - Saudi Arabia builder.getFont().setLocaleIdBi(1025); // Insert some Arabic text builder.writeln("مرحبًا"); doc.save(getArtifactsDir() + "Font.Bidi.docx");
getSmallCaps/setSmallCaps | |
public boolean getSmallCaps() / public void setSmallCaps(boolean value) |
Example:
Shows how to use all capitals and small capitals character formatting properties.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); Run run = new Run(doc, "All capitals"); run.getFont().setAllCaps(true); para.appendChild(run); run = new Run(doc, "SMALL CAPITALS"); run.getFont().setSmallCaps(true); para.appendChild(run); doc.save(getArtifactsDir() + "Font.Caps.docx");
getSnapToGrid/setSnapToGrid | |
public boolean getSnapToGrid() / public void setSnapToGrid(boolean value) |
getSpacing/setSpacing | |
public double getSpacing() / public void setSpacing(double value) |
Example:
Shows how to use character scaling and spacing properties.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Add a run of text with characters 150% width of normal characters builder.getFont().setScaling(150); builder.writeln("Wide characters"); // Add a run of text with extra 1pt space between characters builder.getFont().setSpacing(1.0); builder.writeln("Expanded by 1pt"); // Add a run of text with space between characters reduced by 1pt builder.getFont().setSpacing(-1); builder.writeln("Condensed by 1pt"); doc.save(getArtifactsDir() + "Font.ScalingSpacing.docx");
getStrikeThrough/setStrikeThrough | |
public boolean getStrikeThrough() / public void setStrikeThrough(boolean value) |
Example:
Shows how to use strike-through character formatting properties.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); Run run = new Run(doc, "Double strike through text"); run.getFont().setDoubleStrikeThrough(true); para.appendChild(run); run = new Run(doc, "Single strike through text"); run.getFont().setStrikeThrough(true); para.appendChild(run); doc.save(getArtifactsDir() + "Font.StrikeThrough.docx");
getStyle/setStyle | |
public Style getStyle() / public void setStyle(Style value) |
Example:
Applies 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 Style style = doc.getStyles().add(StyleType.CHARACTER, "MyStyle"); style.getFont().setColor(Color.RED); style.getFont().setName("Courier New"); // Set the style of the current paragraph to our custom style // This will apply to only the text after the style separator builder.getFont().setStyleName("MyStyle"); builder.write("This text is in a custom style."); // Iterate through every run node and apply underlines to the run if its style is not a built in style, // like the one we added for (Run run : doc.getFirstSection().getBody().getParagraphs().get(0).getRuns()) { Style charStyle = run.getFont().getStyle(); // If the style of the run is not a built-in character style, apply double underline if (!charStyle.getBuiltIn()) { run.getFont().setUnderline(Underline.DOUBLE); } doc.save(getArtifactsDir() + "Font.Style.docx");
getStyleIdentifier/setStyleIdentifier | |
public int getStyleIdentifier() / public void setStyleIdentifier(int value) |
Example:
Shows how to use style name or identifier to find text formatted with a specific character style and apply different character style.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert text with two styles that will be replaced by another style builder.getFont().setStyleIdentifier(StyleIdentifier.EMPHASIS); builder.writeln("Text originally in \"Emphasis\" style"); builder.getFont().setStyleIdentifier(StyleIdentifier.INTENSE_EMPHASIS); builder.writeln("Text originally in \"Intense Emphasis\" style"); NodeCollection runs = doc.getChildNodes(NodeType.RUN, true); // Loop through every run node for (Run run : (Iterable<Run>) runs) { // If the run's text is of the "Emphasis" style, referenced by name, change the style to "Strong" if (run.getFont().getStyleName().equals("Emphasis")) run.getFont().setStyleName("Strong"); // If the run's text style is "Intense Emphasis", change it to "Strong" also, but this time reference using a StyleIdentifier if (((run.getFont().getStyleIdentifier()) == (StyleIdentifier.INTENSE_EMPHASIS))) run.getFont().setStyleIdentifier(StyleIdentifier.STRONG); } doc.save(getArtifactsDir() + "Font.ChangeStyle.docx");
getStyleName/setStyleName | |
public java.lang.String getStyleName() / public void setStyleName(java.lang.String value) |
Example:
Shows how to use style name or identifier to find text formatted with a specific character style and apply different character style.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert text with two styles that will be replaced by another style builder.getFont().setStyleIdentifier(StyleIdentifier.EMPHASIS); builder.writeln("Text originally in \"Emphasis\" style"); builder.getFont().setStyleIdentifier(StyleIdentifier.INTENSE_EMPHASIS); builder.writeln("Text originally in \"Intense Emphasis\" style"); NodeCollection runs = doc.getChildNodes(NodeType.RUN, true); // Loop through every run node for (Run run : (Iterable<Run>) runs) { // If the run's text is of the "Emphasis" style, referenced by name, change the style to "Strong" if (run.getFont().getStyleName().equals("Emphasis")) run.getFont().setStyleName("Strong"); // If the run's text style is "Intense Emphasis", change it to "Strong" also, but this time reference using a StyleIdentifier if (((run.getFont().getStyleIdentifier()) == (StyleIdentifier.INTENSE_EMPHASIS))) run.getFont().setStyleIdentifier(StyleIdentifier.STRONG); } doc.save(getArtifactsDir() + "Font.ChangeStyle.docx");
getSubscript/setSubscript | |
public boolean getSubscript() / public void setSubscript(boolean value) |
Example:
Shows how to use subscript, superscript, complex script, text effects, and baseline text position properties.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); // Add a run of text that is raised 5 points above the baseline Run run = new Run(doc, "Raised text"); run.getFont().setPosition(5.0); para.appendChild(run); // Add a run of normal text run = new Run(doc, "Normal text"); 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");
getSuperscript/setSuperscript | |
public boolean getSuperscript() / public void setSuperscript(boolean value) |
Example:
Shows how to use subscript, superscript, complex script, text effects, and baseline text position properties.Document doc = new Document(); Paragraph para = (Paragraph) doc.getChild(NodeType.PARAGRAPH, 0, true); // Add a run of text that is raised 5 points above the baseline Run run = new Run(doc, "Raised text"); run.getFont().setPosition(5.0); para.appendChild(run); // Add a run of normal text run = new Run(doc, "Normal text"); 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");
getTextEffect/setTextEffect | |
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."); // Font animation effects are only visible in older versions of Microsoft Word doc.save(getArtifactsDir() + "Font.SparklingText.doc");
getUnderline/setUnderline | |
public int getUnderline() / public void setUnderline(int value) |
Example:
Shows how use the underline character formatting properties.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set an underline color and style builder.getFont().setUnderline(Underline.DOTTED); builder.getFont().setUnderlineColor(Color.RED); builder.writeln("Underlined text."); doc.save(getArtifactsDir() + "Font.Underlines.docx");
Example:
Inserts formatted text using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Specify font formatting before adding text Font font = builder.getFont(); font.setSize(16); 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 into a document using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Please make sure to visit "); // Specify font formatting for the hyperlink builder.getFont().setColor(Color.BLUE); builder.getFont().setUnderline(Underline.SINGLE); // Insert the link builder.insertHyperlink("Aspose Website", "http://www.aspose.com", false); // Revert to default formatting builder.getFont().clearFormatting(); builder.write(" for more information."); // Holding Ctrl and left clicking on the field in Microsoft Word will take you to the link's address in a web browser doc.save(getArtifactsDir() + "DocumentBuilder.InsertHyperlink.docx");
getUnderlineColor/setUnderlineColor | |
public java.awt.Color getUnderlineColor() / public void setUnderlineColor(java.awt.Color value) |
Example:
Shows how use the underline character formatting properties.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Set an underline color and style builder.getFont().setUnderline(Underline.DOTTED); builder.getFont().setUnderlineColor(Color.RED); builder.writeln("Underlined text."); doc.save(getArtifactsDir() + "Font.Underlines.docx");
Method Detail |
---|
clearFormatting | |
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 into a document using DocumentBuilder.Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.write("Please make sure to visit "); // Specify font formatting for the hyperlink builder.getFont().setColor(Color.BLUE); builder.getFont().setUnderline(Underline.SINGLE); // Insert the link builder.insertHyperlink("Aspose Website", "http://www.aspose.com", false); // Revert to default formatting builder.getFont().clearFormatting(); builder.write(" for more information."); // Holding Ctrl and left clicking on the field in Microsoft Word will take you to the link's address in a web browser doc.save(getArtifactsDir() + "DocumentBuilder.InsertHyperlink.docx");
hasDmlEffect | |
public boolean hasDmlEffect(int dmlEffectType) |
dmlEffectType
- A Example:
Shows how to checks if particular Dml text effect is applied.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));