Sophie

Sophie

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

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: Text</title><meta name="Description" content="Add text content directly to the PDF file using the PdfContentByte class."><meta name="Keywords" content="PDF, JAVA, iText, examples, PdfContentByte, Graphics, absolute positions, 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>Text</h2><div id="content"><div id="sidebar"><a class="toc" href="./../../index.html#directcontent_text">
							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="#pdfsyntax">PDF Syntax</a></li><li><a href="#text">Text State</a></li><li><a href="#fonts">Fonts</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/directcontent/text/Text.java">Text</a><br><div class="description">Adding text at absolute positions</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/text/text.pdf">text.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/text/Logo.java">Logo</a><br><div class="description">Draws the iText logo</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/text/logo.pdf">logo.pdf</a></li></ul></div><div class="example"><div class="small">ANT script (all examples):</div><ul><li><a href="./../../directcontent/text/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=11">
				Chapter 11:
			</a>Adding color and text</li></ul></p>
		</div><a class="top" href="#top">Go to top of the page</a><a name="pdfsyntax"></a><div class="title">PDF Syntax:</div><div xmlns="http://www.w3.org/1999/xhtml">
PDF Syntax to write text looks like this:
<pre class="commandline">BT
  /F13 12 Tf
  288 720 Td
  (ABC) Tj
