Paragraph stylesheets:
Paragraph stylesheets provide a method for grouping a number of font settings under
one name. Additionally there are some styles that have special meaning, such as the
default style and heading styles (see Table of contents for an example).
The basic stylesheets are defined in RtfStylesheet There are four stylesheets defined by default:
Using the paragraph stylesheets is very simple. Just set them as the font for the Paragraph you wish to apply the style to.
Go to top of the pageThe basic stylesheets are defined in RtfStylesheet There are four stylesheets defined by default:
- STYLE_NORMAL: The default text style
- STYLE_HEADING_1, STYLE_HEADING_2, STYLE_HEADING_3: Styles that define the top three heading levels.
Using the paragraph stylesheets is very simple. Just set them as the font for the Paragraph you wish to apply the style to.
// Simply set the stylesheet you wish to use as the Font
// of the Paragraph
document.add(new Paragraph("This is a heading",
RtfParagraphStyle.STYLE_HEADING_1));
document.add(new Paragraph("Just some text that is formatted " +
"in the default style.", RtfParagraphStyle.STYLE_NORMAL));
Example: java
com.lowagie.examples.rtf.features.styles.BasicStylesheets
Generates a document demonstrating the use of paragraph stylesheets: see BasicStylesheets.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Generates a document demonstrating the use of paragraph stylesheets: see BasicStylesheets.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Changing stylesheet properties:
While using the default stylesheets as they are can sometimes be sufficient, often
it will be necessary to adapt them to the requirements. To do this simply use the
methods provided by the RtfParagraphStyle
on the default styles provided. Consult the javadoc for which properties can be changed.
One thing that is important is that all changes must be done before document.open() is called. Otherwise while the changes will be applied to the document content, the style definition will not match any more.
Go to top of the pageOne thing that is important is that all changes must be done before document.open() is called. Otherwise while the changes will be applied to the document content, the style definition will not match any more.
// Set the default style font name. This is inherited
// by all other styles.
RtfParagraphStyle.STYLE_NORMAL.setFontName("Times New Roman");
// Set the colour of the level 1 heading to blue.
RtfParagraphStyle.STYLE_HEADING_1.setColor(Color.BLUE);
// Set the font name of the heading back to Arial again.
RtfParagraphStyle.STYLE_HEADING_2.setFontName("Arial");
// Change the font size
RtfParagraphStyle.STYLE_HEADING_2.setSize(12);
Here it is important to note that the three heading styles all inherit from the default style.
That means that any property that is not explicitly set in the style will take the inherited value.
The result of this is that changing the font name for the default style will propagate to
all other styles, unless a different font is set.
Example: java
com.lowagie.examples.rtf.features.styles.ChangingStylesheets
Generates a document with modified paragraph stylesheets: see ChangingStylesheets.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Generates a document with modified paragraph stylesheets: see ChangingStylesheets.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Extending the list of stylesheets:
In some cases you might want to add your own stylesheets to the document. This basically
comes down to creating your own RtfParagraphStyle
object and then registering it with the RtfWriter2.
There are two ways for creating an RtfParagraphStyle
Finally you must register the new RtfParagraphStyle before calling document.open(). Otherwise while the properties will be set, the stylesheet information won't be set correctly.
Go to top of the pageThere are two ways for creating an RtfParagraphStyle
- With inheritance: use the RtfParagraphStyle(name, basedOnName) constructor, that inherits all unset properties from the RtfParagraphStyle specified by basedOnName. This is especially useful if you want to create extensions of the default style.
- Without inheritance: use the RtfParagraphStyle(styleName, fontName, fontSize, fontStyle, fontColor) constructor to create an RtfParagraphStyle that does not inherit any unset properties.
Finally you must register the new RtfParagraphStyle before calling document.open(). Otherwise while the properties will be set, the stylesheet information won't be set correctly.
// Create the new RtfParagraphStyle. The second parameter is the name of
// the RtfParagraphStyle that this style will inherit default properties from.
RtfParagraphStyle incorrectPar = new RtfParagraphStyle("Incorrect", "Normal");
// Change the desired properties
incorrectStyle.setColor(Color.RED);
incorrectStyle.setStyle(Font.STRIKETHRU);
// Register the new paragraph stylesheet with the RtfWriter2.
writer.getDocumentSettings().registerParagraphStyle(incorrectStyle);
// Create a new RtfParagraphStyle that does not inherit from any other style.
RtfParagraphStyle correctStyle = new RtfParagraphStyle("Correct", "Arial",
12, Font.NORMAL, Color.GREEN);
// Register the new paragraph stylesheet with the RtfWriter2.
writer.getDocumentSettings().registerParagraphStyle(correctStyle);
Example: java
com.lowagie.examples.rtf.features.styles.ExtendingStylesheets
Generates a document with new paragraph stylesheets: see ExtendingStylesheets.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Generates a document with new paragraph stylesheets: see ExtendingStylesheets.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar

