Sophie

Sophie

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

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: ColumnText</title><meta name="Description" content="How to organize text and data in columns with iText"><meta name="Keywords" content="PDF, JAVA, iText, examples, columns, ColumnText, MultiColumnText, 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>ColumnText</h2><div id="content"><div id="sidebar"><a class="toc" href="./../../index.html#objects_columns">
							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="#introduction">Columns</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/columns/Column.java">Column</a><br><div class="description">Defining a simple single object ColumnText object</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/column.pdf">column.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/ColumnSimple.java">ColumnSimple</a><br><div class="description">Adding phrases to a simple ColumnText object</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/columnsimple.pdf">columnsimple.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/ColumnObjects.java">ColumnObjects</a><br><div class="description">Adding different objects to a simple ColumnText object</div><div class="small">Input:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/cover.png">cover.png</a></li></ul><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/columnobjects.pdf">columnobjects.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/ColumnIrregular.java">ColumnIrregular</a><br><div class="description">Defining an irregular ColumnText object</div><div class="small">Input:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/caesar_coin.jpg">caesar_coin.jpg</a></li></ul><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/columnirregular.pdf">columnirregular.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/MultiColumnSimple.java">MultiColumnSimple</a><br><div class="description">Defining a simple MultiColumnText object</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/multicolumnsimple.pdf">multicolumnsimple.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/MultiColumnR2L.java">MultiColumnR2L</a><br><div class="description">Defining a MultiColumnText object that adds its columns from right to left</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/multicolumnR2L.pdf">multicolumnR2L.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/MultiColumnIrregular.java">MultiColumnIrregular</a><br><div class="description">Defining an irregular MultiColumnText object</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/multicolumnirregular.pdf">multicolumnirregular.pdf</a></li></ul></div><div class="example"><div class="small">ANT script (all examples):</div><ul><li><a href="./../../objects/columns/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=7">
				Chapter 7:
			</a>Constructing columns</li></ul></p>
		</div><a class="top" href="#top">Go to top of the page</a><a name="introduction"></a><div class="title">Columns:</div><div xmlns="http://www.w3.org/1999/xhtml">If you want to use iText to make product sheets, articles, newspaper pages, etc...
you will probably want to present your data in columns.
That's why the objects <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html">ColumnText</a>
and <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/MultiColumnText.html">MultiColumnText</a> were created.<br xmlns="http://www.w3.org/1999/xhtml">
With these objects you can add content within a given rectangle (or a userdefined irregular shape)
and this content will be wrapped automatically when the right limit of the rectangle is reached
(or it will be formatted to fit the irregular shape). All content that doesn't fit
isn't shown, but it remains in the column object so that you can display it in
a next column.<br><br>
<div class="subtitle">ColumnText</div>
To show a certain phrase, centered in a rectangle between the coordinates (100, 300) and (200, 500), we write some code like this:
<pre class="commandline">PdfContentByte cb = writer.getDirectContent();
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(phrase,
  100, 300, 200, 500, 15,
  Element.ALIGN_CENTER);
ct.go();</pre>
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#setSimpleColumn(com.lowagie.text.Phrase,%20float,%20float,%20float,%20float,%20float,%20int)">setSimpleColumn(com.lowagie.text.Phrase, float, float, float, float, float, int)</a>.
This specific method only works with content in a
<a href="./../../objects/index.html#phrase">Phrase</a>,
but there are lots of methods in class ColumnText that allow you to specify
indentation etc...
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/Column.java">
						com.lowagie.examples.objects.columns.Column</a><br>Defining a simple single object ColumnText object: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/column.pdf">column.pdf</a><br></div>
It isn't necessary to add the text all at once in one Phrase object.
You can first define the rectangle with
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#setSimpleColumn(float,%20float,%20float,%20float,%20float)">setSimpleColumn(float, float, float, float, float)</a> or
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#setSimpleColumn(float,%20float,%20float,%20float,%20float,%20int)">setSimpleColumn(float, float, float, float, float, int)</a>,
then add some text with
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#addText(com.lowagie.text.Chunk)">addText(com.lowagie.text.Chunk)</a> or
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#addText(com.lowagie.text.Phrase)">addText(com.lowagie.text.Phrase)</a>
and show the column with the <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#go()">go()</a>-method:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">PdfContentByte cb = writer.getDirectContent();
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(60, 300, 100, 500, 15, Element.ALIGN_CENTER);
ct.addText(phrase1);
ct.addText(phrase2);
ct.addText(chunk);
ct.go();</pre>
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/ColumnSimple.java">
						com.lowagie.examples.objects.columns.ColumnSimple</a><br>Adding phrases to a simple ColumnText object: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/columnsimple.pdf">columnsimple.pdf</a><br></div>
