Aspose.Words

Specifying Formatting

Font Formatting

Current font formatting is represented by a Font object returned by the DocumentBuilder.Font property. The Font class contains a wide variety of the font properties possible in Microsoft Word.

Example DocumentBuilderSetFontFormatting

Shows how to set font formatting.

[Java]

 

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

 

// Set font formatting properties

Font font = builder.getFont();

font.setBold(true);

font.setColor(Color.BLUE);

font.setItalic(true);

font.setName("Arial");

font.setSize(24);

font.setSpacing(5);

font.setUnderline(Underline.DOUBLE);

 

// Output formatted text

builder.writeln("I'm a very nice formatted string.");

 

 

Paragraph Formatting

Current paragraph formatting is represented by a ParagraphFormat object that is returned by the DocumentBuilder.ParagraphFormat property. This object encapsulates various paragraph formatting properties available in Microsoft Word.

You can easily reset the paragraph formatting to default to Normal style, left aligned, no indentation, no spacing, no borders and no shading by calling ParagraphFormat.ClearFormatting.

Example DocumentBuilderSetParagraphFormatting

Shows how to set paragraph formatting.

[Java]

 

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

 

// Set paragraph formatting properties

ParagraphFormat paragraphFormat = builder.getParagraphFormat();

paragraphFormat.setAlignment(ParagraphAlignment.CENTER);

paragraphFormat.setLeftIndent(50);

paragraphFormat.setRightIndent(50);

paragraphFormat.setSpaceAfter(25);

 

// Output text

builder.writeln("I'm a very nice formatted paragraph. I'm intended to demonstrate how the left and right indents affect word wrapping.");

builder.writeln("I'm another nice formatted paragraph. I'm intended to demonstrate how the space after paragraph looks like.");

 

 

Cell Formatting

Cell formatting is used during building of a table. It is represented by a CellFormat object returned by the DocumentBuilder.CellFormat property. CellFormat encapsulates various table cell properties like width or vertical alignment.

Example DocumentBuilderSetCellFormatting

Shows how to create a table that contains a single formatted cell.

[Java]

 

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

 

builder.startTable();

builder.insertCell();

 

// Set the cell formatting

CellFormat cellFormat = builder.getCellFormat();

cellFormat.setWidth(250);

cellFormat.setLeftPadding(30);

cellFormat.setRightPadding(30);

cellFormat.setTopPadding(30);

cellFormat.setBottomPadding(30);

 

builder.writeln("I'm a wonderful formatted cell.");

 

builder.endRow();

builder.endTable();

 

 

Row Formatting

Current row formatting is determined by a RowFormat object that is returned by the DocumentBuilder.RowFormat property. The object encapsulates information about all table row formatting.

Example DocumentBuilderSetRowFormatting

Shows how to create a table that contains a single cell and apply row formatting.

[Java]

 

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

 

Table table = builder.startTable();

builder.insertCell();

 

// Set the row formatting

RowFormat rowFormat = builder.getRowFormat();

rowFormat.setHeight(100);

rowFormat.setHeightRule(HeightRule.EXACTLY);

// These formatting properties are set on the table and are applied to all rows in the table.

table.setLeftPadding(30);

table.setRightPadding(30);

table.setTopPadding(30);

table.setBottomPadding(30);

 

builder.writeln("I'm a wonderful formatted row.");

 

builder.endRow();

builder.endTable();

 

 

List Formatting

Aspose.Words allows the easy creation of lists by applying list formatting. DocumentBuilder provides the DocumentBuilder.ListFormat property that returns a ListFormat object. This object has several methods to start and end a list and to increase/decrease the indent.

There are two general types of lists in Microsoft Word: bulleted and numbered.

·          To start a bulleted list, call ListFormat.ApplyBulletDefault.

·          To start a numbered list, call ListFormat.ApplyNumberDefault.

The bullet or number and formatting are added to the current paragraph and all further paragraphs created using DocumentBuilder until ListFormat.RemoveNumbers is called to stop bulleted list formatting.

In Word documents, lists may consist of up to nine levels. List formatting for each level specifies what bullet or number is used, left indent, space between the bullet and text etc.

·          To increase the list level of the current paragraph by one level, call ListFormat.ListIndent.

·          To decrease the list level of the current paragraph by one level, call ListFormat.ListOutdent.

The methods change the list level and apply the formatting properties of the new level.

You can also use the ListFormat.ListLevelNumber property to get or set the list level for the paragraph. The list levels are numbered 0 to 8.

Example DocumentBuilderSetListFormatting

Shows how to build a multilevel list.

[Java]

 

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

 

builder.getListFormat().applyNumberDefault();

 

builder.writeln("Item 1");

builder.writeln("Item 2");

 

builder.getListFormat().listIndent();

 

builder.writeln("Item 2.1");

builder.writeln("Item 2.2");

 

builder.getListFormat().listIndent();

 

builder.writeln("Item 2.2.1");

builder.writeln("Item 2.2.2");

 

builder.getListFormat().listOutdent();

 

builder.writeln("Item 2.3");

 

builder.getListFormat().listOutdent();

 

builder.writeln("Item 3");

 

builder.getListFormat().removeNumbers();

 

 

Page Setup and Section Formatting

Page setup and section properties are encapsulated in the PageSetup object that is returned by the DocumentBuilder.PageSetup property. The object contains all the page setup attributes of a section (left margin, bottom margin, paper size, and so on) as properties.

Example DocumentBuilderSetSectionFormatting

Shows how to set such properties as page size and orientation for the current section.

[Java]

 

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

 

// Set page properties

builder.getPageSetup().setOrientation(Orientation.LANDSCAPE);

builder.getPageSetup().setLeftMargin(50);

builder.getPageSetup().setPaperSize(PaperSize.PAPER_10_X_14);

 

 

Applying a Style

Some formatting objects like Font or ParagraphFormat support styles. A single built-in or user defined style is represented by a Style object that contains the corresponding style properties like name, base style, font and paragraph formatting of the style, and so on.

Furthermore, a Style object provides the Style.StyleIdentifier property that returns a locale-independent style identifier represented by a Style.StyleIdentifier enumeration value. The point is that the names of built-in styles in Microsoft Word are localized for different languages. Using a style identifier, you can find the correct style regardless of the document language. The enumeration values correspond to the Microsoft Word built-in styles such as Normal, Heading 1, Heading 2 etc. All user-defined styles are assigned the StyleIdentifier.User value.

Example DocumentBuilderApplyParagraphStyle

Shows how to apply a paragraph style.

[Java]

 

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

 

// Set paragraph style

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.TITLE);

 

builder.write("Hello");

 

 

Borders and Shading

Borders are represented by the BorderCollection. This is a collection of Border objects that are accessed by index or by border type. Border type is represented by the BorderType enumeration. Some values of the enumeration are applicable to several or only one document element. For example, BorderType.Bottom is applicable to a paragraph or table cell while BorderType.DiagonalDown specifies the diagonal border in a table cell only.

Both the border collection and each separate border have similar attributes like color, line style, line width, distance from text, and optional shadow. They are represented by properties of the same name. You can achieve different border types by combining the property values. In addition, both BorderCollection and Border objects allow you to reset these values to default by calling the Border.ClearFormatting method. Note that when border properties are reset to default values, the border is invisible.

The Shading class contains shading attributes for document elements. You can set the desired shading texture and the colors that are applied to the background and foreground of the element.

The shading texture is set with a TextureIndex enumeration value that allows the application of various patterns to the Shading object. For example, to set a background color for a document element, use the TextureIndex.TextureSolid value and set the foreground shading color as appropriate.

Example DocumentBuilderApplyBordersAndShading

Shows how to apply borders and shading to a paragraph.

[Java]

 

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

 

// Set paragraph borders

BorderCollection borders = builder.getParagraphFormat().getBorders();

borders.setDistanceFromText(20);

borders.getByBorderType(BorderType.LEFT).setLineStyle(LineStyle.DOUBLE);

borders.getByBorderType(BorderType.RIGHT).setLineStyle(LineStyle.DOUBLE);

borders.getByBorderType(BorderType.TOP).setLineStyle(LineStyle.DOUBLE);

borders.getByBorderType(BorderType.BOTTOM).setLineStyle(LineStyle.DOUBLE);

 

// Set paragraph shading

Shading shading = builder.getParagraphFormat().getShading();

shading.setTexture(TextureIndex.TEXTURE_DIAGONAL_CROSS);

shading.setBackgroundPatternColor(new Color(240, 128, 128));  // Light Coral

shading.setForegroundPatternColor(new Color(255, 160, 122));  // Light Salmon

 

builder.write("I'm a formatted paragraph with double border and nice shading.");