General

Components

Community

Development

TDF

Documents > Cookbook >Style Handling



Overview
Style handling methods provide convenient methods to set font and borders.

Font handling
The most simple method to define font settings is to create a font object, and set it to a cell object. The below code snippet defines a font object to describe "Arial" italic font with size "12pt" and black color, and then set it to a cell. The font will work for western characters by default.

		SpreadsheetDocument document = SpreadsheetDocument.newSpreadsheetDocument();
Table table = document.getTableByName("Sheet1");
Font font = new Font("Arial", StyleTypeDefinitions.FontStyle.ITALIC, 12, Color.BLACK);
Cell cell = table.getCellByPosition("A1");
cell.setFont(font);

The most simple method to get font settings of western characters is:

		Font theFont = cell.getFont();
double size = theFont.getSize();
String fontName = theFont.getFamilyName();
StyleTypeDefinitions.FontStyle fontStyle = theFont.getFontStyle();
Color fontColor = theFont.getColor();


Advanced font handling
CellStyleHandler can help you to achieve advanced functions. In Open Document Format, there can be different font settings for different script types. For example, a font setting for English characters and another font setting for Chinese characters. If you want to define the font setting for other script types, you can reference to below codes. The below code snippet defines a font for Chinese characters.

		cell.getStyleHandler().setFont(font, new Locale(Locale.CHINESE.getLanguage(), Locale.CHINA.getCountry()));

The below code snippet shows how to get the font setting for other kinds of scripts.

		CellStyleHandler styleHandler = cell.getStyleHandler();
Font westernFont = styleHandler.getFont(Document.ScriptType.WESTERN);
Font chineseFont = styleHandler.getFont(Document.ScriptType.CJK);
Font complexFont = styleHandler.getFont(Document.ScriptType.CTL);


Border handling
The most simple way to set border is to create a border object and then set it to a cell object. Below code snippet illustrates how to set a cell object with four borders.

		cell = table.getCellByPosition("A1");
cell.setStringValue("four border");
Border border = new Border(Color.RED, 1, StyleTypeDefinitions.SupportedLinearMeasure.PT);
cell.setBorders(CellBordersType.ALL_FOUR, border);

Below code snippet illustrates how to set a cell object with left and right borders, top and bottom borders and diagonal lines.

		cell.setBorders(CellBordersType.LEFT_RIGHT, border);
cell.setBorders(CellBordersType.TOP_BOTTOM, border);
cell.setBorders(CellBordersType.DIAGONAL_LINES, border);

Below code snippet illustrates how to set a cell object with left border, top border and diagonal from bottom left to top right.

		cell.setBorders(CellBordersType.LEFT, border);
cell.setBorders(CellBordersType.TOP, border);
cell.setBorders(CellBordersType.DIAGONALBLTR, border);

Below code snippet illustrates how to get a border definition.

		Border thisBorder = cell.getBorder(CellBordersType.LEFT);
thisBorder = cell.getBorder(CellBordersType.TOP);
thisBorder = cell.getBorder(CellBordersType.DIAGONALBLTR);


Impressum (Legal Info) | Privacy Policy (Datenschutzerklärung) | Statutes (non-binding English translation) - Satzung (binding German version) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Apache License, v2.0. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License v2.0. “LibreOffice” and “The Document Foundation” are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy. LibreOffice was based on OpenOffice.org.