Example usage for com.lowagie.text Document Document

List of usage examples for com.lowagie.text Document Document

Introduction

In this page you can find the example usage for com.lowagie.text Document Document.

Prototype


public Document() 

Source Link

Document

Constructs a new Document -object.

Usage

From source file:classroom.filmfestival_b.Movies13.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {

    // step 1//  w  w w  .  ja v a2s. co  m
    Document document = new Document();
    try {
        // step 2
        OutputStream os = new FileOutputStream(RESULT);
        PdfWriter.getInstance(document, os);
        // step 3
        document.open();
        // step 4
        Session session = (Session) MySessionFactory.currentSession();
        Query q = session.createQuery("from FilmTitle order by title");
        java.util.List<FilmTitle> results = q.list();

        PdfPTable table = new PdfPTable(2);
        table.setWidths(new float[] { 1, 5 });
        File f;
        Paragraph p;
        Chunk c;
        PdfPCell cell;
        Font bold = new Font(Font.HELVETICA, 12, Font.BOLD);
        Font italic = new Font(Font.HELVETICA, 12, Font.ITALIC);
        for (FilmTitle movie : results) {
            f = new File("resources/classroom/filmposters/" + movie.getFilmId() + ".jpg");
            if (f.exists()) {
                cell = new PdfPCell(Image.getInstance(f.getPath()), true);
                cell.setPadding(2);
            } else {
                cell = new PdfPCell();
            }
            table.addCell(cell);
            p = new Paragraph(20);
            c = new Chunk(movie.getTitle(), bold);
            c.setAnchor("http://cinema.lowagie.com/titel.php?id=" + movie.getFilmId());
            p.add(c);
            c = new Chunk(" (" + movie.getYear() + ") ", italic);
            p.add(c);
            c = new Chunk("IMDB");
            c.setAnchor("http://www.imdb.com/title/tt" + movie.getImdb());
            p.add(c);
            cell = new PdfPCell();
            cell.setUseAscender(true);
            cell.setUseDescender(true);
            cell.addElement(p);
            Set<DirectorName> directors = movie.getDirectorNames();
            List list = new List();
            for (DirectorName director : directors) {
                list.add(director.getName());
            }
            cell.addElement(list);
            table.addCell(cell);
        }
        document.add(table);
        // step 5
        document.close();
    } catch (IOException e) {
        LOGGER.error("IOException: ", e);
    } catch (DocumentException e) {
        LOGGER.error("DocumentException: ", e);
    }
}

From source file:classroom.filmfestival_b.Movies14.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {

    // step 1//from w w w.j a  v a  2  s. co  m
    Document document = new Document();
    try {
        // step 2
        OutputStream os = new FileOutputStream(RESULT);
        PdfWriter.getInstance(document, os);
        // step 3
        document.open();
        // step 4
        Session session = (Session) MySessionFactory.currentSession();
        Query q = session.createQuery("from FilmTitle order by title");
        java.util.List<FilmTitle> results = q.list();

        PdfPTable table = new PdfPTable(2);
        table.setWidths(new float[] { 1, 5 });
        File f;
        Paragraph p;
        Chunk c;
        PdfPCell cell = new PdfPCell();
        Font bold = new Font(Font.HELVETICA, 12, Font.BOLD);
        Font italic = new Font(Font.HELVETICA, 12, Font.ITALIC);
        p = new Paragraph("FILMFESTIVAL", bold);
        p.setAlignment(Element.ALIGN_CENTER);
        cell.addElement(p);
        cell.setColspan(2);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell();
        cell.setFixedHeight(20);
        cell.setColspan(2);
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setCellEvent(new Movies14().new PageCell());
        table.addCell(cell);
        table.setHeaderRows(2);
        table.setFooterRows(1);
        for (FilmTitle movie : results) {
            f = new File("resources/classroom/filmposters/" + movie.getFilmId() + ".jpg");
            if (f.exists()) {
                cell = new PdfPCell(Image.getInstance(f.getPath()), true);
                cell.setPadding(2);
            } else {
                cell = new PdfPCell();
            }
            table.addCell(cell);
            p = new Paragraph(20);
            c = new Chunk(movie.getTitle(), bold);
            c.setAnchor("http://cinema.lowagie.com/titel.php?id=" + movie.getFilmId());
            p.add(c);
            c = new Chunk(" (" + movie.getYear() + ") ", italic);
            p.add(c);
            c = new Chunk("IMDB");
            c.setAnchor("http://www.imdb.com/title/tt" + movie.getImdb());
            p.add(c);
            cell = new PdfPCell();
            cell.setUseAscender(true);
            cell.setUseDescender(true);
            cell.addElement(p);
            Set<DirectorName> directors = movie.getDirectorNames();
            List list = new List();
            for (DirectorName director : directors) {
                list.add(director.getName());
            }
            cell.addElement(list);
            table.addCell(cell);
        }
        document.add(table);
        // step 5
        document.close();
    } catch (IOException e) {
        LOGGER.error("IOException: ", e);
    } catch (DocumentException e) {
        LOGGER.error("DocumentException: ", e);
    }
}

