Tutorial: iText by Example

Extended font support

Extended font support:
iText provides a number of built-in fonts and these can be extended by embedding further fonts. Unfortunately embedding fonts is not supported in the RTF package and the RtfFont closes this gap.

The RtfFont extends the the basic iText Font and can thus be used anywhere that the Font can be used. Using it is simple, you just specify the name of the font that you wish to use and then use the RtfFont like any other Font. The important thing is that the name of the font specified exactly matches the name of the font on the target system, otherwise it will not be used.
// Create a RtfFont with the desired font name.
RtfFont msComicSans = new RtfFont("Comic Sans MS");
            
// Use the RtfFont like any other Font.
document.add(new Paragraph("This paragraph uses the" +
    " Comic Sans MS font.", msComicSans));
    	  
The example above only demonstrates setting the font name, but there are also constructors with which you can specify font size, font style and colour.
Example: java com.lowagie.examples.rtf.extensions.font.ExtendedFont
Generates a simple document using the RtfFont class: see ExtendedFont.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
One thing to keep in mind when using the RtfFont is that the font is NOT embedded. This means that for it to be used when displaying the RTF document, the desired font has to be available on the system that displays the font. Whether the font is available on the system that generated the RTF document is irrelevant.
Go to top of the page
Extended font styles:
In addition to providing access to further font names, the RtfFont provides a number of additional font styles that can be used when generating RTF documents. These styles can be used in place of the styles provided by Font or combined with them.

The following extra styles are provides:
  • STYLE_DOUBLE_STRIKETHROUGH: A double strikethrough line.
  • STYLE_SHADOW: Text with a shadow.
  • STYLE_OUTLINE: Outlined text.
  • STYLE_EMBOSSED: Embossed text.
  • STYLE_ENGRAVED: Engraved text.
  • STYLE_HIDDEN: Text that is initially hidden from user view.
// Use the RtfFont.STYLE_* instead of the Font styles.
RtfFont doubleStrikethrough = new RtfFont("Arial", RtfFont.UNDEFINED,
    RtfFont.STYLE_DOUBLE_STRIKETHROUGH);

// Or combine them with Font styles.
RtfFont engravedItalic = new RtfFont("Arial", RtfFont.UNDEFINED,
    RtfFont.STYLE_ENGRAVED | Font.ITALIC);

// The hidden style is special since it hides text.
RtfFont hidden = new RtfFont("Arial", RtfFont.UNDEFINED,
    RtfFont.STYLE_HIDDEN);

Paragraph paragraph = new Paragraph("This text is ", new RtfFont("Arial", 12));
            
// Use the RtfFonts when creating the text.
paragraph.add(new Chunk("deleted,", doubleStrikethrough));
paragraph.add(new Chunk(" engraved and italic", engravedItalic));
paragraph.add(" and");
paragraph.add(new Chunk(" you won't see this", hidden));
paragraph.add(" nothing.");
            
document.add(paragraph);
		  
Example: java com.lowagie.examples.rtf.extensions.font.ExtendedFontStyles
Generates a simple document using the additional font styles in the RtfFont class: see ExtendedFontStyles.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Go to top of the page

iText, the #1 Java-PDF library




Amazon books:
amazon.co.uk-link