11package de .vogella .pdf .write ;
22
3- import java .awt .Color ;
43import java .io .FileOutputStream ;
54import java .util .Date ;
65
7- import com .lowagie .text .Anchor ;
8- import com .lowagie .text .BadElementException ;
9- import com .lowagie .text .Cell ;
10- import com .lowagie .text .Chapter ;
11- import com .lowagie .text .Document ;
12- import com .lowagie .text .DocumentException ;
13- import com .lowagie .text .Font ;
14- import com .lowagie .text .List ;
15- import com .lowagie .text .ListItem ;
16- import com .lowagie .text .Paragraph ;
17- import com .lowagie .text .Section ;
18- import com .lowagie .text .Table ;
19- import com .lowagie .text .pdf .PdfWriter ;
6+ import com .itextpdf .text .Anchor ;
7+ import com .itextpdf .text .BadElementException ;
8+ import com .itextpdf .text .BaseColor ;
9+ import com .itextpdf .text .Chapter ;
10+ import com .itextpdf .text .Document ;
11+ import com .itextpdf .text .DocumentException ;
12+ import com .itextpdf .text .Element ;
13+ import com .itextpdf .text .Font ;
14+ import com .itextpdf .text .List ;
15+ import com .itextpdf .text .ListItem ;
16+ import com .itextpdf .text .Paragraph ;
17+ import com .itextpdf .text .Phrase ;
18+ import com .itextpdf .text .Section ;
19+ import com .itextpdf .text .pdf .PdfPCell ;
20+ import com .itextpdf .text .pdf .PdfPTable ;
21+ import com .itextpdf .text .pdf .PdfWriter ;
2022
2123public class FirstPdf {
2224 private static String FILE = "c:/temp/FirstPdf.pdf" ;
23- private static Font catFont = new Font (Font .TIMES_ROMAN , 18 , Font .BOLD );
24- private static Font redFont = new Font (Font .TIMES_ROMAN , 12 , Font .NORMAL ,
25- Color .RED );
26- private static Font subFont = new Font (Font .TIMES_ROMAN , 16 , Font .BOLD );
27- private static Font smallBold = new Font (Font .TIMES_ROMAN , 12 , Font .BOLD );
25+ private static Font catFont = new Font (Font .FontFamily .TIMES_ROMAN , 18 ,
26+ Font .BOLD );
27+ private static Font redFont = new Font (Font .FontFamily .TIMES_ROMAN , 12 ,
28+ Font .NORMAL , BaseColor .RED );
29+ private static Font subFont = new Font (Font .FontFamily .TIMES_ROMAN , 16 ,
30+ Font .BOLD );
31+ private static Font smallBold = new Font (Font .FontFamily .TIMES_ROMAN , 12 ,
32+ Font .BOLD );
2833
2934 public static void main (String [] args ) {
3035 try {
@@ -61,21 +66,19 @@ private static void addTitlePage(Document document)
6166
6267 addEmptyLine (preface , 1 );
6368 // Will create: Report generated by: _name, _date
64- preface
65- .add (new Paragraph (
66- "Report generated by: " + System .getProperty ("user.name" ) + ", " + new Date (), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
67- smallBold ));
69+ preface .add (new Paragraph (
70+ "Report generated by: " + System .getProperty ("user.name" ) + ", " + new Date (), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
71+ smallBold ));
6872 addEmptyLine (preface , 3 );
6973 preface .add (new Paragraph (
7074 "This document describes something which is very important " ,
7175 smallBold ));
7276
7377 addEmptyLine (preface , 8 );
7478
75- preface
76- .add (new Paragraph (
77- "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.de ;-)." ,
78- redFont ));
79+ preface .add (new Paragraph (
80+ "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.de ;-)." ,
81+ redFont ));
7982
8083 document .add (preface );
8184 // Start a new page
@@ -98,16 +101,16 @@ private static void addContent(Document document) throws DocumentException {
98101 subCatPart .add (new Paragraph ("Paragraph 1" ));
99102 subCatPart .add (new Paragraph ("Paragraph 2" ));
100103 subCatPart .add (new Paragraph ("Paragraph 3" ));
101-
102- // Add a little list
104+
105+ // Add a list
103106 createList (subCatPart );
107+ Paragraph paragraph = new Paragraph ();
108+ addEmptyLine (paragraph , 5 );
109+ subCatPart .add (paragraph );
104110
105- // Add a small table
111+ // Add a table
106112 createTable (subCatPart );
107- // Now a small table
108-
109113
110-
111114 // Now add all this to the document
112115 document .add (catPart );
113116
@@ -121,37 +124,42 @@ private static void addContent(Document document) throws DocumentException {
121124 subPara = new Paragraph ("Subcategory" , subFont );
122125 subCatPart = catPart .addSection (subPara );
123126 subCatPart .add (new Paragraph ("This is a very important message" ));
124-
127+
125128 // Now add all this to the document
126129 document .add (catPart );
127130
128131 }
129132
130- private static void createTable (Section subCatPart ) throws BadElementException {
131- Table t = new Table (3 ,2 );
132-
133- t .setBorderColor (Color .GRAY );
134- t .setPadding (4 );
135- t .setSpacing (4 );
136- t .setBorderWidth (1 );
137-
138- Cell c1 = new Cell ("Table Header 1" );
139- c1 .setHeader (true );
140- t .addCell (c1 );
141- c1 = new Cell ("Table Header 2" );
142- t .addCell (c1 );
143- c1 = new Cell ("Table Header 3" );
144- t .addCell (c1 );
145- t .endHeaders ();
146-
147- t .addCell ("1.0" );
148- t .addCell ("1.1" );
149- t .addCell ("1.2" );
150- t .addCell ("2.1" );
151- t .addCell ("2.2" );
152- t .addCell ("2.3" );
153-
154- subCatPart .add (t );
133+ private static void createTable (Section subCatPart )
134+ throws BadElementException {
135+ PdfPTable table = new PdfPTable (3 );
136+
137+ // t.setBorderColor(BaseColor.GRAY);
138+ // t.setPadding(4);
139+ // t.setSpacing(4);
140+ // t.setBorderWidth(1);
141+
142+ PdfPCell c1 = new PdfPCell (new Phrase ("Table Header 1" ));
143+ c1 .setHorizontalAlignment (Element .ALIGN_CENTER );
144+ table .addCell (c1 );
145+
146+ c1 = new PdfPCell (new Phrase ("Table Header 2" ));
147+ c1 .setHorizontalAlignment (Element .ALIGN_CENTER );
148+ table .addCell (c1 );
149+
150+ c1 = new PdfPCell (new Phrase ("Table Header 3" ));
151+ c1 .setHorizontalAlignment (Element .ALIGN_CENTER );
152+ table .addCell (c1 );
153+ table .setHeaderRows (1 );
154+
155+ table .addCell ("1.0" );
156+ table .addCell ("1.1" );
157+ table .addCell ("1.2" );
158+ table .addCell ("2.1" );
159+ table .addCell ("2.2" );
160+ table .addCell ("2.3" );
161+
162+ subCatPart .add (table );
155163
156164 }
157165
0 commit comments