From source file:classroom.filmfestival_b.Movies15.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {

    // step 1/*  w  w  w.jav  a 2  s.c  o  m*/
    Document document = new Document();
    try {
        // step 2
        OutputStream os = new FileOutputStream(RESULT);
        PdfWriter writer = PdfWriter.getInstance(document, os);
        // step 3
        document.open();
        // step 4
        Session session = (Session) MySessionFactory.currentSession();
        Query q = session.createQuery("from FilmTitle order by title");
        java.util.List<FilmTitle> results = q.list();

        PdfPTable table = new PdfPTable(2);
        table.setComplete(false);
        table.setWidths(new float[] { 1, 5 });
        File f;
        Paragraph p;
        Chunk c;
        PdfPCell cell = new PdfPCell();
        Font bold = new Font(Font.HELVETICA, 12, Font.BOLD);
        Font italic = new Font(Font.HELVETICA, 12, Font.ITALIC);
        p = new Paragraph("FILMFESTIVAL", bold);
        p.setAlignment(Element.ALIGN_CENTER);
        cell.addElement(p);
        cell.setColspan(2);
        cell.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell);
        cell = new PdfPCell();
        cell.setFixedHeight(20);
        cell.setColspan(2);
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setCellEvent(new Movies14().new PageCell());
        table.addCell(cell);
        table.setHeaderRows(2);
        table.setFooterRows(1);
        int counter = 10;
        for (FilmTitle movie : results) {
            f = new File("resources/classroom/filmposters/" + movie.getFilmId() + ".jpg");
            if (f.exists()) {
                cell = new PdfPCell(Image.getInstance(f.getPath()), true);
                cell.setPadding(2);
            } else {
                cell = new PdfPCell();
            }
            table.addCell(cell);
            p = new Paragraph(20);
            c = new Chunk(movie.getTitle(), bold);
            c.setAnchor("http://cinema.lowagie.com/titel.php?id=" + movie.getFilmId());
            p.add(c);
            c = new Chunk(" (" + movie.getYear() + ") ", italic);
            p.add(c);
            c = new Chunk("IMDB");
            c.setAnchor("http://www.imdb.com/title/tt" + movie.getImdb());
            p.add(c);
            cell = new PdfPCell();
            cell.setUseAscender(true);
            cell.setUseDescender(true);
            cell.addElement(p);
            Set<DirectorName> directors = movie.getDirectorNames();
            List list = new List();
            for (DirectorName director : directors) {
                list.add(director.getName());
            }
            cell.addElement(list);
            table.addCell(cell);
            if (counter % 10 == 0) {
                document.add(table);
            }
            System.out.println(writer.getPageNumber());
            counter++;
        }
        table.setComplete(true);
        document.add(table);
        // step 5
        document.close();
    } catch (IOException e) {
        LOGGER.error("IOException: ", e);
    } catch (DocumentException e) {
        LOGGER.error("DocumentException: ", e);
    }
}

From source file:classroom.filmfestival_c.Movies16.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {

    // step 1//  w w w .j  av  a  2 s.  co m
    Document document = new Document();
    try {
        // step 2
        OutputStream os = new FileOutputStream(RESULT);
        PdfWriter.getInstance(document, os);
        // step 3
        document.open();
        // step 4
        PdfPTable table;
        PdfPCell cell;
        Chunk imdb;

        Session session = (Session) MySessionFactory.currentSession();
        Query q = session.createQuery(
                "select distinct festival.id.day from FestivalScreening as festival order by festival.id.day");
        java.util.List<Date> days = q.list();
        java.util.List<FestivalScreening> screenings;

        for (Date day : days) {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(day);
            if (gc.get(GregorianCalendar.YEAR) != YEAR)
                continue;

            table = new PdfPTable(new float[] { 7, 1, 2, 1 });
            cell = new PdfPCell(new Phrase(day.toString(), NORMALWHITE));
            cell.setBackgroundColor(BLACK);
            cell.setColspan(4);
            table.addCell(cell);

            q = session.createQuery("from FestivalScreening where id.day=? order by id.time, id.place");
            q.setDate(0, day);
            screenings = q.list();
            for (FestivalScreening screening : screenings) {
                table.addCell(screening.getFilmTitle().getTitle());
                table.addCell(screening.getId().getPlace().toString());
                cell = new PdfPCell(new Phrase(screening.getId().getTime().toString()));
                if (screening.getPress() == 1) {
                    cell.setBackgroundColor(SILVER);
                }
                table.addCell(cell);
                if (screening.getFilmTitle().getImdb().startsWith("?")) {
                    table.addCell("");
                } else {
                    imdb = new Chunk("imdb");
                    imdb.setAnchor("http://imdb.com/title/tt" + screening.getFilmTitle().getImdb());
                    table.addCell(new Phrase(imdb));
                }
            }

            document.add(table);
        }

        // step 5
        document.close();
    } catch (IOException e) {
        LOGGER.error("IOException: ", e);
    } catch (DocumentException e) {
        LOGGER.error("DocumentException: ", e);
    }
}

