Example usage for com.lowagie.text.pdf ColumnText addElement

List of usage examples for com.lowagie.text.pdf ColumnText addElement

Introduction

In this page you can find the example usage for com.lowagie.text.pdf ColumnText addElement.

Prototype

public void addElement(Element element) 

Source Link

Document

Adds an element.

Usage

From source file:classroom.filmfestival_b.Movies09.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    // step 1/*from www.j av  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();

        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(document.left(), document.bottom(), document.right(), document.top());

        float pos;
        int status;
        File f;
        Image img;
        Paragraph p;
        Chunk c;
        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()) {
                img = Image.getInstance(f.getPath());
                img.setWidthPercentage(0);
                img.scaleToFit(72, 144);
            } else {
                img = null;
            }
            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);
            Set<DirectorName> directors = movie.getDirectorNames();
            List list = new List();
            for (DirectorName director : directors) {
                list.add(director.getName());
            }

            if (img != null)
                column.addElement(img);
            column.addElement(p);
            column.addElement(list);
            pos = column.getYLine();
            status = column.go(true);
            if (ColumnText.hasMoreText(status)) {
                document.newPage();
                column.setText(null);
                column.setYLine(document.top());
            } else {
                column.setYLine(pos);
            }
            if (img != null)
                column.addElement(img);
            column.addElement(p);
            column.addElement(list);
            column.addElement(Chunk.NEWLINE);
            column.go();
        }
        // step 5
        document.close();
    } catch (IOException e) {
        LOGGER.error("IOException: ", e);
    } catch (DocumentException e) {
        LOGGER.error("DocumentException: ", e);
    }
}

From source file:classroom.filmfestival_b.Movies10.java

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

    // step 1// w w w  .j  a  va  2  s . c o  m
    Document document = new Document();
    float middle = (document.right() + document.left()) / 2;
    float columns[][] = { { document.left(), document.bottom(), middle - 12, document.top() },
            { middle + 12, document.bottom(), document.right(), document.top() } };
    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();

        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(columns[0][0], columns[0][1], columns[0][2], columns[0][3]);

        float pos;
        int status;
        int ccount = 0;
        File f;
        Image img;
        Paragraph p;
        Chunk c;
        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()) {
                img = Image.getInstance(f.getPath());
                img.setWidthPercentage(0);
                img.scaleToFit(72, 144);
            } else {
                img = null;
            }
            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);
            Set<DirectorName> directors = movie.getDirectorNames();
            List list = new List();
            for (DirectorName director : directors) {
                list.add(director.getName());
            }

            if (img != null)
                column.addElement(img);
            column.addElement(p);
            column.addElement(list);
            pos = column.getYLine();
            status = column.go(true);
            if (ColumnText.hasMoreText(status)) {
                column.setText(null);
                ccount++;
                if (ccount > 1) {
                    ccount = 0;
                    document.newPage();
                    column.setSimpleColumn(columns[0][0], columns[0][1], columns[0][2], columns[0][3]);
                } else {
                    column.setSimpleColumn(columns[1][0], columns[1][1], columns[1][2], columns[1][3]);
                }
            } else {
                column.setYLine(pos);
            }
            if (img != null)
                column.addElement(img);
            column.addElement(p);
            column.addElement(list);
            column.addElement(Chunk.NEWLINE);
            column.go();
        }
        // step 5
        document.close();
    } catch (IOException e) {
        LOGGER.error("IOException: ", e);
    } catch (DocumentException e) {
        LOGGER.error("DocumentException: ", e);
    }
}

From source file:classroom.filmfestival_b.Movies11.java

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

    // step 1/*from w  w w  .  j  av  a 2s .  c o m*/
    Document document = new Document();
    document.setMargins(36, 36, 48, 48);
    float middle = (document.right() + document.left()) / 2;
    float columns[][] = { { document.left(), document.bottom(), middle - 12, document.top() },
            { middle + 12, document.bottom(), document.right(), document.top() } };
    try {
        // step 2
        OutputStream os = new FileOutputStream(RESULT);
        PdfWriter writer = PdfWriter.getInstance(document, os);
        writer.setPageEvent(new Movies11().new MoviePageEvents(middle, document.top(), document.bottom()));
        // 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();

        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(columns[0][0], columns[0][1], columns[0][2], columns[0][3]);

        float pos;
        int status;
        int ccount = 0;
        File f;
        Image img;
        Paragraph p;
        Chunk c;
        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()) {
                img = Image.getInstance(f.getPath());
                img.setWidthPercentage(0);
                img.scaleToFit(72, 144);
            } else {
                img = null;
            }
            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);
            Set<DirectorName> directors = movie.getDirectorNames();
            List list = new List();
            for (DirectorName director : directors) {
                list.add(director.getName());
            }

            if (img != null)
                column.addElement(img);
            column.addElement(p);
            column.addElement(list);
            pos = column.getYLine();
            status = column.go(true);
            if (ColumnText.hasMoreText(status)) {
                column.setText(null);
                ccount++;
                if (ccount > 1) {
                    ccount = 0;
                    document.newPage();
                    column.setSimpleColumn(columns[0][0], columns[0][1], columns[0][2], columns[0][3]);
                } else {
                    column.setSimpleColumn(columns[1][0], columns[1][1], columns[1][2], columns[1][3]);
                }
            } else {
                column.setYLine(pos);
            }
            if (img != null)
                column.addElement(img);
            column.addElement(p);
            column.addElement(list);
            column.addElement(Chunk.NEWLINE);
            column.go();
        }
        // step 5
        document.close();
    } catch (IOException e) {
        LOGGER.error("IOException: ", e);
    } catch (DocumentException e) {
        LOGGER.error("DocumentException: ", e);
    }
}

