/**
  * 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();
 }
  /**
   * Creates the PDF.
   *
   * @return the bytes of a PDF file.
   * @throws DocumentExcetpion
   * @throws IOException
   * @throws SQLException
   */
  public byte[] createPdf() throws DocumentException, IOException, SQLException {
    DatabaseConnection connection = new HsqldbConnection("filmfestival");
    java.util.List<Movie> movies = PojoFactory.getMovies(connection, 1);
    connection.close();
    // step 1
    Document document = new Document();
    // step 2
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(document, baos);
    // step 3
    document.open();
    // step 4
    document.add(
        new Paragraph(
            "'Stanley Kubrick: A Life in Pictures' is a documentary about Stanley Kubrick and his films:"));

    ByteArrayOutputStream txt = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(txt);
    out.println("<movies>");
    List list = new List(List.UNORDERED, 20);
    ListItem item;
    for (Movie movie : movies) {
      out.println("<movie>");
      out.println(
          String.format("<title>%s</title>", XMLUtil.escapeXML(movie.getMovieTitle(), true)));
      out.println(String.format("<year>%s</year>", movie.getYear()));
      out.println(String.format("<duration>%s</duration>", movie.getDuration()));
      out.println("</movie>");
      item = new ListItem(movie.getMovieTitle());
      list.add(item);
    }
    document.add(list);
    out.print("</movies>");
    out.flush();
    out.close();
    PdfFileSpecification fs =
        PdfFileSpecification.fileEmbedded(writer, null, "kubrick.xml", txt.toByteArray());
    writer.addFileAttachment(fs);
    // step 5
    document.close();
    return baos.toByteArray();
  }