| home | suche | kontakt/johner | institut | hinweise studierende | tech-docs | blog | mindmailer |
![]() |
PDF Generierung
Mit der Bibliothek von itext lassen sich leicht PDFs aus Java-Code heraus generieren. Was muss dazu gemacht werden? Ganz einfach: die Datei itext-xxx.jar von http://www.lowagie.com/iText/ herunter laden und zum Klassenpfad hinzufügen (in Eclipse rechter Mausklick auf das Projekt -> Properties -> Java Build Path -> Libraries).
Auf der oben genannten Seite findet sich auch ein gutes Tutorial.
Im folgenden Code erzeugen wir ein PDF, das wie folgt aussieht:
Beispiel-Code
package itext;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import jfreechart.FreeChart;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
public class IText {
public static void main(String[] args) {
try {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));
document.open();
//Chapters und
Paragraph title1 = new Paragraph("Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0); //do not show chapter number on page
//... Sections
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
//Text zu Section
Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
//Liste zu Section
List l = new List(true, false, 10);
l.add(new ListItem("First item of list"));
l.add(new ListItem("Second item of list"));
section1.add(l);
document.add(chapter1);
document.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

