Sophie

Sophie

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

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 Chunk Object</title><meta name="Description" content="Describes the functionality of the Chunk object."><meta name="Keywords" content="PDF, JAVA, iText, examples, Chunk, 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 Chunk Object</h2><div id="content"><div id="sidebar"><a class="toc" href="./../../index.html#objects_chunk">
							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="#intro">Introduction</a></li><li><a href="#underline">Adding lines under, through or above a Chunk</a></li><li><a href="#subsup">Sub- and Superscript</a></li><li><a href="#background">Changing the background color</a></li><li><a href="#rendering">Stroking vs. Filling</a></li><li><a href="#skew">Skewing text</a></li><li><a href="#generic">Generic functionality</a></li><li><a href="#measure">Measuring and Scaling a Chunk</a></li><li><a href="#endline">When the end of a line is reached</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/objects/chunk/Lines.java">Lines</a><br><div class="description">Demonstrates how to add lines under, through,... a Chunk</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Lines.pdf">Lines.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/SubSupScript.java">SubSupScript</a><br><div class="description">Demonstrates the use of sub- and superscript</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/SubSupScript.pdf">SubSupScript.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Background.java">Background</a><br><div class="description">How to change the background color of a Chunk</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Background.pdf">Background.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/ChunkColor.java">ChunkColor</a><br><div class="description">Changing the color of a Chunk</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/ChunkColor.pdf">ChunkColor.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Rendering.java">Rendering</a><br><div class="description">Some special rendering functionality</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Rendering.pdf">Rendering.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Skew.java">Skew</a><br><div class="description">Skewing Chunks</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Skew.pdf">Skew.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Generic.java">Generic</a><br><div class="description">Using the Generic tag to add styles</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Generic.pdf">Generic.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Glossary.java">Glossary</a><br><div class="description">Other use of the Generic tag: register keywords for a glossary</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Glossary.pdf">Glossary.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Width.java">Width</a><br><div class="description">How to measure and scale the width of a Chunk</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Width.pdf">Width.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/EndOfLine.java">EndOfLine</a><br><div class="description">What happens when the end of the line is reached?</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/EndOfLine.pdf">EndOfLine.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/SplitChar.java">SplitChar</a><br><div class="description">Defining Split Characters</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/SplitChar.pdf">SplitChar.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Hyphenation.java">Hyphenation</a><br><div class="description">Using auto-hyphenation</div><div class="small">Extra jars needed:</div><ul><li>itext-hyph-xml.jar</li></ul><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Hyphenation.pdf">Hyphenation.pdf</a></li></ul></div><div class="example"><div class="small">ANT script (all examples):</div><ul><li><a href="./../../objects/chunk/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=4">
				Chapter 4:
			</a>Composing text elements</li></ul></p>
		</div><a class="top" href="#top">Go to top of the page</a><a name="intro"></a><div class="title">Introduction:</div><div xmlns="http://www.w3.org/1999/xhtml">