ET</pre>
The example above write ABC (Tj) at a certain position (Tj) using a certain font (/F13) with a certain size (12).
Text State is a subset of the graphics state and again iText offers lots of methods so that you don't have to write PDF syntax manually,
but before looking at the lists of the text state, text positioning and text showing operators,
you must know about the text object operators:
<table>
<tr>
<td>BT</td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#beginText()">beginText()</a></td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td>ET</td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#endText()">endText()</a></td>
</tr>
</table>
If you use any method mentioned below outside a beginText/endText block, your PDF reader will give you a 'damaged PDF file' warning or error.
Don't forget to use these methods!
</div><a class="top" href="#top">Go to top of the page</a><a name="text"></a><div class="title">Text State:</div><div xmlns="http://www.w3.org/1999/xhtml">
There are 9 graphics state parameters that only affect text (see table 5.1 in the PDF Reference Manual).
In the table below, I sum up the parameters (italic), the corresponding operators (bold, see table 5.2 in the Reference Manual)
and the corresponding iText method to change the text state.
<table width="100%" border="1">
<tr>
<td><i>Tc</i></td>
<td><b>Tc</b></td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#setCharacterSpacing(float)">setCharacterSpacing(float)</a></td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td><i>Tw</i></td>
<td><b>Tw</b></td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#setWordSpacing(float)">setWordSpacing(float)</a></td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td><i>Th</i></td>
<td><b>Th</b></td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#setHorizontalScaling(float)">setHorizontalScaling(float)</a></td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td><i>Tl</i></td>
<td><b>TL</b></td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#setLeading(float)">setLeading(float)</a></td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td><i>Tf</i></td>
<td rowspan="2"><b>Tf</b></td>
<td rowspan="2"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#setFontAndSize(com.lowagie.text.pdf.BaseFont,%20float)">setFontAndSize(com.lowagie.text.pdf.BaseFont, float)</a></td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml"><td><i>Tfs</i></td></tr>
<tr>
<td><i>Tmode</i></td>
<td><b>Tr</b></td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#setTextRenderingMode(int)">setTextRenderingMode(int)</a><br xmlns="http://www.w3.org/1999/xhtml">
The text rendering mode is one of the following values:
<ul>
<li><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#TEXT_RENDER_MODE_FILL">PdfContentByte.TEXT_RENDER_MODE_FILL</a></li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#TEXT_RENDER_MODE_STROKE">PdfContentByte.TEXT_RENDER_MODE_STROKE</a></li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#TEXT_RENDER_MODE_FILL_STROKE">PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE</a></li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#TEXT_RENDER_MODE_INVISIBLE">PdfContentByte.TEXT_RENDER_MODE_INVISIBLE</a></li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#TEXT_RENDER_MODE_FILL_CLIP">PdfContentByte.TEXT_RENDER_MODE_FILL_CLIP</a></li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#TEXT_RENDER_MODE_STROKE_CLIP">PdfContentByte.TEXT_RENDER_MODE_STROKE_CLIP</a></li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#TEXT_RENDER_MODE_FILL_STROKE_CLIP">PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE_CLIP</a></li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#TEXT_RENDER_MODE_CLIP">PdfContentByte.TEXT_RENDER_MODE_CLIP</a></li>
</ul>
See the chapter on <a href="./../../objects/chunk/index.html#">Chunks</a> for an example.
</td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td><i>Trise</i></td>
<td><b>Ts</b></td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#setTextRise(float)">setTextRise(float)</a></td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td><i>Tk</i></td>
<td></td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfGState.html#setTextKnockout(boolean)">PdfGState.setTextKnockout(boolean)</a></td>
</tr>
</table>
Text also has it's own coordinate system: <i xmlns="http://www.w3.org/1999/xhtml">text space</i>.
It is defined by the text matrix <i>Tm</i> and the text state parameters <i>Tfs</i>, <i>Th</i> and <i>Trise</i>,
which together form the transformation matrix text space to user space. This is the list of Text-positioning parameters
(see also Table 5.5 in the PDF Reference Manual):
<table width="100%" border="1">
<tr>
<td>Td</td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#moveText(float,%20float)">moveText(float, float)</a></td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td>Td</td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#moveTextWithLeading(float,%20float)">moveTextWithLeading(float, float)</a></td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td>Tm</td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#setTextMatrix(float,%20float)">setTextMatrix(float, float)</a>
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#setTextMatrix(float,%20float,%20float,%20float,%20float,%20float)">setTextMatrix(float, float, float, float, float, float)</a></td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td>T*</td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#newlineText()">newlineText()</a></td>
</tr>
</table>
Finally there are some text-showing operators (see Table 5.6 of the PDF Reference Manual).
<table xmlns="http://www.w3.org/1999/xhtml" width="100%" border="1">
<tr>
<td>Tj</td>
<td>
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#showText(java.lang.String)">showText(java.lang.String)</a>
</td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td>'</td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#newlineShowText(java.lang.String)">newlineShowText(java.lang.String)</a>
</td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td>"</td>
<td><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#newlineShowText(float,%20float,%20java.lang.String))">newlineShowText(float, float, java.lang.String)</a></td>
</tr>
<tr xmlns="http://www.w3.org/1999/xhtml">
<td>TJ</td>
<td>
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#showText(com.lowagie.text.pdf.PdfTextArray)">showText(com.lowagie.text.pdf.PdfTextArray)</a></td>
</tr>
</table>
iText also adds some extra methods that combine different methods that are summed up above:
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#showTextKerned(java.lang.String))">showTextKerned(java.lang.String)</a>,
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#showTextAligned(int,%20java.lang.String,%20float,%20float,%20float)">showTextAligned(int,%20java.lang.String,%20float,%20float,%20float)</a> and
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#showTextAlignedKerned(int,%20java.lang.String,%20float,%20float,%20float)">showTextAlignedKerned(int,%20java.lang.String,%20float,%20float,%20float)</a>.<br xmlns="http://www.w3.org/1999/xhtml">
This is a simple examples that writes some text at absolute positions:
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/text/Text.java">
						com.lowagie.examples.directcontent.text.Text</a><br>Adding text at absolute positions: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/text/text.pdf">text.pdf</a><br></div>
In this example, you see that graphics state methods also apply to text state:
text glyphs are shapes and if you want to change the color, you have to change the 'colorfill':
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/text/Logo.java">
						com.lowagie.examples.directcontent.text.Logo</a><br>Draws the iText logo: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/text/logo.pdf">logo.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="fonts"></a><div class="title">Fonts:</div><div xmlns="http://www.w3.org/1999/xhtml">
The first part of Chapter 5 of the PDF Reference Manual deals with
<a xmlns="" href="./../../directcontent/text/index.html#">Text State</a>;
the second part (starting with section 5.4) tells you all about fonts.<br xmlns="http://www.w3.org/1999/xhtml">
In this tutorial fonts are discussed in 3 chapters of a <a xmlns="" href="./../../index.html#part3">separate part</a>:
<ol xmlns="http://www.w3.org/1999/xhtml">
<li><a xmlns="" href="./../../fonts/index.html#">Font objects</a></li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="./../../fonts/styles/index.html#">Font characteristics</a></li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="./../../fonts/getting/index.html#">Getting Fonts</a></li>
</ol>
</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:58
			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=PDF&=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>