Of course, if there is more text than space in the rectangle,
we don't want to loose the text that didn't fit. Maybe we want
this text to be shown in another column. That's why we are going
to take a look at the return value of the <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#go()">go()</a>-method.
If this return value has the flag '<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#NO_MORE_COLUMN">NO_MORE_COLUMN</a>' turned
on, there wasn't enough room for the text in the column. If all the text was shown,
the flag '<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#NO_MORE_TEXT">NO_MORE_TEXT</a>' is on.<br xmlns="http://www.w3.org/1999/xhtml">
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#go()">go()</a> causes (partial or complete) content to be written
to the document. If you first want to check if the content will fit the rectangle you defined, use
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#go(boolean)">go(true)</a>
instead of <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#go()">go()</a>.
This will simulate adding content. Check if NO_MORE_TEXT or NO_MORE_COLUMN is returned and
decide if you actually want to add the column or if you want to change its size first.<br xmlns="http://www.w3.org/1999/xhtml">
The next example gives you a column to which all kinds of objects were added with method
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#addElement(com.lowagie.text.Element)">addElement(com.lowagie.text.Element)</a>.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/ColumnObjects.java">
						com.lowagie.examples.objects.columns.ColumnObjects</a><br>Adding different objects to a simple ColumnText object: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/columnobjects.pdf">columnobjects.pdf</a><br>
						External resources for this example:
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/cover.png">cover.png</a><br></div>
ColumnText can also be used to fit content in irregular, user-defined shapes.
Use method <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#setColumns(float[],%20float[])">setColumns(float[], float[])</a>
to define a left and right boundary for the text:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">float[] left  = {70,790, 70,60};
float[] right = {300,790, 300,700, 240,700, 240,590, 300,590, 300,106, 270,60};
ct.setColumns(left, right);</pre>
The left border is a straight line (x = 70), but the right border is irregular.
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/ColumnIrregular.java">
						com.lowagie.examples.objects.columns.ColumnIrregular</a><br>Defining an irregular ColumnText object: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/columnirregular.pdf">columnirregular.pdf</a><br>
						External resources for this example:
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/caesar_coin.jpg">caesar_coin.jpg</a><br></div>
<div xmlns="http://www.w3.org/1999/xhtml" class="subtitle">MultiColumnText</div>
If you want to add multiple columns automatically, you can use class
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/MultiColumnText.html">MultiColumnText</a>.
It's a wrapper for <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html">ColumnText</a>
that does a lot of work in your place. With
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/MultiColumnText.html#addRegularColumns(float,%20float,%20float,%20int)">addRegularColumns(left, right, gutterWidth, numColumns)</a>,
you define that a page should have <span xmlns="http://www.w3.org/1999/xhtml" class="commandline">numColumns</span> columns, with <span class="commandline">gutterWidth</span> space between them.
The first column has <span class="commandline">left</span> space to the left; the last column <span class="commandline">right</span> space to the right.
If you want iText to take care of the distribution of columns over multiple pages, you create the object like this:
<pre class="commandline">MultiColumnText mct = new MultiColumnText();</pre>
Otherwise, you specify the height of the column and a Y position; and you will have to check if all the text was displayed.
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/MultiColumnSimple.java">
						com.lowagie.examples.objects.columns.MultiColumnSimple</a><br>Defining a simple MultiColumnText object: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/multicolumnsimple.pdf">multicolumnsimple.pdf</a><br></div>
If, for some reason, you want to add the columns from right to left, take a look at this example:
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/MultiColumnR2L.java">
						com.lowagie.examples.objects.columns.MultiColumnR2L</a><br>Defining a MultiColumnText object that adds its columns from right to left: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/multicolumnR2L.pdf">multicolumnR2L.pdf</a><br></div>
MultiColumnText can also be used for irregular columns.
Just add one or more columns with
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/MultiColumnText.html#addColumn(float[],%20float[])">addColumn(float[],%20float[])</a>,
the way you would do it with
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/ColumnText.html#setColumns(float[],%20float[])">ColumnText.setColumns(float[], float[])</a>.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/MultiColumnIrregular.java">
						com.lowagie.examples.objects.columns.MultiColumnIrregular</a><br>Defining an irregular MultiColumnText object: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/columns/multicolumnirregular.pdf">multicolumnirregular.pdf</a><br></div>
You see that we used another constructor for this last example.
We could have use automatic distribution over different pages (and add the diamond shape
using a PageEvent), but instead we created the MultiColumnText object like this:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">MultiColumnText mct = new MultiColumnText(document.top() - document.bottom());</pre>
The parameter defines a maximum height for the column. If you do this, the columns will not
automatically be distributed over different pages! You will need to use a loop:
<pre class="commandline">do {
    document.add(mct);
    mct.nextColumn();
    document.newPage();
} while (mct.isOverflow());</pre>
</div><a class="top" href="#top">Go to top of the page</a><div id="footer">
			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=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>