/** * Manipulates a PDF file src with the file dest as result * * @param src the original PDF * @param dest the resulting PDF * @throws IOException * @throws DocumentException * @throws SQLException */ public void manipulatePdf(String src, String dest) throws IOException, DocumentException, SQLException { // Create a database connection DatabaseConnection connection = new HsqldbConnection("filmfestival"); // Create a list with bookmarks ArrayList<HashMap<String, Object>> outlines = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> map = new HashMap<String, Object>(); outlines.add(map); map.put("Title", "Calendar"); ArrayList<HashMap<String, Object>> kids = new ArrayList<HashMap<String, Object>>(); map.put("Kids", kids); int page = 1; List<Date> days = PojoFactory.getDays(connection); for (Date day : days) { HashMap<String, Object> kid = new HashMap<String, Object>(); kids.add(kid); kid.put("Title", day.toString()); kid.put("Action", "GoTo"); kid.put("Page", String.format("%d Fit", page++)); } // Create a reader PdfReader reader = new PdfReader(src); // Create a stamper PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); stamper.setOutlines(outlines); // Close the stamper stamper.close(); reader.close(); // Close the database connection connection.close(); }
/** * Creates a PDF document. * * @param filename the path to the new PDF document * @throws DocumentException * @throws IOException */ public void createPdf(String filename) throws IOException, DocumentException { // step 1 Document document = new Document(PageSize.A4.rotate()); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3 document.open(); // step 4 PdfContentByte over = writer.getDirectContent(); PdfContentByte under = writer.getDirectContentUnder(); try { DatabaseConnection connection = new HsqldbConnection("filmfestival"); locations = PojoFactory.getLocations(connection); List<Date> days = PojoFactory.getDays(connection); List<Screening> screenings; int d = 1; for (Date day : days) { drawTimeTable(under); drawTimeSlots(over); drawInfo(over); drawDateInfo(day, d++, over); screenings = PojoFactory.getScreenings(connection, day); for (Screening screening : screenings) { drawBlock(screening, under, over); drawMovieInfo(screening, over); } document.newPage(); } connection.close(); } catch (SQLException sqle) { sqle.printStackTrace(); document.add(new Paragraph("Database error: " + sqle.getMessage())); } // step 5 document.close(); }