Sophie

Sophie

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

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: Tables</title><meta name="Description" content="How to organize text and data in tables with iText"><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>Tables</h2><div id="content"><div id="sidebar"><a class="toc" href="./../../index.html#objects_tables">
							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">My First PdfPTable</a></li><li><a href="#tablewidths">Widths, alignment and spacing</a></li><li><a href="#addingcells">Adding PdfPCells</a></li><li><a href="#styles">Cellheights, -alignment, -padding, -leading, -colors</a></li><li><a href="#nextpage">Splitting tables over several pages</a></li><li><a href="#continued">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/tables/MyFirstTable.java">MyFirstTable</a><br><div class="description">Use a PdfPTable to add a table to a PDF document</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/MyFirstTable.pdf">MyFirstTable.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableWidthAlignment.java">TableWidthAlignment</a><br><div class="description">Changing the width and the alignment of the complete table</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableWidthAlignment.pdf">TableWidthAlignment.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableSpacing.java">TableSpacing</a><br><div class="description">Defining the spacing between the table and other content</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableSpacing.pdf">TableSpacing.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellWidths.java">CellWidths</a><br><div class="description">Changing the widths of columns</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellWidths.pdf">CellWidths.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/DefaultCell.java">DefaultCell</a><br><div class="description">Using getDefaultCell to change the default style</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/DefaultCell.pdf">DefaultCell.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/NestedTables.java">NestedTables</a><br><div class="description">Using nested tables</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/NestedTables.pdf">NestedTables.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/ImageCell.java">ImageCell</a><br><div class="description">Adding an Image to a table</div><div class="small">Input:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/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/ImageCell.pdf">ImageCell.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellHeights.java">CellHeights</a><br><div class="description">Playing with heights of PdfPCells</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellHeights.pdf">CellHeights.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellAlignment.java">CellAlignment</a><br><div class="description">Changing the alignment of the contents of a PdfPCell</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/Alignment.pdf">Alignment.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellPaddingLeading.java">CellPaddingLeading</a><br><div class="description">Changing the padding and the leading of the contents of a PdfPCell</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/PaddingLeading.pdf">PaddingLeading.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellColors.java">CellColors</a><br><div class="description">Using some Rectangle methods on PdfPCell</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellColors.pdf">CellColors.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableBorders.java">TableBorders</a><br><div class="description">Demonstrates different borderstyles</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableBorders.pdf">TableBorders.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/AddBigTable.java">AddBigTable</a><br><div class="description">A very big table added with document.add()</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/AddBigTable.pdf">AddBigTable.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/SplitRows.java">SplitRows</a><br><div class="description">Add a table to a PDF with document.add() and if the cell doesn't fit, the row is split in two parts</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/SplitRowsBetween.pdf">SplitRowsBetween.pdf</a></li><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/SplitRowsWithin.pdf">SplitRowsWithin.pdf</a></li><li><a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/OmitRows.pdf">OmitRows.pdf</a></li></ul></div><div class="example"><div class="small">ANT script (all examples):</div><ul><li><a href="./../../objects/tables/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="intro"></a><div class="title">My First PdfPTable:</div><div xmlns="http://www.w3.org/1999/xhtml">