From source file:classroom.filmfestival_c.Movies25.java

@SuppressWarnings("unchecked")
public static boolean addText(String s, PdfContentByte canvas, float[] f, float size, boolean simulate)
        throws DocumentException, IOException {
    StyleSheet styles = new StyleSheet();
    styles.loadTagStyle("p", "size", size + "px");
    styles.loadTagStyle("p", "align", "justify");
    styles.loadTagStyle("p", "hyphenation", "en_us");
    ArrayList<Element> objects = HTMLWorker.parseToList(new StringReader(s), styles, null);
    ColumnText ct = new ColumnText(canvas);
    ct.setAlignment(Element.ALIGN_JUSTIFIED);
    ct.setLeading(size * 1.2f);//from   w ww.j  ava  2s .  co m
    ct.setSimpleColumn(f[1] + 2, f[2] + 2, f[3] - 2, f[4]);
    for (Element element : objects) {
        ct.addElement(element);
    }
    return ColumnText.hasMoreText(ct.go(simulate));
}

From source file:corner.orm.tapestry.pdf.components.AbstractPdfTableDisplay.java

License:Apache License

protected int findCurrentTemplateSourceStep(List source, PdfWriterDelegate writer, int rows, ColumnText ct)
        throws DocumentException {
    if (rows >= source.size()) {
        return rows;
    }/*from   w w w .ja va2 s .  c o  m*/
    PdfPTable table = createPdfTable(writer.getPdfWriter(), source, 0, rows);
    ct.addElement(table);
    if (ColumnText.hasMoreText(ct.go(true))) {
        ct.setYLine(this.getColumnTextStartHeight());
        return rows - 1;
    }
    // ?
    ct.setYLine(this.getColumnTextStartHeight());
    return findCurrentTemplateSourceStep(source, writer, ++rows, ct);
}

From source file:corner.orm.tapestry.pdf.components.AbstractPdfTableDisplay.java

License:Apache License

protected void addStepTableData(PdfWriterDelegate writer, List source, ColumnText ct, int currentSeq,
        int currentTemplateSourceStep, boolean isEndOfSource) {
    try {/*from w  ww .  j  a va2 s .c om*/
        //
        PdfPTable table = isEndOfSource
                ? createPdfFooterTable(writer.getPdfWriter(), source, currentSeq, currentTemplateSourceStep)
                : createPdfTable(writer.getPdfWriter(), source, currentSeq, currentTemplateSourceStep);
        ct.addElement(table);
        ct.go(false);
    } catch (DocumentException e) {
        throw new PdfSystemException(e);
    }
}

From source file:corner.orm.tapestry.pdf.components.PdfTable.java

License:Apache License

/**
 * ColumnText?// ww  w  . ja  v  a 2 s  .c o  m
 * 
 * @param ct
 *            ColumnText
 * @param writer
 *            pdf writer
 * @param doc
 *            document
 */
@Override
protected void renderColumnText(ColumnText ct, PdfWriterDelegate writer, Document doc) {
    List source = getSource();
    ct.addElement(createAllRowsTable(writer.getPdfWriter(), source));
}

From source file:fr.opensagres.odfdom.converter.pdf.internal.stylable.StylableDocumentSection.java

License:Open Source License

