Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > 927a1532fb070c51449654bb68b5bde2 > files > 368

itext-manual-2.0.8-0.0.2mdv2008.1.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>iText Tutorial: The Font objects</title><meta name="Description" content="Describes the different Font objects in iText."><meta name="Keywords" content="PDF, JAVA, iText, examples, Fonts, Lowagie, Bruno"><link rel="stylesheet" href="./../style.css" type="text/css"></head><body><a name="top" class="logo" href="http://www.lowagie.com/iText"><img src="http://www.lowagie.com/iText/images/logo.gif" border="0" alt="iText"></a><h1>Tutorial: iText by Example</h1><h2>The Font objects</h2><div id="content"><div id="sidebar"><a class="toc" href="./../index.html#fonts">
							Table of Contents
						</a><div align="Center" class="small">Best viewed with:<br><script type="text/javascript"><!--
google_ad_client = "pub-0340380473790570";
google_ad_width = 180;
google_ad_height = 60;
google_ad_format = "180x60_as_rimg";
google_cpa_choice = "CAAQyaj8zwEaCIwcWMzeycafKMu293M";
//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div><div class="sidetitle">Sections:</div><ul><li><a href="#inaction">In the book</a></li><li><a href="#font">The Font-class</a></li><li><a href="#fontfactory">The FontFactory-class</a></li><li><a href="#basefont">The BaseFont-class</a></li><li><a href="#more">to be continued...</a></li></ul><br><br><div class="sidetitle">Examples:</div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/StandardType1Fonts.java">StandardType1Fonts</a><br><div class="description">Sums up the 14 Standard Type 1 Fonts</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/StandardType1Fonts.pdf">StandardType1Fonts.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/FontFactoryType1Fonts.java">FontFactoryType1Fonts</a><br><div class="description">Sums up the 14 Standard Type 1 Fonts (using the FontFactory)</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/FontFactoryType1Fonts.pdf">FontFactoryType1Fonts.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/TrueType.java">TrueType</a><br><div class="description">Using a True Type font</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/truetype.pdf">truetype.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/FullFontNames.java">FullFontNames</a><br><div class="description">Retrieving the full font name</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/fullfontname_arialbi.txt">fullfontname_arialbi.txt</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/ListEncodings.java">ListEncodings</a><br><div class="description">Asking the font for its available encodings</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/encodings.txt">encodings.txt</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/FontEncoding.java">FontEncoding</a><br><div class="description">Using a True Type font that will be embedded in the PDF</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/fontencoding.pdf">fontencoding.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/UnicodeExample.java">UnicodeExample</a><br><div class="description">Using BaseFont.IDENTITY_H as 'encoding'</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/unicode.pdf">unicode.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/EncodingFont.java">EncodingFont</a><br><div class="description">Using an encoding to display characters from other alphabets</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/encodingfont.pdf">encodingfont.pdf</a></li></ul></div><div class="example"><div class="small">ANT script (all examples):</div><ul><li><a href="./../fonts/build.xml">
					build.xml
				</a></li></ul></div></div><div id="main"><a name="inaction"></a><div class="title">In the book:</div><div xmlns="http://www.w3.org/1999/xhtml" xmlns:site="http://www.lowagie.com/iText/site">
		<p xmlns=""><a href="http://itext.ugent.be/itext-in-action/"><img src="http://itext.ugent.be/img/lowagie_3d.jpg" border="0" align="right"></a>
			The examples in this free online tutorial will help you getting started
			with iText. Note that most examples are two years old.
			Some of the examples may be obsolete. Also the theory that
			comes with the examples isn't always 100% accurate.
			If you want more recent examples or if you want to know more
			about the theoretical background of	PDF and iText, please consult the book
			<a href="http://itext.ugent.be/itext-in-action/">
				"iText in Action".
			</a>
			Note that the first and the third chapter of the book
			can be downloaded for free from <a href="http://www.manning.com/affiliate/idevaffiliate.php?id=223_53">http://manning.com/lowagie/</a></p><p>
			More specifically:
			<ul><li><a class="subtitle" href="http://itext.ugent.be/itext-in-action/chapter.php?chapter=8">
				Chapter 8:
			</a>Choosing the right font</li><li><a class="subtitle" href="http://itext.ugent.be/itext-in-action/chapter.php?chapter=9">
				Chapter 9:
			</a>Using fonts</li></ul></p>
		</div><a class="top" href="#top">Go to top of the page</a><a name="font"></a><div class="title">The Font-class:</div><div xmlns="http://www.w3.org/1999/xhtml">
