Sophie

Sophie

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

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: PdfPTable</title><meta name="Description" content="Some extra PdfPTable functionality"><meta name="Keywords" content="PDF, JAVA, iText, PdfPTable, Table, 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>PdfPTable</h2><div id="content"><div id="sidebar"><a class="toc" href="./../../../index.html#objects_tables_pdfptable">
							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="#">Adding a PdfPTable at an absolute position</a></li><li><a href="#memory">Memory management for large tables</a></li><li><a href="#cellevents">Cell and Table Events</a></li><li><a href="#verticaltext">Vertical Text</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/tables/pdfptable/WriteSelectedRows.java">WriteSelectedRows</a><br><div class="description">Demonstrates the writeSelectedRows method</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/WriteSelectedRows.pdf">WriteSelectedRows.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/Tables.java">Tables</a><br><div class="description">Adds a table to an absolute position</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/tables.pdf">tables.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/SplitTable.java">SplitTable</a><br><div class="description">Demonstrates how to split a Table in two sections of columns</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/SplitTable.pdf">SplitTable.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/FragmentTable.java">FragmentTable</a><br><div class="description">Break one large Table up into different smaller tables with the same header</div><div class="small">Argument(s):</div><ul><li>50</li></ul><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/FragmentTable.pdf">FragmentTable.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/CellEvents.java">CellEvents</a><br><div class="description">Demonstrates what one can do with cell events</div><div class="small">Input:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/otsoe.jpg">otsoe.jpg</a></li></ul><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/CellEvents.pdf">CellEvents.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/TableEvents1.java">TableEvents1</a><br><div class="description">Demonstrates what one can do with table events</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/TableEvents1.pdf">TableEvents1.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/TableEvents2.java">TableEvents2</a><br><div class="description">Demonstrates what one can do with table events (cells with colspan &gt; 1)</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/TableEvents2.pdf">TableEvents2.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/FloatingBoxes.java">FloatingBoxes</a><br><div class="description">Demonstrates how to simulate cellspacing as in HTML</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/FloatingBoxes.pdf">FloatingBoxes.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/VerticalTextInCells.java">VerticalTextInCells</a><br><div class="description">Demonstrates how to add vertical text in a cell</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/VerticalText.pdf">VerticalText.pdf</a></li></ul></div><div class="example"><div class="small">ANT script (all examples):</div><ul><li><a href="./../../../objects/tables/pdfptable/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=6">
				Chapter 6:
			</a>Constructing tables</li></ul></p>
		</div><a class="top" href="#top">Go to top of the page</a><a name=""></a><div class="title">Adding a PdfPTable at an absolute position:</div><div xmlns="http://www.w3.org/1999/xhtml">
You can add a PdfPTable with <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/Document.html#add(com.lowagie.text.Element)">Document.add()</a>
as described in the <a href="./../../../objects/tables/index.html#">previous chapter</a>,
but you can also choose to put a PdfPTable at an absolute position with the method
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#writeSelectedRows(int,%20int,%20float,%20float,%20com.lowagie.text.pdf.PdfContentByte)">writeSelectedRows</a>.
The mechanisms to add objects at absolute positions will be described in
<a href="./../../../index.html#part4">Part IV</a> of the tutorial.
All you need to know for now is that you have to pass the following parameters to the method:
<ul xmlns="http://www.w3.org/1999/xhtml">
	<li>rowStart - the first row to be written, zero index</li>
	<li>rowEnd - the last row to be written + 1. If it is -1 all the rows to the end are written.</li>
	<li>xPos - the x write coodinate of the upper left corner of the table</li>
	<li>yPos - the y write coodinate of the upper left corner of the table (remark: the 0 is at the bottom of the page as explained in the <a xmlns="" href="./../../../general/faq/index.html#xycoordinates">FAQ</a>)</li>
	<li xmlns="http://www.w3.org/1999/xhtml">canvas - the PdfContentByte where the rows will be written to (explained in <a xmlns="" href="./../../../index.html#part4">Part IV</a> of the tutorial)</li>
</ul>
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/WriteSelectedRows.java">
						com.lowagie.examples.objects.tables.pdfptable.WriteSelectedRows</a><br>Demonstrates the writeSelectedRows method: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/WriteSelectedRows.pdf">WriteSelectedRows.pdf</a><br></div>
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/Tables.java">
						com.lowagie.examples.objects.tables.pdfptable.Tables</a><br>Adds a table to an absolute position: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/tables.pdf">tables.pdf</a><br></div>
As you can see in the example, there are some things you need to keep in mind
when using <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#writeSelectedRows(int,%20int,%20float,%20float,%20com.lowagie.text.pdf.PdfContentByte)">writeSelectedRows</a>.
If you add a PdfPTable with document.add(), the width of the table
is always calculated as a percentage of the available page width at the current pointer
(see page 2 of the PDF in the example). But if you add a PdfPTable at
an absolute position, iText can't know what you are up to with the rest
of the page. What is the available space?
That's something iText can't know, so you need to set a total width of the Table with the methods
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setTotalWidth(float)">setTotalWidth(float totalWidth)</a>
or <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setTotalWidth(float[])">setTotalWidth(float[] columnWidth)</a>.<br xmlns="http://www.w3.org/1999/xhtml">
You are also responsible for checking it the PdfPTable fits on the page.
On page 5, rows 100-150 don't fit on the page because we started writing the rows at y = 200 instead of y = 820.
Rows 112-149 are written, but you can't see them because they are outside the visible area of your page.<br>
As you can see in the example, you can use
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#writeSelectedRows(int,%20int,%20float,%20float,%20com.lowagie.text.pdf.PdfContentByte)">writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas)</a>
to split your table horizontally over different pages if you have too many rows to fit on one page.
If your table is too wide, you can also split the table vertically between columns.
You need the method
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#writeSelectedRows(int,%20int,%20int,%20int,%20float,%20float,%20com.lowagie.text.pdf.PdfContentByte)">writeSelectedRows(int colStart, int colEnd, int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas)</a>
to do this.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/SplitTable.java">
						com.lowagie.examples.objects.tables.pdfptable.SplitTable</a><br>Demonstrates how to split a Table in two sections of columns: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/SplitTable.pdf">SplitTable.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="memory"></a><div class="title">Memory management for large tables:</div><div xmlns="http://www.w3.org/1999/xhtml">