If you want to use iText to make bills, invoices, list, reports, etc...
you will probably want to present your data in a tabular form.
That's why the objects <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html">PdfPTable</a>
and <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html">PdfPCell</a> were created.<br xmlns="http://www.w3.org/1999/xhtml">
Both classes are very easy to use: just construct a table with a certain number of columns
and add cells to it:
<pre class="commandline">PdfPTable table = new PdfPTable(3);
PdfPCell cell =
  new PdfPCell(new Paragraph("header with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
document.add(table);</pre>
The result is the PDF equivalent of the HTML table:
<pre class="commandline">&lt;table border="1" width="80%" cellpadding="2"&gt;
  &lt;tr&gt;
    &lt;td colspan="3"&gt;header with colspan 3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;1.1&lt;/td&gt;
    &lt;td&gt;1.2&lt;/td&gt;
    &lt;td&gt;1.3&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;2.1&lt;/td&gt;
    &lt;td&gt;2.2&lt;/td&gt;
    &lt;td&gt;2.3&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;</pre>
<table border="1" width="300" cellpadding="2">
<tr><td colspan="3">header with colspan 3</td></tr>
<tr><td>1.1</td><td>1.2</td><td>1.3</td></tr>
<tr><td>2.1</td><td>2.2</td><td>2.3</td></tr>
</table>
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/MyFirstTable.java">
						com.lowagie.examples.objects.tables.MyFirstTable</a><br>Use a PdfPTable to add a table to a PDF document: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/MyFirstTable.pdf">MyFirstTable.pdf</a><br></div>
PdfPTable is a very powerful and flexible object, but for some specific needs, you can also use
one of the <a href="./../../objects/tables/alternatives/index.html#">alternatives for PdfPTable</a>.
If you have a Swing application with JTables, you can look at the
<a href="./../../objects/tables/alternatives/index.html#jtable">JTable2Pdf</a> section.
PdfPTable only works for generating PDF. If you need to generate HTML or RTF, you need the (no longer supported)
<a href="./../../objects/tables/alternatives/index.html#table">Table</a> object.
</div><a class="top" href="#top">Go to top of the page</a><a name="tablewidths"></a><div class="title">Widths, alignment and spacing:</div><div xmlns="http://www.w3.org/1999/xhtml">
If you 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>,
the default width of the table is 80 percent of the available space and the table is aligned in the center.
You can change these defaults with
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setWidthPercentage(float)">setWidthPercentage</a>
and <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setHorizontalAlignment(int)">setHorizontalAlignment</a>.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableWidthAlignment.java">
						com.lowagie.examples.objects.tables.TableWidthAlignment</a><br>Changing the width and the alignment of the complete table: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableWidthAlignment.pdf">TableWidthAlignment.pdf</a><br></div>
We have defined the number of columns of the table and based on
the widthpercentage, iText calculated an absolute width.
The default absolute width of each cell is (absolute width table / number):
all cells have the same width. Of course you may want to change this.
You can do this in different ways:<ul xmlns="http://www.w3.org/1999/xhtml">
<li>Use another constructor: <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#PdfPTable(float[])">PdfPTable(float[] relativeWidths)</a>.
For instance, if you want two 'normal' columns and a third column that's double the size of a 'normal' column,
use the float-array <span xmlns="http://www.w3.org/1999/xhtml" class="commandline">{1f, 1f, 2f}</span> as relativewidths. iText will calculate the absolute width for you.</li>
<li>You can also use the method <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setWidths(float[])">setWidths</a>
to change this array after you constructed the PdfPTable (but be careful: the array needs to have the same size as the number of columns).
In the example the percentages are changed from (10%, 10%, 5%, 75%) to (20%, 20%, 10%, 50%) so the second table looks completely different.</li>
<li xmlns="http://www.w3.org/1999/xhtml">If you want to work with absolute widths for the columns. You have to let iText calculate a widthpercentage for the table.
In this case you should use: <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setWidthPercentage(float[],%20com.lowagie.text.Rectangle)">setWidthPercentage(float[] columnWidth, Rectangle pageSize)</a>.
As you can see in the example, you need to do some calculations first to get the right pagesize.</li>
<li xmlns="http://www.w3.org/1999/xhtml">It even easier to use absolute widths if you lock the width of the table to a 'total width'. You need the methods
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setTotalWidth(float)">setTotalWidth</a>
and 
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setLockedWidth(boolean)">setLockedWidth</a> for this.
In the example the relation between the different cells will remain 10%, 10%, 5%, 75%, so you'll have 2 columns with a width of 30pt, one with a width of 15pt and one that's 225pt wide.</li>
</ul>
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellWidths.java">
						com.lowagie.examples.objects.tables.CellWidths</a><br>Changing the widths of columns: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellWidths.pdf">CellWidths.pdf</a><br></div>
If you don't change the defaults, all tables will be glued to eachother or to the other content of the page unless you add the necessary newlines.
If you want to avoid this, you can also use the methods
<a 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>
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableSpacing.java">
						com.lowagie.examples.objects.tables.TableSpacing</a><br>Defining the spacing between the table and other content: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableSpacing.pdf">TableSpacing.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="addingcells"></a><div class="title">Adding PdfPCells:</div><div xmlns="http://www.w3.org/1999/xhtml">
The easiest way to add a new cell to a table is using the method
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#addCell(java.lang.String)">addCell(String text)</a>.
In our first example 'MyFirstTable', we used the method
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#addCell(com.lowagie.text.pdf.PdfPCell)">addCell(PdfPCell cell)</a>
because we wanted to set some style attributes for the new PdfPCell.
If we use <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#addCell(java.lang.String)">addCell(String text)</a>
a new PdfPCell is constructed internally with the string as content and
using the layout of the 'default cell'. We can change the style attributes of
this default cell with the help of the method
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#getDefaultCell()">getDefaultCell</a>.
This also works for <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#addCell(com.lowagie.text.Phrase)">addCell(Phrase phrase)</a>
which is very similar to <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#addCell(java.lang.String)">addCell(String text)</a>.
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">table.getDefaultCell().setGrayFill(0.8f);
table.getDefaultCell().setBorderColor(new Color(255, 0, 0));
table.getDefaultCell().setColspan(2);</pre>
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/DefaultCell.java">
						com.lowagie.examples.objects.tables.DefaultCell</a><br>Using getDefaultCell to change the default style: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/DefaultCell.pdf">DefaultCell.pdf</a><br></div>
<div xmlns="http://www.w3.org/1999/xhtml" class="subtitle">Nested Tables</div>
In previous examples we already used
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setColspan(int)">setColspan</a>
to change the colspan of a Cell. Unfortunately, due to the design of PdfPTable, setRowspan is not supported.
You have to use a workaround. Instead of spanning a cell over different rows, you will have to use nested tables
for the other cells. You can use
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#addCell(com.lowagie.text.pdf.PdfPTable)">addCell(PdfPTable table)</a>
to achieve this:
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/NestedTables.java">
						com.lowagie.examples.objects.tables.NestedTables</a><br>Using nested tables: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/NestedTables.pdf">NestedTables.pdf</a><br></div>
<div xmlns="http://www.w3.org/1999/xhtml" class="subtitle">Images in a PdfPCell</div>
Finally, there is one 'addCell' method left in PdfPTable:
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#addCell(com.lowagie.Image)">addCell(Image image)</a>.
You can use this to add an Image that will be scaled to fit in the Cell.
This is similar to adding a cell that was constructed with
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#PdfPCell(com.lowagie.text.Image)">new PdfPCell(Image image)</a> or
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#PdfPCell(com.lowagie.text.Image,%20boolean)">new PdfPCell(Image image, boolean fit)</a>,
but as you can see in the example, there are some small differences:
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/ImageCell.java">
						com.lowagie.examples.objects.tables.ImageCell</a><br>Adding an Image to a table: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/ImageCell.pdf">ImageCell.pdf</a><br>
						External resources for this example:
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/otsoe.jpg">otsoe.jpg</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="styles"></a><div class="title">Cellheights, -alignment, -padding, -leading, -colors:</div><div xmlns="http://www.w3.org/1999/xhtml">
The content of a cell is wrapped by default. You can change this default with
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setNoWrap(boolean)">setNoWrap(true)</a>,
but the result can look rather ugly, so be careful with this option.<br xmlns="http://www.w3.org/1999/xhtml">
If you are using the default (nowrap = false), the height of the Cell is adjusted dynamically.
Each cell in the same row has the height of the 'highest' cell.
iText calculates this height based on different parameters: the number of lines of the content, the leading, the padding,...
In some cases you don't want to give that kind of control to iText: you want a cell to have a fixed height.
This can be done with the method <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setFixedHeight(float)">setFixedHeight</a>.
Of course, if the content doesn't fit into this height, you will loose part of it.<br xmlns="http://www.w3.org/1999/xhtml">
Maybe you want to do the opposite: maybe you want a cell to have a minimum height, even if it hasn't that much content.
In that case, you'll have to use <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setMinimumHeight(float)">setMinimumHeight</a>.<br xmlns="http://www.w3.org/1999/xhtml">
Finally, there is another useful method <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setExtendLastRow(boolean)">setExtendLastRow</a>.
It extends the last row of a Table to the bottom of the page.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellHeights.java">
						com.lowagie.examples.objects.tables.CellHeights</a><br>Playing with heights of PdfPCells: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellHeights.pdf">CellHeights.pdf</a><br></div>
The following example demonstrates the self explaining methods
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setHorizontalAlignment(int)">setHorizontalAlignment</a> and
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setVerticalAlignment(int)">setVerticalAlignment</a>.
For the horizontal alignment, you can choose:
<ul xmlns="http://www.w3.org/1999/xhtml">
<li>Element.ALIGN_LEFT</li>
<li>Element.ALIGN_CENTER</li>
<li>Element.ALIGN_RIGHT</li>
<li>Element.ALIGN_JUSTIFIED</li>
</ul>
For the vertical alignment:
<ul>
<li>Element.ALIGN_TOP</li>
<li>Element.ALIGN_MIDDLE</li>
<li>Element.ALIGN_BOTTOM</li>
<li>Element.ALIGN_BASELINE</li>
</ul>
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellAlignment.java">
						com.lowagie.examples.objects.tables.CellAlignment</a><br>Changing the alignment of the contents of a PdfPCell: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/Alignment.pdf">Alignment.pdf</a><br></div>
The padding is the space between the content and the outer cellborder.
You can set the amount of this space with
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setPadding(float)">setPadding</a>,
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setPaddingTop(float)">setPaddingTop</a>,
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setPaddingRight(float)">setPaddingRight</a>,
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setPaddingLeft(float)">setPaddingLeft</a> and/or
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setPaddingBottom(float)">setPaddingBottom</a>.<br xmlns="http://www.w3.org/1999/xhtml">
The leading is the space between the lines of the content. This value can be set with
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setLeading(float,%20float)">setLeading(float,%20float)</a>.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellPaddingLeading.java">
						com.lowagie.examples.objects.tables.CellPaddingLeading</a><br>Changing the padding and the leading of the contents of a PdfPCell: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/PaddingLeading.pdf">PaddingLeading.pdf</a><br></div>
If you set the padding, the borderwidth isn't taken into account.
You can force iText to calculate the borderwidth into the padding with
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setUseBorderPadding(boolean)">setUseBorderPadding(true)</a>.
In the next example, some method to set colors and grayfill of borders and cells are demonstrated.
As you can see, the padding of the green cell doesn't take the width of the magenta border into account.
In the blue cell, the word 'blue' doesn't overlap with the cyan border.
All methods that were used to change the borders and their color, were methods of the class
<a href="http://itext.ugent.be/library/api/com/lowagie/text/Rectangle.html">Rectangle</a>.
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellColors.java">
						com.lowagie.examples.objects.tables.CellColors</a><br>Using some Rectangle methods on PdfPCell: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/CellColors.pdf">CellColors.pdf</a><br></div>
A last set of methods that influence the position of the content within a cell, are the methods
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setUseAscender(boolean)">setUseAscender</a> and
<a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPCell.html#setUseDescender(boolean)">setUseDescender</a>.
An example is the best way to demonstrate the difference:
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableBorders.java">
						com.lowagie.examples.objects.tables.TableBorders</a><br>Demonstrates different borderstyles: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/TableBorders.pdf">TableBorders.pdf</a><br></div>
In the example shown above, you also learn how to change the width and the color of cellborders.
</div><a class="top" href="#top">Go to top of the page</a><a name="nextpage"></a><div class="title">Splitting tables over several pages:</div><div xmlns="http://www.w3.org/1999/xhtml">
If you have really large tables, you can't expect them to fit on one page.
iText splits the table automatically between rows. If your table has a header that needs to be repeated on every page, you need
to tell the table how many rows the header counts. This can be done with the
method <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setHeaderRows(int)">setHeaderRows</a>.
In the example, there is one headerrow:
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/AddBigTable.java">
						com.lowagie.examples.objects.tables.AddBigTable</a><br>A very big table added with document.add(): see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/AddBigTable.pdf">AddBigTable.pdf</a><br></div>
In the previous example every row just took one line.
If you have rows that are much larger, you must know that tables are split between rows by default.
So if only part of a row fits on the current page, the complete row is passed to the next page
(see <a xmlns="http://www.w3.org/1999/xhtml" href="SplitRowsBetween.pdf">SplitRowsBetween.pdf</a>). Only if the row doesn't fit on 1 page
it is split in two.<br>You can change this by using
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setSplitLate(boolean)">setSplitLate(false)</a>.
In this case, the row is split immediately. As you can see in <a xmlns="http://www.w3.org/1999/xhtml" href="SplitRowsWithin.pdf">SplitRowsWithin.pdf</a>
the maximum size of each page is used.<br>
If you really don't want the rows to be split, you'll have to use
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPTable.html#setSplitRows(boolean)">setSplitRows(false)</a>,
but in that case, you will lose a lot of data: see <a xmlns="http://www.w3.org/1999/xhtml" href="OmitRows.pdf">OmitRows.pdf</a>.
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/SplitRows.java">
						com.lowagie.examples.objects.tables.SplitRows</a><br>Add a table to a PDF with document.add() and if the cell doesn't fit, the row is split in two parts: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/SplitRowsBetween.pdf">SplitRowsBetween.pdf</a> <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/SplitRowsWithin.pdf">SplitRowsWithin.pdf</a> <a href="http://itext.ugent.be/library/com/lowagie/examples/objects/tables/OmitRows.pdf">OmitRows.pdf</a><br></div>
</div><a class="top" href="#top">Go to top of the page</a><a name="continued"></a><div class="title">To be continued:</div><div xmlns="http://www.w3.org/1999/xhtml">
On this page, we have already seen a lot of functionality that makes
PdfPTable a flexible and very useful object. But if you really need powerful table functionality,
with hundreds of columns and/or thousands and thousands of rows, you need to continue reading about PdfPTable in the
<a xmlns="" href="./../../objects/tables/pdfptable/index.html#">next chapter</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: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>