From source file:classroom.filmfestival_c.Movies17.java

@SuppressWarnings({ "unchecked", "deprecation" })
public static void main(String[] args) {

    // step 1//from  w ww  .  ja  va2 s . co m
    Document document = new Document();
    try {
        // step 2
        OutputStream os = new FileOutputStream(RESULT);
        PdfWriter.getInstance(document, os);
        // step 3
        document.open();
        // step 4

        Session session = (Session) MySessionFactory.currentSession();
        Query q = session.createQuery(
                "select distinct festival.id.day from FestivalScreening as festival order by festival.id.day");
        java.util.List<Date> days = q.list();

        Query query = session.createQuery("from FestivalScreening where id.day=? order by id.time, id.place");
        for (Date day : days) {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(day);
            if (gc.get(GregorianCalendar.YEAR) != YEAR)
                continue;
            document.add(getTable(query, day));
            document.newPage();
        }

        // step 5
        document.close();
    } catch (IOException e) {
        LOGGER.error("IOException: ", e);
    } catch (DocumentException e) {
        LOGGER.error("DocumentException: ", e);
    }
}

From source file:classroom.filmfestival_c.Movies18.java

@SuppressWarnings({ "unchecked", "deprecation" })
public static void main(String[] args) {

    // step 1/*from  ww w . ja v  a 2  s  .  c om*/
    Document document = new Document();
    try {
        // step 2
        OutputStream os = new FileOutputStream(RESULT);
        PdfWriter.getInstance(document, os);
        // step 3
        document.open();
        // step 4

        Session session = (Session) MySessionFactory.currentSession();
        Query q = session.createQuery(
                "select distinct festival.id.day from FestivalScreening as festival order by festival.id.day");
        java.util.List<Date> days = q.list();

        Chapter chapter;
        Query query = session.createQuery("from FestivalScreening where id.day=? order by id.time, id.place");
        for (Date day : days) {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(day);
            if (gc.get(GregorianCalendar.YEAR) != YEAR)
                continue;
            chapter = new Chapter("", 0);
            chapter.setNumberDepth(0);
            chapter.setBookmarkTitle(day.toString());
            chapter.add(getTable(query, day));
            document.add(chapter);
        }

        // step 5
        document.close();
    } catch (IOException e) {
        LOGGER.error("IOException: ", e);
    } catch (DocumentException e) {
        LOGGER.error("DocumentException: ", e);
    }
}

From source file:classroom.filmfestival_c.Movies21.java

@SuppressWarnings("unchecked")
public static void main(String args[]) {
    Movies18.main(args);//from www .  j  a  va 2  s . c om
    Movies20.main(args);
    try {
        PdfReader reader1 = new PdfReader(Movies18.RESULT);
        List<Map> list1 = SimpleBookmark.getBookmark(reader1);
        int[] offsets1 = new int[list1.size() + 1];
        int count = 0;
        for (Map<String, String> mark : list1) {
            offsets1[count++] = getPageNumber(mark.get("Page"));
        }
        offsets1[count] = reader1.getNumberOfPages() + 1;
        PdfReader reader2 = new PdfReader(Movies20.RESULT);
        List<Map> list2 = SimpleBookmark.getBookmark(reader2);
        if (list2.size() != list1.size()) {
            throw new DocumentException("The documents don't have the same number of bookmark entries.");
        }
        int[] offsets2 = new int[list2.size() + 1];
        count = 0;
        for (Map<String, String> mark : list2) {
            offsets2[count++] = getPageNumber(mark.get("Page"));
        }
        offsets2[count] = reader2.getNumberOfPages() + 1;
        Document document = new Document();
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT));
        document.open();
        for (int i = 0; i < list1.size(); i++) {
            for (int j = offsets1[i]; j < offsets1[i + 1]; j++) {
                copy.addPage(copy.getImportedPage(reader1, j));
            }
            for (int j = offsets2[i]; j < offsets2[i + 1]; j++) {
                copy.addPage(copy.getImportedPage(reader2, j));
            }
        }
        document.close();
    } catch (IOException e) {
        LOGGER.warn("IOException: " + e);
    } catch (DocumentException e) {
        LOGGER.warn("IOException: " + e);
    }
}