You have to be very careful with large tables.
When you add objects to a Document, these objects are written
to the outputstream 'as soon as possible'. The objects that have
been written are made eligible for destruction. However, when
you construct a PdfPTable and keep adding new cells without adding
the table to the document, the PdfPTable-object keeps on growing,
taking more and more memory that isn't being released.
For really large tables, this can become a serious problem.<br>
The next example shows how to split a table into different sections.
As the tables are glued to eachother (don't use <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setSpacingBefore(float)">setSpacingBefore</a>
and 
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setSpacingAfter(float)">setSpacingAfter</a> here),
nobody will see the difference between 1 large table or several small tables.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/FragmentTable.java">
						com.lowagie.examples.objects.tables.pdfptable.FragmentTable</a> 50<br>Break one large Table up into different smaller tables with the same header: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/FragmentTable.pdf">FragmentTable.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="cellevents"></a><div class="title">Cell and Table Events:</div><div xmlns="http://www.w3.org/1999/xhtml">
Remember the class <a xmlns="" href="./../../../objects/chunk/index.html#generic">Chunk</a>
and how you could add your own functionality in a generic way with a PageEvent?
You can do the same with a PdfPCell using a <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCellEvent.html">PdfPCellEvent</a>.
In the example, we want to strike a line OVER the contents from the lowerleft to the upperright corner.
Using methods that are explained in <a href="./../../../index.html#part4">Part IV</a> of the tutorial, we can apply almost every layout that is possible within the PDF syntax.
We just have to implement the method
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCellEvent.html#cellLayout(com.lowagie.text.pdf.PdfPCell,%20com.lowagie.text.Rectangle,%20com.lowagie.text.pdf.PdfContentByte[])">cellLayout</a>.
You can use the cell itself that is passed as a parameter and you get a Rectangle object that holds to absolute position of the cell on the page.
The array of PdfContentByte arrays needs some further explanation:
<ul xmlns="http://www.w3.org/1999/xhtml">
<li><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#BASECANVAS">PdfPtable.BASECANVAS</a> - the original PdfContentByte. Anything placed here will be under the table.</li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#BACKGROUNDCANVAS">PdfPtable.BACKGROUNDCANVAS</a> - the layer where the background goes to.</li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#LINECANVAS">PdfPtable.LINECANVAS</a> - the layer where the lines go to.</li>
<li xmlns="http://www.w3.org/1999/xhtml"><a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#TEXTCANVAS">PdfPtable.TEXTCANVAS</a> - the layer where the text go to. Anything placed here will be over the table.</li>
</ul>
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/CellEvents.java">
						com.lowagie.examples.objects.tables.pdfptable.CellEvents</a><br>Demonstrates what one can do with cell events: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/CellEvents.pdf">CellEvents.pdf</a><br>
						External resources for this example:
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/otsoe.jpg">otsoe.jpg</a><br></div>
If you want the same kind of control over a complete table, it gets a little bit more complicated.
You need to implement the method <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTableEvent.html#tableLayout(com.lowagie.text.pdf.PdfPTable,%20float[][],%20float[],%20int,%20int,%20com.lowagie.text.pdf.PdfContentByte[])">tableLayout</a> of
the <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTableEvent.html">PdfPTableEvent</a> interface.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/TableEvents1.java">
						com.lowagie.examples.objects.tables.pdfptable.TableEvents1</a><br>Demonstrates what one can do with table events: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/TableEvents1.pdf">TableEvents1.pdf</a><br></div>
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/TableEvents2.java">
						com.lowagie.examples.objects.tables.pdfptable.TableEvents2</a><br>Demonstrates what one can do with table events (cells with colspan &gt; 1): see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/TableEvents2.pdf">TableEvents2.pdf</a><br></div>
In the next example cell and table events are combined to simulate cellspacing.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/FloatingBoxes.java">
						com.lowagie.examples.objects.tables.pdfptable.FloatingBoxes</a><br>Demonstrates how to simulate cellspacing as in HTML: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/FloatingBoxes.pdf">FloatingBoxes.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="verticaltext"></a><div class="title">Vertical Text:</div><div xmlns="http://www.w3.org/1999/xhtml">
Finally the answer to a question that pops up on the mailinglist from time to time:
is it possible to put vertical text in a Cell. The answer is: yes, but you need to know
how to use the class <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfTemplate.html">PdfTemplate</a>
(which is explained in the <a href="./../../../directcontent/index.html#templates">direct content</a> of the tutorial).
If you can't wait, just take a look at the following example.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/VerticalTextInCells.java">
						com.lowagie.examples.objects.tables.pdfptable.VerticalTextInCells</a><br>Demonstrates how to add vertical text in a cell: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/pdfptable/VerticalText.pdf">VerticalText.pdf</a><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=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>