There are 14 standard fonts that should be available by default in each PDF reader.
These fonts are Courier, Courier Bold, Courier Italic (Oblique), Courier Bold and Italic,
Helvetica, Helvetica Bold, Helvetica Italic (Oblique), Helvetica Bold and Italic,
Times Roman, Times Roman Bold, Times Roman Italic, Times Roman Bold and Italic,
Symbol and ZapfDingBats&reg; (see also the section on <a xmlns="" href="./../fonts/getting/index.html#builtin">built-in fonts</a>).
As you can see the different faces (normal, bold, italic and bolditalic) of the same type face (Courier, Helvetica, Times Roman)
are considered to be different fonts. This is an issue that shall recur in the sections on
<site xmlns="http://www.w3.org/1999/xhtml" chapter="/fonts/getting" section="files">getting fonts from files</site> and <site chapter="fonts/styles" section="styles">font styles</site>.
iText tries to make these differences more transparant, by asking you
for the font 'family' (Courier, Helvetica, Times Roman) and the font style (normal, bold, italic).<br>
You can construct a Base 14 <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Font.html">Font</a>-object with one
of the simple <a href="http://itext.ugent.be/library/api/com/lowagie/text/Font.html#constructor_summary">constructors</a>
as is done in the next example:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">fonts[0] =
  new Font(Font.COURIER, Font.DEFAULTSIZE, Font.NORMAL);
fonts[2] =
  new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD);
fonts[3] =
  new Font(Font.COURIER, Font.DEFAULTSIZE, Font.BOLD | Font.ITALIC);
fonts[7] =
  new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.BOLDITALIC);
fonts[9] =
  new Font(Font.TIMES_ROMAN, Font.DEFAULTSIZE, Font.ITALIC);</pre>
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/StandardType1Fonts.java">
						com.lowagie.examples.fonts.StandardType1Fonts</a><br>Sums up the 14 Standard Type 1 Fonts: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/StandardType1Fonts.pdf">StandardType1Fonts.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="fontfactory"></a><div class="title">The FontFactory-class:</div><div xmlns="http://www.w3.org/1999/xhtml">
In the next example, we use the class <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/FontFactory.html">FontFactory</a> to create a Font,
as this is the easiest way to get a new <a href="http://itext.ugent.be/library/api/com/lowagie/text/Font.html">Font</a>-object
(unless you want to use the mechanism of <a href="./../fonts/styles/index.html#styles">Font Propagation</a>).
If you read the chapter on <a href="./../fonts/getting/index.html#">getting fonts</a>,
you will see that there are different kinds of font 'sources'. Fontfactory has an API
with some methods that can construct a Font object of any kind in a uniform way (except for some specialized CID fonts).
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">fonts[0] =
  FontFactory.getFont(
    FontFactory.COURIER,
    Font.DEFAULTSIZE,
    Font.NORMAL);
fonts[2] =
  FontFactory.getFont(
    FontFactory.COURIER,
    Font.DEFAULTSIZE,
    Font.BOLD);
fonts[3] =
  FontFactory.getFont(
    FontFactory.COURIER,
    Font.DEFAULTSIZE,
    Font.BOLD | Font.ITALIC);
fonts[7] =
  FontFactory.getFont(
    FontFactory.HELVETICA,
    Font.DEFAULTSIZE,
    Font.BOLDITALIC);
fonts[9] =
  FontFactory.getFont(
    FontFactory.TIMES_ROMAN,
    Font.DEFAULTSIZE,
    Font.ITALIC);</pre>
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/FontFactoryType1Fonts.java">
						com.lowagie.examples.fonts.FontFactoryType1Fonts</a><br>Sums up the 14 Standard Type 1 Fonts (using the FontFactory): see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/FontFactoryType1Fonts.pdf">FontFactoryType1Fonts.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="basefont"></a><div class="title">The BaseFont-class:</div><div xmlns="http://www.w3.org/1999/xhtml">