private List<ColumnText> fillTable(float height) {
    // copy text for simulation
    List<ColumnText> tt = null;
    if (breakHandlingParent == null && colIdx >= layoutTable.getNumberOfColumns()) {
        // more column breaks than available column
        // we try not to lose content
        // but results may be different than in open office
        // anyway it is logical error made by document creator
        tt = new ArrayList<ColumnText>();
        ColumnText t = createColumnText();
        tt.add(t);/* w  ww  . j a va2s. co m*/
        for (int i = 0; i < texts.size(); i++) {
            PdfPTable table = new PdfPTable(1);
            table.setWidthPercentage(100.0f);
            PdfPCell cell = new PdfPCell();
            cell.setBorder(Table.NO_BORDER);
            cell.setPadding(0.0f);
            cell.setColumn(ColumnText.duplicate(texts.get(i)));
            table.addCell(cell);
            t.addElement(table);
        }
    } else {
        tt = new ArrayList<ColumnText>(texts);
        for (int i = 0; i < tt.size(); i++) {
            tt.set(i, ColumnText.duplicate(tt.get(i)));
        }
    }
    // clear layout table
    clearTable(layoutTable, true);
    setWidthIfNecessary();

    // try to fill cells with text
    ColumnText t = tt.get(0);
    for (PdfPCell cell : layoutTable.getRow(0).getCells()) {
        cell.setFixedHeight(height >= 0.0f ? height : -1.0f);
        cell.setColumn(ColumnText.duplicate(t));
        //
        t.setSimpleColumn(cell.getLeft() + cell.getPaddingLeft(),
                height >= 0.0f ? -height : PdfPRow.BOTTOM_LIMIT, cell.getRight() - cell.getPaddingRight(), 0);
        int res = 0;
        try {
            res = t.go(true);
        } catch (DocumentException e) {
            throw new ODFConverterException(e);
        }
        if (!ColumnText.hasMoreText(res)) {
            // no overflow in current column
            if (tt.size() == 1) {
                // no more text
                return null;
            } else {
                // some text waiting for new column
                tt.remove(0);
                t = tt.get(0);
            }
        }
    }
    return tt;
}

From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.elements.StylableDocumentSection.java

License:Open Source License

private List<ColumnText> fillTable(float height) {
    // copy text for simulation
    List<ColumnText> tt = null;
    if (breakHandlingParent == null && colIdx >= layoutTable.getNumberOfColumns()) {
        // more column breaks than available column
        // we try not to lose content
        // but results may be different than in open office
        // anyway it is logical error made by document creator
        tt = new ArrayList<ColumnText>();
        ColumnText t = createColumnText();
        tt.add(t);/*  w  ww . j a v  a 2 s  . co  m*/
        for (int i = 0; i < texts.size(); i++) {
            PdfPTable table = new PdfPTable(1);
            table.setWidthPercentage(100.0f);
            PdfPCell cell = new PdfPCell();
            cell.setBorder(Table.NO_BORDER);
            cell.setPadding(0.0f);
            cell.setColumn(ColumnText.duplicate(texts.get(i)));
            table.addCell(cell);
            t.addElement(table);
        }
    } else {
        tt = new ArrayList<ColumnText>(texts);
        for (int i = 0; i < tt.size(); i++) {
            tt.set(i, ColumnText.duplicate(tt.get(i)));
        }
    }
    // clear layout table
    clearTable(layoutTable, true);
    setWidthIfNecessary();

    // try to fill cells with text
    ColumnText t = tt.get(0);
    for (PdfPCell cell : layoutTable.getRow(0).getCells()) {
        cell.setFixedHeight(height >= 0.0f ? height : -1.0f);
        cell.setColumn(ColumnText.duplicate(t));
        //
        t.setSimpleColumn(cell.getLeft() + cell.getPaddingLeft(),
                height >= 0.0f ? -height : PdfPRow.BOTTOM_LIMIT, cell.getRight() - cell.getPaddingRight(), 0);
        int res = 0;
        try {
            res = t.go(true);
        } catch (DocumentException e) {
            throw new XWPFConverterException(e);
        }
        if (!ColumnText.hasMoreText(res)) {
            // no overflow in current column
            if (tt.size() == 1) {
                // no more text
                return null;
            } else {
                // some text waiting for new column
                tt.remove(0);
                t = tt.get(0);
            }
        }
    }
    return tt;
}

From source file:it.eng.spagobi.engines.worksheet.exporter.WorkSheetPDFExporter.java

License:Mozilla Public License

public void addHtmlToPdfContentByte(String html, float[] pos) {

    PdfContentByte cb = docWriter.getDirectContent();
    StyleSheet styles = createDefaultStyleSheet();

    ColumnText ct = new ColumnText(cb);
    ct.setSimpleColumn(pos[0], pos[1], pos[2], pos[3]);
    ct.setYLine(pos[3]);/*from ww w .jav a2s  . c o  m*/
    try {
        ArrayList htmlObjs = HTMLWorker.parseToList(new StringReader(html), styles);
        for (int k = 0; k < htmlObjs.size(); ++k) {
            ct.addElement((Element) htmlObjs.get(k));
        }
        ct.go();
    } catch (Exception e) {
        throw new RuntimeException("Could not parse HTML", e);
    }
}