A Chunk is the smallest significant part of text that can be added to a document.
It is the 'atom' building block of most of the other High Level Text objects.
All the contents of a Chunk are of the same font, fontsize, style, color, etc...
In the following sections, all Chunk-functionality will be described:
</div><a class="top" href="#top">Go to top of the page</a><a xmlns="" name="underline"></a><div class="title">Adding lines under, through or above a Chunk:</div><div xmlns="http://www.w3.org/1999/xhtml">
As you can read in the chapter on <a xmlns="" href="./../../fonts/index.html#extrastyles">Fonts</a>,
you can define the style as <a href="http://itext.ugent.be/library/api/com/lowagie/text/Font.html#UNDERLINE">Font.UNDERLINE</a> or
<a href="http://itext.ugent.be/library/api/com/lowagie/text/Font.html#STRIKETHRU">Font.STRIKETHRU</a> to underline the text or
strike through it. This works for RTF and HTML, but in PDF, you get a lot more functionality if you 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>.
There are two variations of this method:
<ul xmlns="http://www.w3.org/1999/xhtml"><li>
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#setUnderline(float,%20float)">Chunk.setUnderline(float thickness, float yPosition)</a> draws a line that has the
length of the Chunk, is <span xmlns="http://www.w3.org/1999/xhtml" class="commandline">thickness</span> points thick and positioned <span class="commandline">yPosition</span> above the baseline of the Chunk.
<pre class="commandline">Chunk underlined = new Chunk("underlined");
underlined.setUnderline(0.2f, -2f);
Chunk strikethru = new Chunk("strike through example");
strikethru.setUnderline(0.5f, 3f);</pre></li>
<li>
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#setUnderline(java.awt.Color,%20float,%20float,%20float,%20float,%20int)">Chunk.setUnderline(Color color, float thickness, float thicknessMul, float yPosition, float yPositionMul, int cap)</a>
lets you define the color of the line. You can set the absolute thickness and y postition with
<span xmlns="http://www.w3.org/1999/xhtml" class="commandline">thickness</span> and <span class="commandline">yPosition</span>,
but you can also let these values depend on the fontsize with
<span class="commandline">thicknessMul</span> and <span class="commandline">yPositionMul</span>.
In the example, the same Chunk with the same linedefinitions is written twice, but with a different
fontsize. As you can see, some lines vary along with the fontsize, others don't. This all depends
on the parameters that were passed with the method setUnderline.
Finally you can define the line cap style (see Table 4.4 in the PDF Reference Manual).<br>
<img xmlns="" border="0" src="./../../images/linecap.gif" alt="line cap styles">
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">Chunk c = new Chunk("Multiple lines");
c.setUnderline(new Color(0xFF, 0x00, 0x00),
	0.0f, 0.3f, 0.0f, 0.4f,
	PdfContentByte.LINE_CAP_ROUND);
