Пример #1
0
  /**
   * 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();
  }