Sophie

Sophie

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

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: Font characteristics</title><meta name="Description" content="Changing the style of a Font."><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>Font characteristics</h2><div id="content"><div id="sidebar"><a class="toc" href="./../../index.html#fonts_styles">
							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="#measuring">Width and heigths of glyphs</a></li><li><a href="#styles">Font Styles</a></li><li><a href="#direction">Direction</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/styles/WidthHeight.java">WidthHeight</a><br><div class="description">Asking a font for the width/height of a textstring</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/widthheight.pdf">widthheight.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/FixedFontWidth.java">FixedFontWidth</a><br><div class="description">Changing the width of the font glyphs</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/fixedfontwidth.pdf">fixedfontwidth.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/FontStylePropagation.java">FontStylePropagation</a><br><div class="description">Explains the mechanism of Font Style Propagation</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/FontStylePropagation.pdf">FontStylePropagation.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/ExtraStyles.java">ExtraStyles</a><br><div class="description">Demonstrates how to underline or strike through text</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/ExtraStyles.pdf">ExtraStyles.pdf</a></li><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/ExtraStyles.rtf">ExtraStyles.rtf</a></li><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/ExtraStyles.html">ExtraStyles.html</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/FontColor.java">FontColor</a><br><div class="description">Shows how to change the color of a font</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/FontColor.pdf">FontColor.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/RightToLeft.java">RightToLeft</a><br><div class="description">Writing text from left to right</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/righttoleft.pdf">righttoleft.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/ComplexText.java">ComplexText</a><br><div class="description">Writing text from left to right at absolute positions</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/complextext.pdf">complextext.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/Vertical.java">Vertical</a><br><div class="description">Writing vertical text</div><div class="small">Extra jars needed:</div><ul><li>iTextAsian.jar</li></ul><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/vertical.pdf">vertical.pdf</a></li></ul></div><div class="example"><div class="small">ANT script (all examples):</div><ul><li><a href="./../../fonts/styles/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="measuring"></a><div class="title">Width and heigths of glyphs:</div><div xmlns="http://www.w3.org/1999/xhtml">
When getting a font from the FontFactory or when constructing a Font directly,
you always specify a <i>font size</i>. This size is an indication of the height
of the glyphs in points. It doesn't tell you how height each glyph will be and
it doesn't tell you anything about the width.<br>
The width and heights of each glyph are defined in the font file.
If you need to fit some text into a certain area, you may want to know the width and
height of some textstring in advance. You can ask the width from class
<a xmlns="" href="./../..objects/chunk/index.html#measure">Chunk</a>,
but you can also ask a basefont for its dimensions, using
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#getWidthPoint(java.lang.String,%20float)">BaseFont.getWidthPoint(java.lang.String, float)</a>,
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#getAscentPoint(java.lang.String,%20float)">BaseFont.getAscentPoint(java.lang.String, float)</a> and
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#getDescentPoint(java.lang.String,%20float)">getDescentPoint(java.lang.String, float)</a>.<br xmlns="http://www.w3.org/1999/xhtml">
Text is written on a 'baseline'. The ascent is the maximum height above reached by the glyphs in your text
above this baseline. It is always a positive value. The descent is the maximum depth below the baseline.
It is always a negative value. The height of your text is (ascent - descent).
Take a look at the example below to see how the basic methods work
and check the API if you need some more methods to get widths, ascents and descents.
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/WidthHeight.java">
						com.lowagie.examples.fonts.styles.WidthHeight</a><br>Asking a font for the width/height of a textstring: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/widthheight.pdf">widthheight.pdf</a><br></div>
In some cases you may want full control over the width of each glyph.
No problem, just use <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#getWidths()">BaseFont.getWidths()</a>,
change the widths and and force the basefont to use these changed widths with <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html#setForceWidthsOutput(boolean)">BaseFont.setForceWidthsOutput(boolean)</a>.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/FixedFontWidth.java">
						com.lowagie.examples.fonts.styles.FixedFontWidth</a><br>Changing the width of the font glyphs: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/fixedfontwidth.pdf">fixedfontwidth.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="styles"></a><div class="title">Font Styles:</div><div xmlns="http://www.w3.org/1999/xhtml">