c.setUnderline(new Color(0x00, 0xFF, 0x00),
	5.0f, 0.0f, 0.0f, -0.5f,
	PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
c.setUnderline(new Color(0x00, 0x00, 0xFF),
	0.0f, 0.2f, 15.0f, 0.0f,
	PdfContentByte.LINE_CAP_BUTT);</pre></li></ul>
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Lines.java">
						com.lowagie.examples.objects.chunk.Lines</a><br>Demonstrates how to add lines under, through,... a Chunk: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Lines.pdf">Lines.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="subsup"></a><div class="title">Sub- and Superscript:</div><div xmlns="http://www.w3.org/1999/xhtml">
If you want to add a Chunk above or below the current y-position,
you can use the method <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#setTextRise(float)">setTextRise</a>.
In the example, we marked the baseline with <span xmlns="http://www.w3.org/1999/xhtml" class="commandline">c.setUnderline(new Color(0xC0, 0xC0, 0xC0), 0.2f, 0.0f, 0.0f, 0.0f, PdfContentByte.LINE_CAP_BUTT);</span>
to demonstrate how a positive textrise puts text above the baseline and a negative textrise below.<br>
Note that 'underline' definitions are not affected by this value.
The underline has to follow the baseline otherwise mixing normal text
and superscript would cause a discontinuity. As for subscript: it doesn't
have much sense mixing it with underline text.
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/SubSupScript.java">
						com.lowagie.examples.objects.chunk.SubSupScript</a><br>Demonstrates the use of sub- and superscript: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/SubSupScript.pdf">SubSupScript.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="background"></a><div class="title">Changing the background color:</div><div xmlns="http://www.w3.org/1999/xhtml">
With the methods <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#setBackground(java.awt.Color)">setBackground(Color color)</a> and
<a href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#setBackground(java.awt.Color,%20float,%20float,%20float,%20float)">setBackground(Color color, float extraLeft, float extraBottom, float extraRight, float extraTop)</a>,
you can change the background area of a Chunk (for instance to highlight a word). The first method fills the box that surrounds the Chunk,
with the second method, you can make the rectangle bigger or smaller.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Background.java">
						com.lowagie.examples.objects.chunk.Background</a><br>How to change the background color of a Chunk: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Background.pdf">Background.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="rendering"></a><div class="title">Stroking vs. Filling:</div><div xmlns="http://www.w3.org/1999/xhtml">
In the PDF syntax characters (glyphs) are regarded as filled shapes.
If you want to change the color of a character, you change the
<a xmlns="" href="./../../fonts/styles/index.html#styles">color of the font</a>,
but in reality you change the 'fill' color. This is demonstrated in the example:
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/ChunkColor.java">
						com.lowagie.examples.objects.chunk.ChunkColor</a><br>Changing the color of a Chunk: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/ChunkColor.pdf">ChunkColor.pdf</a><br></div>
With the method <a href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#setTextRenderMode(int,%20float,%20java.awt.Color)">setTextRenderMode(int mode, float strokeWidth, Color strokeColor)</a>,
you can also change the 'outline' of the text. As you can read in the chapters
on <a href="./../../index.html#part4">direct content</a>,
lines are not 'filled', but 'striken'. With the parameters <span xmlns="http://www.w3.org/1999/xhtml" class="commandline">strokeWidth</span>
and <span class="commandline">strokeColor</span>, you define the width and the color of the strokes
used to draw the character.<br>
The <span class="commandline">mode</span> parameter, can be one of the following values:
<ol>
<li><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/PdfContentByte.html#TEXT_RENDER_MODE_FILL">PdfContentByte.TEXT_RENDER_MODE_FILL</a>: glyphs will be filled (fontcolor), the strokeWidth and strokeColor don't play a role here.</li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/PdfContentByte.html#TEXT_RENDER_MODE_STROKE">PdfContentByte.TEXT_RENDER_MODE_STROKE</a>: you will only see the outline of the glyphs, with the given strokeWidth and strokeColor.</li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/PdfContentByte.html#TEXT_RENDER_MODE_FILL_STROKE">PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE</a>: glyphs will be filled with the fontcolor and will have an outline with the given strokeWidth and strokeColor.</li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/PdfContentByte.html#TEXT_RENDER_MODE_INVISIBLE">PdfContentByte.TEXT_RENDER_MODE_INVISIBLE</a>: the glyphs will be invisible.</li>
</ol>
This method can also be used to simulate a bold font (for instance if you are using a font that doesn't support
<a href="http://itext.ugent.be/library/api/com/lowagie/text/Font.html#BOLD">Font.Bold</a>).
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Rendering.java">
						com.lowagie.examples.objects.chunk.Rendering</a><br>Some special rendering functionality: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Rendering.pdf">Rendering.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="skew"></a><div class="title">Skewing text:</div><div xmlns="http://www.w3.org/1999/xhtml">
Method <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#setSkew(float,%20float)">setSkew(float alpha, float beta)</a> can be
used to simulate italic fonts (if you are using a font that doesn't support
<a href="http://itext.ugent.be/library/api/com/lowagie/text/Font.html#ITALIC">Font.ITALIC</a>),
just take 0f as value for alfa and 12f as value for beta.<br xmlns="http://www.w3.org/1999/xhtml">
Alfa is the angle of the baseline in degrees. In the example, we rotate some text
45f and -45f degrees. Beta is the square-angle on the baseline.
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Skew.java">
						com.lowagie.examples.objects.chunk.Skew</a><br>Skewing Chunks: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Skew.pdf">Skew.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="generic"></a><div class="title">Generic functionality:</div><div xmlns="http://www.w3.org/1999/xhtml">If all the features summed up above still doesn't answer your needs,
you can create your own functionality, using the PageEvent
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPageEvent.html#onGenericTag(com.lowagie.text.pdf.PdfWriter,%20com.lowagie.text.Document,%20com.lowagie.text.Rectangle,%20java.lang.String)">onGenericTag(PdfWriter writer,
Document document, Rectangle rect, String text)</a>.
You can create your own class implementing the
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPageEvent.html">PdfPageEvent</a>-interface.
The best way to do this, is to extend the class <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPageEventHelper.html">PdfPageEventHelper</a>
and define some custom functionality, overriding the <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPageEvent.html#onGenericTag(com.lowagie.text.pdf.PdfWriter,%20com.lowagie.text.Document,%20com.lowagie.text.Rectangle,%20java.lang.String)">onGenericTag</a>-method.
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">public class Generic extends PdfPageEventHelper {
  public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
    // writer is the current writer to which the Chunk is written
    // document is the document to which the Chunk was added
    // rect is the area surrounding the Chunk
    // text is the text you passed with setGenericTag
  }
}</pre>
Don't forget to set the PageEvent:
<pre class="commandline">writer.setPageEvent(new Generic());</pre>
In the first example, we look at the text that is passed with the genericTag
(<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#setGenericTag(java.lang.String)">setGenericTag(String text)</a>).
If this text equals to "ellipse", we draw an ellipse OVER the Chunk.
It we passed the word "box", we draw a rectangle UNDER the Chunk.
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
  if ("ellipse".equals(text)) {
    PdfContentByte cb = writer.getDirectContent();
    cb.setRGBColorStroke(0xFF, 0x00, 0x00);
    cb.ellipse(rect.left(), rect.bottom() - 5f, rect.right(), rect.top());
    cb.stroke();
    cb.resetRGBColorStroke();
  }
  else if ("box".equals(text)) {
    PdfContentByte cb = writer.getDirectContentUnder();
    rect.setGrayFill(0.5f);
    cb.rectangle(rect);
  }
}</pre>
Read the chapters on <a xmlns="" href="./../../index.html#part4">direct content</a>
to learn about the PDF syntax that gives you almost unlimited possibilities.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Generic.java">
						com.lowagie.examples.objects.chunk.Generic</a><br>Using the Generic tag to add styles: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Generic.pdf">Generic.pdf</a><br></div>
