/* * This class is part of the book "iText in Action - 2nd Edition" * written by Bruno Lowagie (ISBN: 9781935182610) * For more info, go to: http://itextpdf.com/examples/ * This example only works with the AGPL version of iText. */ package part1.chapter05; import java.io.FileOutputStream; import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Set; import java.util.TreeSet; import com.lowagie.database.DatabaseConnection; import com.lowagie.database.HsqldbConnection; import com.lowagie.filmfestival.FilmFonts; import com.lowagie.filmfestival.Movie; import com.lowagie.filmfestival.MovieComparator; import com.lowagie.filmfestival.PojoFactory; import com.lowagie.filmfestival.PojoToElementFactory; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Font.FontFamily; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.GrayColor; import com.itextpdf.text.pdf.PdfPageEventHelper; import com.itextpdf.text.pdf.PdfWriter; public class MovieCountries2 extends MovieCountries1 { /** The resulting PDF file. */ public static final String RESULT = "results/part1/chapter05/movie_countries2.pdf"; /** * Inner class to add a watermark to every page. */ class Watermark extends PdfPageEventHelper { Font FONT = new Font(FontFamily.HELVETICA, 52, Font.BOLD, new GrayColor(0.75f)); public void onEndPage(PdfWriter writer, Document document) { ColumnText.showTextAligned(writer.getDirectContentUnder(), Element.ALIGN_CENTER, new Phrase("FOOBAR FILM FESTIVAL", FONT), 297.5f, 421, writer.getPageNumber() % 2 == 1 ? 45 : -45); } } /** * Creates a PDF document. * @param filename the path to the new PDF document * @throws DocumentException * @throws IOException * @throws SQLException */ public void createPdf(String filename) throws IOException, DocumentException, SQLException { // Create a database connection DatabaseConnection connection = new HsqldbConnection("filmfestival"); // step 1 Document document = new Document(PageSize.A4, 36, 36, 54, 36); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); TableHeader event = new TableHeader(); writer.setPageEvent(event); writer.setPageEvent(new Watermark()); // step 3 document.open(); // step 4 Statement stm = connection.createStatement(); ResultSet rs = stm.executeQuery( "SELECT country, id FROM film_country ORDER BY country"); while (rs.next()) { event.setHeader(rs.getString("country")); Set movies = new TreeSet(new MovieComparator(MovieComparator.BY_YEAR)); movies.addAll(PojoFactory.getMovies(connection, rs.getString("id"))); for(Movie movie : movies) { document.add(new Paragraph(movie.getMovieTitle(), FilmFonts.BOLD)); if (movie.getOriginalTitle() != null) document.add(new Paragraph(movie.getOriginalTitle(), FilmFonts.ITALIC)); document.add(new Paragraph(String.format("Year: %d; run length: %d minutes", movie.getYear(), movie.getDuration()), FilmFonts.NORMAL)); document.add(PojoToElementFactory.getDirectorList(movie)); } document.newPage(); } // step 5 document.close(); // close the database connection connection.close(); } /** * Main method. * * @param args no arguments needed * @throws DocumentException * @throws IOException * @throws SQLException */ public static void main(String[] args) throws IOException, DocumentException, SQLException { new MovieCountries2().createPdf(RESULT); } }