<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>No pain no gain &#187; Java</title>
	<atom:link href="http://www.dikant.de/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dikant.de</link>
	<description>Personal blog of Peter Dikant</description>
	<lastBuildDate>Sun, 23 May 2010 20:57:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Layout PDF documents in Java with iText</title>
		<link>http://www.dikant.de/2007/10/15/layout-pdf-documents-in-java-with-itext/</link>
		<comments>http://www.dikant.de/2007/10/15/layout-pdf-documents-in-java-with-itext/#comments</comments>
		<pubDate>Mon, 15 Oct 2007 13:40:53 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[iText]]></category>

		<guid isPermaLink="false">http://www.dikant.de/2007/10/15/layout-pdf-documents-in-java-with-itext/</guid>
		<description><![CDATA[iText is a free Java library for generating PDF documents inside your own applications. The library is easy to use and can produce great results with only little programming. The basic usage is pretty simple. Let&#8217;s take a look at &#8230; <a href="http://www.dikant.de/2007/10/15/layout-pdf-documents-in-java-with-itext/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lowagie.com/iText/">iText</a> is a free Java library for generating PDF documents inside your own applications. The library is easy to use and can produce great results with only little programming. The basic usage is pretty simple. Let&#8217;s take a look at a small &#8220;Hello World&#8221; application:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Document</span> document <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Document</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    PdfWriter.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span>document, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;output.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Paragraph<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
document.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Creating a new document object will initialize a new PDF document with A4 dimensions. The <code>PdfWriter</code> will be used to write the resulting document to an <code>OutputStream</code>, which is in the above example a file. After opening the document you can just add new paragraphs to the document.</p>
<p><span id="more-38"></span></p>
<p>Using this document based approach allows quick results with limited control over the layout of the resulting PDF document. To have a better control of the document layout, you need to use the <code>PdfContentByte</code> object to modify the document. This object allows us to position text and graphics with absolute coordinates in the document. Please note that the PDF coordinate system has the origin in the lower left corner of the page. The x-axis is oriented to the left and the y-axis to the top. The coordinates are measured in points. 1 inch is divided into 72 points so that 1 Millimeter equals 2.8346 points. This means that you can position elements on the page with an accuracy of approximately 1/3 Millimeters. </p>
<p>Let&#8217;s create a new document and initialize the <code>PdfContentByte</code> object:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Document</span> document <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Document</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    PdfWriter writer <span style="color: #339933;">=</span> PdfWriter.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span>document, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;output.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    PdfContentByte cb <span style="color: #339933;">=</span> writer.<span style="color: #006633;">getDirectContent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// now we can place content elements on the page</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
document.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Once the <code>PdfContentByte</code> object is initialized, we can use the handle to that object to print text or draw directly on the page. All coordinates are relative to the lower left corner of the page. Let&#8217;s write a headline which is centered at the top of the page with a line below it which spawn from the left side to the right side of the page:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// we need to switch to text mode</span>
cb.<span style="color: #006633;">beginText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// write text centered at 155 mm from left side and 270 mm from bottom</span>
cb.<span style="color: #006633;">showTextAligned</span><span style="color: #009900;">&#40;</span>PdfContentByte.<span style="color: #006633;">ALIGN_CENTER</span>, <span style="color: #0000ff;">&quot;Our headline&quot;</span>, <span style="color: #cc66cc;">439</span>, <span style="color: #cc66cc;">765</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// leave text mode</span>
cb.<span style="color: #006633;">endText</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// now draw a line below the headline</span>
cb.<span style="color: #006633;">setLineWidth</span><span style="color: #009900;">&#40;</span>1f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
cb.<span style="color: #006633;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">755</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
cb.<span style="color: #006633;">lineTo</span><span style="color: #009900;">&#40;</span>595. <span style="color: #cc66cc;">755</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
cb.<span style="color: #006633;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It is already possible to create some complex documents with the above methods but when dealing with longer text passages the method <code>showTextAligned</code> has its drawbacks. When dealing with longer text passages, iText has the capability to work with so called columns. A simple column is a rectangular area on the page where a given text is rendered into. The text will be wrapped automatically and if it does not fit into a single column it can continue in another column. This concept of text columns is also widely used in desktop publishing applications. Let&#8217;s take a look at how we can use text columns to place text in the document:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// first define a standard font for our text</span>
<span style="color: #003399;">Font</span> helvetica8BoldBlue <span style="color: #339933;">=</span> FontFactory.<span style="color: #006633;">getFont</span><span style="color: #009900;">&#40;</span>FontFactory.<span style="color: #006633;">HELVETICA</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #003399;">Font</span>.<span style="color: #006633;">BOLD</span>, <span style="color: #003399;">Color</span>.<span style="color: #006633;">blue</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// create a column object</span>
ColumnText ct <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ColumnText<span style="color: #009900;">&#40;</span>cb<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// define the text to print in the column</span>
Phrase myText <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Phrase<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet, ...&quot;</span>, helvetica8BoldBlue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ct.<span style="color: #006633;">setSimpleColumn</span><span style="color: #009900;">&#40;</span>myText, <span style="color: #cc66cc;">72</span>, <span style="color: #cc66cc;">600</span>, <span style="color: #cc66cc;">355</span>, <span style="color: #cc66cc;">317</span>, <span style="color: #cc66cc;">10</span>, <span style="color: #003399;">Element</span>.<span style="color: #006633;">ALIGN_LEFT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ct.<span style="color: #006633;">go</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The above example will create a 10cm x 10cm text box with the sample text printed in a blue 8 point large Helvetica font left aligned.</p>
<p>Please note that when you define additional columns with the <code>setSimpleColumn()</code> method, text which does not fit into the first column will continue in the next column. Although this will be a desired behavior most of the time, sometimes you might want to avoid this. In that case you need to create a new <code>ColumnText</code> object before calling <code>setSimpleColumn()</code>.</p>
<p>This should get you going with complex layouts in iText. Please consult the official <a href="http://itextdocs.lowagie.com/tutorial/">iText documentation</a> for further information about the iText API.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dikant.de/2007/10/15/layout-pdf-documents-in-java-with-itext/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