In the following example, we use the generic tag to keep a TreeMap of words
and we register the page on which the words were used. Once the document is completed,
we add an extra page, that gives us a glossary of words. It's a very simple example,
but with some changes, you can do a lot of really complex stuff with it.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Glossary.java">
						com.lowagie.examples.objects.chunk.Glossary</a><br>Other use of the Generic tag: register keywords for a glossary: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Glossary.pdf">Glossary.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="measure"></a><div class="title">Measuring and Scaling a Chunk:</div><div xmlns="http://www.w3.org/1999/xhtml">
If you have a Chunk and you want to know it's width on a page,
you can use the method <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#getWidthPoint()">getWidthPoint()</a>.
It gives you the width in points and you will have to do
<a href="./../../general/faq/index.html#measurements">maths</a>
to know the width in inches or centimeters.<br xmlns="http://www.w3.org/1999/xhtml">
With the method <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#setHorizontalScaling(float)">setHorizontalScaling(float scale)</a>,
you can shrink (scale &lt; 1.0f) or expand (scale &gt; 1.0f) the Chunk.
In the example we first print the Chunk in its actual size,
then we print it twice at half its size (scale = 0.5f).
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Width.java">
						com.lowagie.examples.objects.chunk.Width</a><br>How to measure and scale the width of a Chunk: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/Width.pdf">Width.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="endline"></a><div class="title">When the end of a line is reached:</div><div xmlns="http://www.w3.org/1999/xhtml">
<div class="subtitle">Phrase</div>
If you keep on adding Chunks to a document, you reach the end of the line at a certain moment.
Of course, you haven't defined yet, what iText should do when this happens.
By default, iText performs a 'carriage return', but what about 'newline'?
How much space should iText take? The space between two (base)lines in a text,
is called 'leading' and you can't define it in class Chunk. You need other classes
such as <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Paragraph.html">Paragraph</a> for this
kind of stuff.<br xmlns="http://www.w3.org/1999/xhtml">
In the following example we use the lesser known (and a little bit superfluous) class
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Phrase.html">Phrase</a>. A Phrase is a series of chunks
(that can have different styles) and with a certain leading as extra parameter.
Unlike class Paragraph, it doesn't know anything about indentation. The default fontsize
is 12, so we take a leading of 16. Take a look at the first page of the example:
all Chunks are written on the same line, over and over again. On the second page,
we added the same Chunks, but grouped in a Phrase with a leading of 16. When the
end of the line is reached, a new line is started.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/EndOfLine.java">
						com.lowagie.examples.objects.chunk.EndOfLine</a><br>What happens when the end of the line is reached?: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/EndOfLine.pdf">EndOfLine.pdf</a><br></div>