The <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Font.html">Font</a>-object returned by
the <a href="http://itext.ugent.be/library/api/com/lowagie/text/FontFactory.html">FontFactory</a>
is in many cases just a wrapper class for the more complex class
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html">BaseFont</a>.
You can create such a basefont directly by using one of the
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#createFont(java.lang.String,%20java.lang.String,%20boolean)">BaseFont.createFont</a> methods:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">BaseFont helvetica =
  BaseFont.createFont(
    BaseFont.HELVETICA,
    BaseFont.CP1252,
    BaseFont.NOT_EMBEDDED);</pre>
<ol>
<li>The first parameter should the name of the font or the location where it can be found.
On windows systems, most fonts are in the directory C:\WINDOWS\FONTS, so this could be a correct way to construct a basefont object for Arial:
<pre class="commandline">BaseFont arial =
  BaseFont.createFont(
    "C:\\WINDOWS\\FONTS\\ARIAL.TTF",
    BaseFont.CP1252,
    BaseFont.NOT_EMBEDDED);</pre>
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/TrueType.java">
						com.lowagie.examples.fonts.TrueType</a><br>Using a True Type font: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/truetype.pdf">truetype.pdf</a><br></div>
You can ask a font for its full font names. Take a look at the example.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/FullFontNames.java">
						com.lowagie.examples.fonts.FullFontNames</a><br>Retrieving the full font name: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/fullfontname_arialbi.txt">fullfontname_arialbi.txt</a><br></div>
We create a font using the path to its ttf-file.
Then we ask the ttf file for the fontnames.
(Skip to the section on <a href="./../fonts/getting/index.html#files">fonts from files</a>
to learn how you can register complete font-directories with one method, so that you don't have to use the
path to each separate font anytime you want to create a Font object.)</li>
<li xmlns="http://www.w3.org/1999/xhtml">The second parameter is the <i>encoding</i>. A <i>character</i> is an abstract symbol, whereas a <i>glyph</i> is a specific graphical rendering of a character.
For example, the glyphs A, <b>A</b> and <i>A</i> are renderings of the abstract "A" character.
The encoding is the association between the character codes (obtained from text strings) and glyph descriptions.
The same Java character in a Java String object can be rendered in many different ways, depending on the encoding.
Basefont has some default values, but you could use any of the encodings that are available in the font you are using.
<table border="1">
<tr><td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#WINANSI">BaseFont.WINANSI</a> = <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#CP1252">BaseFont.CP1252</a></td><td xmlns="http://www.w3.org/1999/xhtml">Latin 1</td></tr>
<tr><td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#CP1250">BaseFont.CP1250</a></td><td xmlns="http://www.w3.org/1999/xhtml">Latin 2: Eastern Europe</td></tr>
<tr><td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#CP1257">BaseFont.CP1257</a>1257</td><td xmlns="http://www.w3.org/1999/xhtml">Windows Baltic</td></tr>
<tr><td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#MACROMAN">BaseFont.MACROMAN</a></td><td xmlns="http://www.w3.org/1999/xhtml">MacRomanEncoding</td></tr>
<tr><td colspan="2">You could easily extend the list with "1251" (Cyrillic), "1253" (Greek), "1254" (Turkish)</td></tr>
</table>
You can ask a font for its encodings as is done in the next example:
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/ListEncodings.java">
						com.lowagie.examples.fonts.ListEncodings</a><br>Asking the font for its available encodings: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/encodings.txt">encodings.txt</a><br></div>
Note that not all fonts contain all the encodings you want. Just go looking for the font that is best suited for your needs.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/FontEncoding.java">
						com.lowagie.examples.fonts.FontEncoding</a><br>Using a True Type font that will be embedded in the PDF: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/fontencoding.pdf">fontencoding.pdf</a><br></div>
In the next example, we are passing the value <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#IDENTITY_H">IDENTITY_H</a>
as <i xmlns="http://www.w3.org/1999/xhtml">encoding</i>. BaseFont.IDENTITY_H and BaseFont.IDENTITY_V are not really encodings.
They indicate that the unicode character wil be looked up in the font
and stored as-is, taking two bytes of space. It's the only way to have
Asian fonts and some encodings left out by Adobe such as Thai.
For Europe or the Middle-East, it is better to use an available
encoding that will store a single byte per character.
Fonts with BaseFont.IDENTITY_H or BaseFont.IDENTITY_V
will always be embedded no matter what you enter as third parameter.
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/UnicodeExample.java">
						com.lowagie.examples.fonts.UnicodeExample</a><br>Using BaseFont.IDENTITY_H as 'encoding': see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/unicode.pdf">unicode.pdf</a><br></div>
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/EncodingFont.java">
						com.lowagie.examples.fonts.EncodingFont</a><br>Using an encoding to display characters from other alphabets: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/encodingfont.pdf">encodingfont.pdf</a><br></div></li>
