Itext
Itext
Create a new Java project "[Link]" with the package "[Link]". Create a folder "lib" and put the iText library (jar file) into this folder. Add the jar to your classpath.
Create the following class "[Link]" . I assume that the coding is pretty much self-explaining. I tried to add lots of comments to make it easier to understand. For more complex examples have a look at the iText Homepage .
package [Link];
import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link];
public class FirstPdf { private static String FILE = "c:/temp/[Link]"; private static Font catFont = new Font([Link].TIMES_ROMAN, 18, [Link]); private static Font redFont = new Font([Link].TIMES_ROMAN, 12,
[Link], [Link]); private static Font subFont = new Font([Link].TIMES_ROMAN, 16, [Link]); private static Font smallBold = new Font([Link].TIMES_ROMAN, 12, [Link]);
public static void main(String[] args) { try { Document document = new Document(); [Link](document, new FileOutputStream(FILE)); [Link](); addMetaData(document); addTitlePage(document); addContent(document); [Link](); } catch (Exception e) { [Link](); } }
// iText allows to add metadata to the PDF which can be viewed in your Adobe // Reader // under File -> Properties
private static void addMetaData(Document document) { [Link]("My first PDF"); [Link]("Using iText"); [Link]("Java, PDF, iText"); [Link]("Lars Vogel"); [Link]("Lars Vogel"); }
private static void addTitlePage(Document document) throws DocumentException { Paragraph preface = new Paragraph();
addEmptyLine(preface, 1);
addEmptyLine(preface, 8);
[Link](new Paragraph( "This document is a preliminary version and not subject to your license agreement or any other agreement with [Link] ;-).", redFont));
[Link](preface);
private static void addContent(Document document) throws DocumentException { Anchor anchor = new Anchor("First Chapter", catFont); [Link]("First Chapter");
Paragraph subPara = new Paragraph("Subcategory 1", subFont); Section subCatPart = [Link](subPara); [Link](new Paragraph("Hello"));
subPara = new Paragraph("Subcategory 2", subFont); subCatPart = [Link](subPara); [Link](new Paragraph("Paragraph 1")); [Link](new Paragraph("Paragraph 2")); [Link](new Paragraph("Paragraph 3"));
// Add a list
createList(subCatPart); Paragraph paragraph = new Paragraph(); addEmptyLine(paragraph, 5); [Link](paragraph);
// Add a table
createTable(subCatPart);
// Next section
anchor = new Anchor("Second Chapter", catFont); [Link]("Second Chapter");
subPara = new Paragraph("Subcategory", subFont); subCatPart = [Link](subPara); [Link](new Paragraph("This is a very important message"));
[Link](catPart);
private static void createTable(Section subCatPart) throws BadElementException { PdfPTable table = new PdfPTable(3);
[Link](table);
private static void createList(Section subCatPart) { List list = new List(true, false, 10); [Link](new ListItem("First point")); [Link](new ListItem("Second point")); [Link](new ListItem("Third point")); [Link](list); }
private static void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { [Link](new Paragraph(" ")); } } }
package [Link];
import [Link];
public static void main(String[] args) { try { Document document = new Document(); [Link](document, new FileOutputStream(FILE)); [Link]();
// Left
Paragraph paragraph = new Paragraph("This is right aligned text"); [Link](Element.ALIGN_RIGHT); [Link](paragraph);
// Centered
paragraph = new Paragraph("This is centered text"); [Link](Element.ALIGN_CENTER); [Link](paragraph);
// Left
paragraph = new Paragraph("This is left aligned text"); [Link](Element.ALIGN_LEFT); [Link](paragraph);
[Link](50); [Link](paragraph);