<div xmlns="http://www.w3.org/1999/xhtml" class="subtitle">SplitCharacter</div>
The default behaviour of iText, is to try to add as many 'complete words' to a line as possible.
iText will split sentences when a space or a hyphen is encountered. But what if the Chunk is longer than the
page width? In that case, iText will split the Chunk, just before the first character that doesn't
fit the page. In some cases, you don't want this to happen. You can avoid this by implementing
the interface <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/SplitCharacter.html">SplitCharacter</a>
and add this class to the Chunk with <a href="http://itext.ugent.be/library/api/com/lowagie/text/Chunk.html#setSplitCharacter(com.lowagie.text.SplitCharacter)">setSplitCharacter(SplitCharacter splitCharacter)</a>.
You have to write your own <a href="http://itext.ugent.be/library/api/com/lowagie/text/SplitCharacter.html#isSplitCharacter(int,%20int,%20int,%20char[],%20com.lowagie.text.pdf.PdfChunk[])">isSplitCharacter</a>,
which may seem rather complex at first sight. This is the default implementation:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">public boolean isSplitCharacter(int start, int current, int end, char[] cc, PdfChunk[] ck) {
   char c;
   if (ck == null)
       c = cc[current];
   else
       c = ck[Math.min(current, ck.length - 1)].getUnicodeEquivalent(cc[current]);
   if (c &lt;= ' ' || c == '-') {
       return true;
   }
   if (c &lt; 0x2e80)
       return false;
   return ((c &gt;= 0x2e80 &amp;&amp; c &lt; 0xd7a0)
   || (c &gt;= 0xf900 &amp;&amp; c &lt; 0xfb00)
   || (c &gt;= 0xfe30 &amp;&amp; c &lt; 0xfe50)
   || (c &gt;= 0xff61 &amp;&amp; c &lt; 0xffa0));
}</pre>
You can copy this method in your own <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/SplitCharacter.html">SplitCharacter</a> implementation and make these small changes to have iText split words on dots and slashes too:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">if (c &lt;= ' ' || c == '-' || c == '.' || c == '/') {
    return true;
}</pre>
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/SplitChar.java">
						com.lowagie.examples.objects.chunk.SplitChar</a><br>Defining Split Characters: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/chunk/SplitChar.pdf">SplitChar.pdf</a><br></div>
<div xmlns="http://www.w3.org/1999/xhtml" class="subtitle">Hyphenation</div>
To conclude this section, it is also possible to let iText hyphenate your
Chunks automatically. To achieve this, you have to create an instance of
the class <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/HyphenationAuto.html">HyphenationAuto</a>:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">// Hyphenating a British text
Chunk ckEN = new Chunk(textEN);
HyphenationAuto autoEN = new HyphenationAuto("en", "GB", 2, 2);
ckEN.setHyphenation(autoEN);
// Hyphenating a Dutch text
Chunk ckNL = new Chunk(textNL);
HyphenationAuto autoNL = new HyphenationAuto("nl", null, 2, 2);
ckNL.setHyphenation(autoNL);</pre>
In the example, the same text (the introduction of Charles Dickens's epic Tale of Two Cities)
is hyphenated once according to UK-hyphenation rules and once according to US-hyphenation rules.
As you can see, there is a slight difference.<br>
Important remark: you need the <a xmlns="" href="../../../downloads/itext-hyph-xml.jar">itext-hyph-xml.jar</a> in your CLASSPATH
if you want to use this functionality. In this jar, you'll find files like:
<ul xmlns="http://www.w3.org/1999/xhtml"><li>en_GB.xml</li><li>en_US.xml</li><li>fr.xml</li><li>nl.xml</li></ul>
These files contain all the hyphenation rules that are specific for a certain language
(GB english, US english, French, Dutch,...). As you can see the first two parameters of the
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/HyphenationAuto.html#HyphenationAuto(java.lang.String,%20java.lang.String,%20int,%20int)">HyphenationAuto constructor</a>
correspond with parts of the filename, the third and fourth parameter specify how
many characters may be 'orphaned' at the start of a word resp. at the end of a word.<br xmlns="http://www.w3.org/1999/xhtml">
Remark that the XML files were not created by the iText developers.
The XML files were created for Apache FOP.
We have downloaded them and we have put them in a separate jar
for your convenience. Some of them may be GPL or not usable
for commercial purposes so, read the licenses and decide what to keep.<br>
If you can't find the hyphenation pattern you are looking for,
you can create your own as described at the
<a href="http://xml.apache.org/fop/hyphenation.html">FOP</a> site.
Put your xml file as a resource in the package com.lowagie.text.pdf.hyphenation.hyph
or put the xml file in some directory and call <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/hyphenation/Hyphenator.html#setHyphenDir(java.lang.String)">Hyphenator.setHyphenDir()</a>.
</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's a lot more to be told on Chunk, see <a xmlns="" href="./../../objects/anchors/index.html#">Anchors and Actions</a>.</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:50
			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=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><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=JAVA&=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>