<li xmlns="http://www.w3.org/1999/xhtml">With the third parameter you specify if you want to embed (a subset of) a font into the PDF document.
This is necessary if you are using special fonts that aren't on the system used by the consumer of your document.
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/FontEncoding.java">
						com.lowagie.examples.fonts.FontEncoding</a><br>Using a True Type font that will be embedded in the PDF: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/fontencoding.pdf">fontencoding.pdf</a><br></div></li>
</ol>
</div><a class="top" href="#top">Go to top of the page</a><a name="more"></a><div class="title">to be continued...:</div><div xmlns="http://www.w3.org/1999/xhtml">
There more on fonts in the chapters <a xmlns="" href="./../fonts/getting/index.html#">Getting fonts</a> (built-in, fonts from files, fonts from jars, CID fonts)
and <a href="./../fonts/styles/index.html#">Font characteristics</a> (changing the style, changing the write direction,...).
</div><a class="top" href="#top">Go to top of the page</a><div id="footer" xmlns="http://www.w3.org/1999/xhtml">
			Page Updated: 2006/09/17 10:13:51
			Copyright &copy; 1999-2005
			Bruno Lowagie<br><a href="http://www.lowagie.com/iText/">iText</a> is a Free Java-Pdf library by Bruno Lowagie and Paulo Soares.
		</div></div></div><div class="commercial"><br><script type="text/javascript"><!--
google_ad_client = "pub-0340380473790570";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_ad_channel ="";
google_ad_type = "text_image";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "1B09BD";
google_color_url = "100670";
google_color_text = "707070";
//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script><br><br><div class="subtitle">Amazon books:</div><script type="text/javascript"><!--
document.write('<iframe src="http://rcm.amazon.com/e/cm?t=itisacatalofwebp&o=1&p=8&l=as1&asins=1932394796&fc1=000000&lc1=0000ff&bc1=&lt1=_blank&IS2=1&bg1=ffffff&f=ifr" width="120" height="240" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" align="Center"></iframe>');
//--></script><a xmlns="" href="http://www.amazon.co.uk/exec/obidos/ASIN/1932394796/catloogjecom-21" class="amazonlinks">
			amazon.co.uk-link
		</a><br><br><script type="text/javascript"><!--
document.write('<iframe src="http://rcm.amazon.com/e/cm?t=itisacatalofwebp&o=1&p=8&l=as1&asins=0321304748&fc1=000000&lc1=0000ff&bc1=&lt1=_blank&IS2=1&bg1=ffffff&f=ifr" width="120" height="240" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" align="Center"></iframe>');
//--></script><a href="http://www.amazon.co.uk/exec/obidos/ASIN/0321304748/catloogjecom-21" class="amazonlinks">
			amazon.co.uk-link
		</a><br><br><script type="text/javascript"><!--
document.write('<iframe src="http://rcm.amazon.com/e/cm?t=itisacatalofwebp&o=1&p=8&l=as1&asins=0201570440&fc1=000000&lc1=0000ff&bc1=&lt1=_blank&IS2=1&bg1=ffffff&f=ifr" width="120" height="240" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" align="Center"></iframe>');
//--></script><a href="http://www.amazon.co.uk/exec/obidos/ASIN/0201570440/catloogjecom-21" class="amazonlinks">
			amazon.co.uk-link
		</a><br><br><script type="text/javascript"><!--
document.write('<iframe src="http://rcm.amazon.com/e/cm?t=itisacatalofwebp&o=1&p=10&l=st1&mode=books&search=Fonts&=1&fc1=&lc1=&lt1=&bg1=&f=ifr" width="120" height="460" border="0" frameborder="0" style="border:none;" scrolling="no" marginwidth="0" marginheight="0"></iframe>');
//--></script><br></div></body></html>