Tutorial: iText by Example

Generating HTML with iText

UNDER CONSTRUCTION
Why I wrote HtmlWriter.:
iText wasn't always called iText. The first name of the library was 'rugPdf'. RUG was the abbreviation for Rijksuniversiteit Gent (my employer since november 1998). Writing iText was one of my first tasks at the University. The new abbreviation is UGent because the 'Rijks' was droppend and Ghent University is now called 'Universiteit Gent' in Dutch (or rather: in Flemish, because Ghent is a Belgian City).
The very first versions of this rugPdf library were very low level and meant to generate PDF only: you had to know a lot about the PDF syntax to be able to use it. In order to make PDF generation more transparant (and to avoid that our developers should all be PDF experts), I rewrote rugPdf completely after our initial product were successfully brought into production. I created a plethora of high level objects, such as com.lowagie.text.Document, com.lowagie.text.Chunk, com.lowagie.text.Phrase, com.lowagie.text.Paragraph, com.lowagie.text.List,...
These objects would be translated to PDF behind the screens: developers would no longer need to know anything about PDF syntax. However: for me it was very hard to debug the source code. While testing, lots of broken PDF files were generated and it wasn't always possible to retrieve the exact location where everything went wrong: just try reading half of a generated PDF file in a plain text editor and you will know why ;-)
So I had the idea to have two Writers listening to one document at the same time: com.lowagie.text.pdf.PdfWriter and com.lowagie.text.html.HtmlWriter. As the HTML format is less critical than PDF syntax, it was sufficient for me to take one look at the broken HTML file to see what high level object caused the Runtime error.

The moral of this long introduction is: HtmlWriter is a class that produces HTML, but it was only written for debugging reasons. If you are looking for a library that generates HTML, you will find other products with far more features and possibilities than provided with iText. If you want to generate documents in different formats simultaneously, than you might find HtmlWriter useful.
Go to top of the page
The Hello World example for HTML:
We take the first example we made in PDF and RTF and now we let the document listen to com.lowagie.text.html.HtmlWriter:
Example: java com.lowagie.examples.html.HelloHtml
Generates a simple 'Hello World' HTML file: see HelloWorld.html
Go to top of the page
Metadata in HTML:
As described in the section on adding metadata before opening a document, there are specific methods to add the title, author, subject and keywords of the document contents. These methods will add the specific META-tags to your HTML. The methods to add the creator, producer and creationdate, will add comment sections to the HTML-header. You can use addHeader(java.lang.String,%20java.lang.String) to add other META tags (such as 'Expires').
// standard meta information
document.addTitle("Hello World example");
document.addAuthor("Bruno Lowagie");
document.addSubject("This example explains step 3 in Chapter 1");
document.addKeywords("Metadata, iText, step 3, tutorial");
// custom (HTML) meta information
document.addHeader("Expires", "0");
// meta information that will be in a comment section in HTML
document.addCreator("My program using iText");
This is what the HTML header will look like:
<head>
	<title>
		Hello World example
	</title>
	<meta name="author" content="Bruno Lowagie" />
	<meta name="subject" content="This example explains step 3 in Chapter 1" />
	<meta name="keywords" content="Metadata, iText, step 3, tutorial" />
	<meta name="Expires" content="0" />
	<!-- Creator: My program using iText -->
	<!-- Producer: iTextXML by lowagie.com -->
	<!-- CreationDate: Fri Dec 17 14:13:18 CET 2004 -->
</head>
Example: java com.lowagie.examples.html.HelloWorldMeta
Generates an HTML file with metadata: see HelloWorldMeta.html
Go to top of the page



Amazon books:
amazon.co.uk-link

amazon.co.uk-link