From source file:classroom.filmfestival_c.Movies24.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    Session session = null;/*  w w w. j  av  a  2  s  .  c  o m*/
    try {
        session = (Session) MySessionFactory.currentSession();
        Query q = session.createQuery("from FilmTitle order by title");
        List<FilmTitle> results = q.list();
        Document document = new Document();
        PdfSmartCopy copy = new PdfSmartCopy(document, new FileOutputStream(RESULT));
        document.open();
        PdfReader reader;
        PdfStamper stamper;
        AcroFields form;
        ByteArrayOutputStream baos;
        for (FilmTitle movie : results) {
            baos = new ByteArrayOutputStream();
            reader = new PdfReader(DATASHEET);
            stamper = new PdfStamper(reader, baos);
            form = stamper.getAcroFields();
            form.setField("title", movie.getTitle());
            form.setField("director", getDirectors(movie));
            form.setField("year", String.valueOf(movie.getYear()));
            form.setField("duration", String.valueOf(movie.getDuration()));
            form.setField("category", "c" + getCategory(movie));
            Set<FestivalScreening> screenings = (Set<FestivalScreening>) movie.getFestivalScreenings();
            for (FestivalScreening screening : screenings) {
                form.setField(screening.getId().getPlace(), "Yes");
            }
            stamper.setFormFlattening(true);
            stamper.close();
            reader = new PdfReader(baos.toByteArray());
            copy.addPage(copy.getImportedPage(reader, 1));
        }
        document.close();
    } catch (HibernateException e) {
        LOGGER.warn("HibernateException: " + e);
    } catch (IOException e) {
        LOGGER.warn("IOException: " + e);
    } catch (DocumentException e) {
        LOGGER.warn("DocumentException: " + e);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
        } catch (HibernateException e) {
            LOGGER.warn("HibernateTest - Closing session: " + e);
        }
    }
}

From source file:classroom.filmfestival_c.Movies25.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    createTemplate();//ww  w. j a  v a  2  s . c  o m
    Session session = (Session) MySessionFactory.currentSession();
    Query q = session.createQuery("from FilmTitle order by title");
    java.util.List<FilmTitle> results = q.list();
    try {
        Document document = new Document();
        PdfSmartCopy copy = new PdfSmartCopy(document, new FileOutputStream(RESULT));
        document.open();

        PdfReader reader;
        PdfStamper stamper = null;
        ByteArrayOutputStream baos = null;
        AcroFields form = null;
        int count = 0;
        for (FilmTitle movie : results) {
            if (count == 0) {
                baos = new ByteArrayOutputStream();
                reader = new PdfReader(BACKGROUND);
                stamper = new PdfStamper(reader, baos);
                stamper.setFormFlattening(true);
                form = stamper.getAcroFields();
            }
            count++;
            byte[] pdf = createPdf(movie);
            reader = new PdfReader(pdf);
            PdfImportedPage page = stamper.getImportedPage(reader, 1);
            PushbuttonField bt = form.getNewPushbuttonFromField("movie_" + count);
            bt.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
            bt.setProportionalIcon(true);
            bt.setTemplate(page);
            form.replacePushbuttonField("movie_" + count, bt.getField());
            if (count == 16) {
                stamper.close();
                reader = new PdfReader(baos.toByteArray());
                copy.addPage(copy.getImportedPage(reader, 1));
                count = 0;
            }
        }
        if (count > 0) {
            stamper.close();
            reader = new PdfReader(baos.toByteArray());
            copy.addPage(copy.getImportedPage(reader, 1));
            count = 0;
        }
        document.close();
    } catch (IOException ioe) {
        LOGGER.error("IOException: ", ioe);
    } catch (DocumentException de) {
        LOGGER.error("DocumentException: ", de);
    }
}

From source file:classroom.intro.HelloWorld01.java

public static void main(String[] args) {

    // step 1/*from  w  w w. j  a  v  a2 s  .c o m*/
    Document document = new Document();
    try {
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World"));
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
}