<div class="subtitle">Fonts that don't have the style bold and/or italic</div>
Some font families don't have font definitions to show the font in bold or italic.
In this case, iText can simulate a bold font by changing the way glyphs are
rendered (see <a xmlns="" href="./../../objects/chunk/index.html#">Stroking vs. Filling</a>).
Italic can be simulated by <a href="./../../objects/chunk/index.html#">skewing</a> chunks.<br xmlns="http://www.w3.org/1999/xhtml"><br>
<div class="subtitle">Font Style Propagation</div>
There are some rules you should know about 'font propagation'.<br>
When you create a Phrase or a Paragraph  with some content in a certain font (NOT THE DEFAULT FONT) and you add some more content. The fontstyle of the initial object can be propagated if you want to. This is what happens in 'Hello 1!' and 'Hello 2' of the example.
<pre class="commandline">Phrase myPhrase =
  new Phrase("Hello 1! ",
    new Font(Font.TIMES_ROMAN, 8, Font.BOLD));
myPhrase.add(
  new Phrase("some other font ",
    new Font(Font.HELVETICA, 8)));
myPhrase.add(
  new Phrase("This is the end of the sentence.\n",
    new Font(Font.TIMES_ROMAN, 8, Font.ITALIC)));</pre> 
The initial Phrase with the text 'Hello 1!' was bold, some italic text was added, but style bold is propagated, so the style of the printed text is now bold and italic.<br>
If the Font(style) of the initial Phrase is undefined, the font is not propagated:
<pre class="commandline">myPhrase = new Phrase(12);
myPhrase.add(
  new Phrase("Hello 2! ",
    new Font(Font.TIMES_ROMAN, 8, Font.BOLD)));
myPhrase.add(
  new Phrase("This is the end of the sentence.\n",
    new Font(Font.TIMES_ROMAN, 8, Font.ITALIC)));</pre>
As you can see, 'This is the end of the sentence' is italic, but not bold. The font wasn't propagated.
The same applies if you use <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/FontFactory.html">FontFactory</a>
to get the Font-object. This is because the FontFactory uses another technique to construct a font.
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">myPhrase =
  new Phrase("Hello 3! ",
    FontFactory.getFont(FontFactory.TIMES_ROMAN, 8, Font.BOLD));
myPhrase.add(
  new Phrase("some other font ",
    FontFactory.getFont(FontFactory.HELVETICA, 8)));
myPhrase.add(
  new Phrase("This is the end of the sentence.\n",
    FontFactory.getFont(FontFactory.TIMES_ROMAN, 8, Font.ITALIC)));</pre>
As mentioned earlier: FontFactory is the safest way to get a Font. It will always give you the font style you are expecting,
regardless of the font style that was used before. Font Propagation can be useful in some cases, but it's a rather quirky feature.
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/FontStylePropagation.java">
						com.lowagie.examples.fonts.styles.FontStylePropagation</a><br>Explains the mechanism of Font Style Propagation: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/FontStylePropagation.pdf">FontStylePropagation.pdf</a><br></div>
<div xmlns="http://www.w3.org/1999/xhtml" class="subtitle">Underline and strike through</div>
Allthough underlining and striking through text isn't really font-related,
both 'style-types' are added to the <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Font.html">Font</a>-class:
<a href="http://itext.ugent.be/library/api/com/lowagie/text/Font.html#UNDERLINE">Font.UNDERLINE</a> and
<a href="http://itext.ugent.be/library/api/com/lowagie/text/Font.html#STRIKETHRU">Font.STRIKETHRU</a>.
In the example, we generate PDF, RTF and HTML. If however, you only need to generate PDF,
it is much better to use <a href="./../../objects/chunk/index.html#underline">Chunk-functionality</a>.
Font.UNDERLINE and Font.STRIKETHRU use some default values of this functionality.
If you want complete control over the height, width, color,... of the line,
you should use the method <a href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#setUnderline(java.awt.Color,%20float,%20float,%20float,%20float,%20int)">Chunk.setUnderline</a>
as described in the chapter on <a href="./../../objects/chunk/index.html#underline">Chunks</a>.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/ExtraStyles.java">
						com.lowagie.examples.fonts.styles.ExtraStyles</a><br>Demonstrates how to underline or strike through text: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/ExtraStyles.pdf">ExtraStyles.pdf</a> <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/ExtraStyles.rtf">ExtraStyles.rtf</a> <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/ExtraStyles.html">ExtraStyles.html</a><br></div>
