Wie können mit Java PDF Dokumente erzeugt werden?
Das geht mit ein paar Zeilen Code und iText. Die iText Lib läßt sich leicht integrieren. Hier mal ein Beispiel um ein paar Daten zu haben, nehmen wir mal einen Lebenskalender. Der ganze Code (nur eine Java Klasse) liegt auf Github und kann von dort geladen werden.
Also mal ein neues Mavenprojekt anlegen und in der pom.xml die iText Abhängigkeit einfügen:
1 2 3 4 5 |
<dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>4.2.0</version> </dependency> |
Dann mal ein paar Zeilen Javacode für die PDF Erzeugung z.B. so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
private static void printPdfDokument(List<String> kalender) throws DocumentException, FileNotFoundException { Document document = new Document(PageSize.A4); PdfWriter.getInstance(document, new FileOutputStream("lebenskalender.pdf")); document.open(); Font f = new Font(); f.setFamily("Courier"); f.setSize(10); for (String woche : kalender) { document.add(new Paragraph(woche, f)); } document.close(); } |
und schon können wir ein PDF Dokument erzeugen. Hier das Ergebnis lebenskalender.pdf.
Hier mal die Ausgabe auf System.out:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
Lebenskalender Berechne von aktuellem Alter 52 Jahren und 7 Monate Jahre Wochen 0 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 1 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 2 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 4 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXX 42 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 43 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 44 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 45 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 46 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 47 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 48 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 49 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 50 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 51 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 52 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 53 XXXXXXXXXXXXXXXXXXXXXXXXXXXX........................ 54 .................................................... 55 .................................................... 56 .................................................... 57 .................................................... 58 .................................................... 59 .................................................... 60 .................................................... 61 .................................................... 62 .................................................... 63 .................................................... 64 .................................................... 65 .................................................... 66 .................................................... 67 .................................................... 68 .................................................... 69 .................................................... 70 .................................................... 71 .................................................... 72 .................................................... 73 .................................................... 74 .................................................... 75 .................................................... 76 .................................................... 77 .................................................... Lebenserwartung in Deutschland: Männer 77 Jahre 9 Monate Frauen 82 Jahre 10 Monate Statistisch also noch: 27 Jahre als Mann zu leben! |
Wer auch so einen Kalender mit seinen Daten haben will, kann mir gern eine E-Mail senden mit Angabe seines Alters. Ich sende dann den ersten 10 Einsendungen kostenlos einen PDF Lebenskalender zu 😉
Wer die Idee Gut findet, kann ja einen like abgeben.