/* * 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 part2.chapter06; 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.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import com.itextpdf.text.pdf.PdfWriter; public class StampStationery { /** The original PDF file. */ public static final String ORIGINAL = "results/part2/chapter06/original.pdf"; /** The resulting PDF. */ public static final String RESULT = "results/part2/chapter06/stamped_stationary.pdf"; /** * Main method. * @param args no arguments needed * @throws DocumentException * @throws IOException * @throws SQLException */ public static void main(String[] args) throws IOException, DocumentException, SQLException { Stationery.createStationary(Stationery.STATIONERY); StampStationery stationary = new StampStationery(); stationary.createPdf(ORIGINAL); stationary.manipulatePdf(ORIGINAL, Stationery.STATIONERY, RESULT); } /** * Manipulates a PDF file src with the file dest as result * @param src the original PDF * @param stationery a PDF that will be added as background * @param dest the resulting PDF * @throws IOException * @throws DocumentException */ public void manipulatePdf(String src, String stationery, String dest) throws IOException, DocumentException { // Create readers PdfReader reader = new PdfReader(src); PdfReader s_reader = new PdfReader(stationery); // Create the stamper PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); // Add the stationery to each page PdfImportedPage page = stamper.getImportedPage(s_reader, 1); int n = reader.getNumberOfPages(); PdfContentByte background; for (int i = 1; i <= n; i++) { background = stamper.getUnderContent(i); background.addTemplate(page, 0, 0); } // CLose the stamper stamper.close(); reader.close(); s_reader.close(); } /** * 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 SQLException, IOException, DocumentException { // Create a database connection DatabaseConnection connection = new HsqldbConnection("filmfestival"); // step 1 Document document = new Document(PageSize.A4, 36, 36, 72, 36); // step 2 PdfWriter.getInstance(document, new FileOutputStream(filename)); // 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()) { document.add(new Paragraph(rs.getString("country"), FilmFonts.BOLD)); document.add(Chunk.NEWLINE); 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(); } }