<div xmlns="http://www.w3.org/1999/xhtml" class="subtitle">Font Color</div>
If you want to change the font color, just ask the
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/FontFactory.html">FontFactory</a>
for another font with the color of your choice:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">font red =
  FontFactory.getFont(
    FontFactory.COURIER,
    Font.DEFAULTSIZE,
    Font.NORMAL,
    new Color(0xFF, 0x00, 0x00));</pre>
It's a little more complicated if you want to change the color of some
text you want to add at an <a xmlns="" href="./../../index.html#part4">absolute position</a>.
As you can read in the <a href="./../../directcontent/text/index.html#text">Text State chapter</a>,
you have to use class <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/BaseFont.html">BaseFont</a>
and this class doesn't have a color value. In this case, you have to know that, according to
the PDF specs, characters are seen as 'shapes'. Those shapes are filled with a certain color.
So if you want to change the color of the font, you need to change the color with the 'ColorFill'
of your choice in <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html">PdfContentByte</a>:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">BaseFont bf =
  FontFactory.getFont(FontFactory.COURIER).getCalculatedBaseFont();
cb.beginText();
cb.setRGBColorFill(new Color(0x00, 0x00, 0xFF));
cb.setFontAndSize(bf, 12);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + "This text is centered", 250, 700, 0);
cb.endText();</pre>
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/FontColor.java">
						com.lowagie.examples.fonts.styles.FontColor</a><br>Shows how to change the color of a font: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/FontColor.pdf">FontColor.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="direction"></a><div class="title">Direction:</div><div xmlns="http://www.w3.org/1999/xhtml">
When adding objects to a document, iText always writes text from left to right by default.
Of course there are languages that are written in another direction.
Arabic and Hebrew for instance are written from right to left.
Most Asian characters should be written vertically.
Japanese for instance, is written in columns starting on the right top of a page.<br><br>
<div class="subtitle">Changing the run direction</div>
There are two objects in iText that allow you to change the run direction of the text:
<a xmlns="" href="./../../objects/tables/index.html#">tables</a>
and <a href="./../../objects/columns/index.html#">columns</a>.
Both objects have a method
(<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#setRunDirection(int)">ColumnText.setRunDirection(int)</a>
and <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setRunDirection(int)">PdfPTable.setRunDirection(int)</a>)
to change the run direction to one of the following values:
<ul xmlns="http://www.w3.org/1999/xhtml">
<li><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfWriter.html#RUN_DIRECTION_DEFAULT">PdfWriter.RUN_DIRECTION_DEFAULT:</a> the default run direction</li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfWriter.html#RUN_DIRECTION_NO_BIDI">PdfWriter.RUN_DIRECTION_NO_BIDI:</a> do not use bidirectional reordering</li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfWriter.html#RUN_DIRECTION_LTR">PdfWriter.RUN_DIRECTION_LTR:</a> use bidirectional reordering with left-to-right preferential run direction.</li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfWriter.html#RUN_DIRECTION_RTL">PdfWriter.RUN_DIRECTION_RTL:</a> use bidirectional reordering with right-to-left preferential run direction.</li>
</ul>
In the following example, the use of NO_BIDI and RTL is demonstrated with ColumnText (first page)
and PdfPTable (second page).
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/RightToLeft.java">
						com.lowagie.examples.fonts.styles.RightToLeft</a><br>Writing text from left to right: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/righttoleft.pdf">righttoleft.pdf</a><br></div>
If you're adding RTL text at absolute positions, you should always use a ColumnText object.
In the next example, some RTL text is added with a rotation of 20 degrees.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/ComplexText.java">
						com.lowagie.examples.fonts.styles.ComplexText</a><br>Writing text from left to right at absolute positions: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/complextext.pdf">complextext.pdf</a><br></div>
<div xmlns="http://www.w3.org/1999/xhtml" class="subtitle">Vertical text</div>
If you want to add vertical text to a PDF document, you will have to use the
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/VerticalText.html">VerticalText</a> object.
It writes Text vertically. Note that the naming is done according to horizontal text although it referrs to vertical text. A line with the alignment Element.LEFT_ALIGN will actually be top aligned.
Take a look at the example to see how it's done:
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/Vertical.java">
						com.lowagie.examples.fonts.styles.Vertical</a><br>Writing vertical text: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/fonts/styles/vertical.pdf">vertical.pdf</a><br>
						Extra jars needed in your CLASSPATH:
						 iTextAsian.jar<br></div>
</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:57
			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=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>