Sophie

Sophie

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

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: Graphics2D</title><meta name="Description" content="If you don't want to read all the chapters on PDF syntax, just use the JAVA API"><meta name="Keywords" content="PDF, JAVA, iText, examples, PdfContentByte, Graphics, absolute positions, 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>Graphics2D</h2><div id="content"><div id="sidebar"><a class="toc" href="./../../index.html#directcontent_graphics2D">
							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="#graphics2D">The java.awt.Graphics2D object</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/directcontent/graphics2D/G2D.java">G2D</a><br><div class="description">A Simple Graphics2D example</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/graphics2D.pdf">graphics2D.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/ArabicText.java">ArabicText</a><br><div class="description">Drawing arabic text using the Graphics2D object</div><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/arabictext.pdf">arabictext.pdf</a></li></ul></div><div class="example"><a class="source" href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/JFreeChartExample.java">JFreeChartExample</a><br><div class="description">Some JFreeChart examples</div><div class="small">Extra jars needed:</div><ul><li>jfreechart.jar</li><li>jcommon.jar</li></ul><div class="small">Output:</div><ul><li><a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/barchart.pdf">barchart.pdf</a></li><li><a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/piechart.pdf">piechart.pdf</a></li><li><a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/xychart.pdf">xychart.pdf</a></li></ul></div><div class="example"><div class="small">ANT script (all examples):</div><ul><li><a href="./../../directcontent/graphics2D/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=12">
				Chapter 12:
			</a>Drawing to Java Graphics2D</li></ul></p>
		</div><a class="top" href="#top">Go to top of the page</a><a name="graphics2D"></a><div class="title">The java.awt.Graphics2D object:</div><div xmlns="http://www.w3.org/1999/xhtml">
Maybe you really don't want to learn to use PDF Syntax, maybe you really don't
want to learn a complete new API such as iText, maybe you just want to stick to
what you know, in casu, the JAVA API of the JSDK. No problem. In that case, we
have a class that exactly meets your needs:
<a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfGraphics2D.html">PdfGraphics2D</a>.
This class extends java.awt.Graphics2D and overrides its methods so that they
produce PDF syntax. Isn't that nice?
(Before you ask: there's also a <a href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfPrinterGraphics2D.html">PdfGraphics2D</a> object
extending java.awt.print.PrinterGraphics.)<br xmlns="http://www.w3.org/1999/xhtml">
There are several <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfContentByte.html#createGraphics(float,%20float)">createGraphics</a> methods
in class PdfContentByte. In the example below, I copied the code of an example in SUN's Graphics2D tutorial literally.
The Graphics2D object I used, was retrieved from a PdfTemplate:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline"> PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(w, h);
Graphics2D g2 = tp.createGraphics(w, h, new DefaultFontMapper());</pre>
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/G2D.java">
						com.lowagie.examples.directcontent.graphics2D.G2D</a><br>A Simple Graphics2D example: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/graphics2D.pdf">graphics2D.pdf</a><br></div>
So if you already know all about Graphics2D in JAVA or if you have programs writing stuff
to a Graphics2D object (for instance to a JPanel in some JFrame), you don't need to learn
all about the PDF syntax, you can use your existing code to write your output to PDF!
For instance, if you are familiar with java.awt.Font, but not that familiar with the Fonts in iText/PDF,
you could use PdfGraphics2D as is done in the next example:
<div id="example">
					Example: java
					<a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/ArabicText.java">
						com.lowagie.examples.directcontent.graphics2D.ArabicText</a><br>Drawing arabic text using the Graphics2D object: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/arabictext.pdf">arabictext.pdf</a><br></div>
The only syntax you need to know is this:
<pre xmlns="http://www.w3.org/1999/xhtml" class="commandline">java.awt.Font font = new java.awt.Font("arial", 0, 18);
PdfContentByte cb = writer.getDirectContent();
java.awt.Graphics2D g2 =
    cb.createGraphicsShapes(PageSize.A4.width(), PageSize.A4.height());
g2.setFont(font);
g2.drawString(text, 100, 100);
g2.dispose();</pre>
DO NOT FORGET to call <a xmlns="" href="http://itext.ugent.be/library/api/com/lowagie/text/pdf/PdfGraphics2D.html#dispose()">PdfGraphics2D.dispose()</a>!!!<br xmlns="http://www.w3.org/1999/xhtml"><br>
A good example of how iText can be plugged in into another library that makes extensive use
of Graphics2D can be found at <a href="http://www.jfree.org/jfreechart/">JFree.org</a>.
JFree.org is the home of JFreeChart, one of the most used libraries for creating Charts.
Charts are written to a java.awt.Graphics2D objects, so if you want to have those charts
in PDF, just plug in iText:
<div id="example">
					Example: java
					<a xmlns="" href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/JFreeChartExample.java">
						com.lowagie.examples.directcontent.graphics2D.JFreeChartExample</a><br>Some JFreeChart examples: see
						 <a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/barchart.pdf">barchart.pdf</a> <a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/piechart.pdf">piechart.pdf</a> <a href="http://itext.ugent.be/library/com/lowagie/examples/directcontent/graphics2D/xychart.pdf">xychart.pdf</a><br>
						Extra jars needed in your CLASSPATH:
						 jfreechart.jar jcommon.jar<br></div>
If you have an Free/Open Source Software application that uses iText in this context,
please send me an example, so I can add it to this chapter.
</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:51
			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></div></body></html>