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

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

Introduction

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

Prototype

public void setYLine(float yLine) 

Source Link

Document

Sets the yLine.

Usage

From source file:classroom.filmfestival_b.Movies09.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    // step 1//from www.  ja  v  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/*from w  w w  .  j  a v a  2  s  .co 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 a v  a2  s  .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.newspaper_a.Newspaper04.java

public static void putText(PdfContentByte canvas, Phrase phrase, float llx, float lly, float urx, float ury)
        throws DocumentException {
    ColumnText column = new ColumnText(canvas);
    column.setAlignment(Element.ALIGN_CENTER);
    column.setSimpleColumn(llx, lly, urx, ury);
    column.setText(phrase);//from   w ww. ja  va  2  s .co m
    column.go(true);
    float offset = (column.getYLine() - lly) / 2f;
    column.setText(phrase);
    column.setYLine(ury - offset);
    column.go();
}

From source file:classroom.newspaper_a.Newspaper05.java

public static void putText(PdfContentByte canvas, Phrase p, float llx, float lly, float w, float h)
        throws DocumentException {
    PdfTemplate template = canvas.createTemplate(h, w);
    ColumnText column = new ColumnText(template);
    column.setSimpleColumn(0, 0, h, w);/*  w w w.  j  a v a 2 s.c  o m*/
    column.setAlignment(Element.ALIGN_CENTER);
    column.setText(p);
    column.go(true);
    float offset = w - (column.getYLine() / 2f);
    column.setText(p);
    column.setYLine(offset);
    column.go();
    canvas.addTemplate(template, 0, 1, -1, 0, llx + w, lly);
}

From source file:corner.orm.tapestry.jasper.exporter.CornerPdfExporter.java

License:Apache License

/**
 * ?.????./* ww w  .  j av  a2  s. c  o m*/
 * <p>:ColumnText?,
 * ?NO_MORE_COLUMN???,TextField??.
 * TextField?.
 * @see net.sf.jasperreports.engine.export.JRPdfExporter#exportText(net.sf.jasperreports.engine.JRPrintText)
 */
@Override
protected void exportText(JRPrintText text) throws DocumentException {

    JRStyledText styledText = getStyledText(text, false);

    if (styledText == null) {
        return;
    }

    int textLength = styledText.length();

    int x = text.getX() + getOffsetX();
    int y = text.getY() + getOffsetY();
    int width = text.getWidth();
    int height = text.getHeight();
    int topPadding = text.getTopPadding();
    int leftPadding = text.getLeftPadding();
    int bottomPadding = text.getBottomPadding();
    int rightPadding = text.getRightPadding();

    int xFillCorrection = 0;
    int yFillCorrection = 0;

    double angle = 0;

    switch (text.getRotation()) {
    case JRTextElement.ROTATION_LEFT: {
        y = text.getY() + getOffsetY() + text.getHeight();
        xFillCorrection = 1;
        width = text.getHeight();
        height = text.getWidth();
        int tmpPadding = topPadding;
        topPadding = leftPadding;
        leftPadding = bottomPadding;
        bottomPadding = rightPadding;
        rightPadding = tmpPadding;
        angle = Math.PI / 2;
        break;
    }
    case JRTextElement.ROTATION_RIGHT: {
        x = text.getX() + getOffsetX() + text.getWidth();
        yFillCorrection = -1;
        width = text.getHeight();
        height = text.getWidth();
        int tmpPadding = topPadding;
        topPadding = rightPadding;
        rightPadding = bottomPadding;
        bottomPadding = leftPadding;
        leftPadding = tmpPadding;
        angle = -Math.PI / 2;
        break;
    }
    case JRTextElement.ROTATION_UPSIDE_DOWN: {
        x = text.getX() + getOffsetX() + text.getWidth();
        y = text.getY() + getOffsetY() + text.getHeight();
        int tmpPadding = topPadding;
        topPadding = bottomPadding;
        bottomPadding = tmpPadding;
        tmpPadding = leftPadding;
        leftPadding = rightPadding;
        rightPadding = tmpPadding;
        angle = Math.PI;
        break;
    }
    case JRTextElement.ROTATION_NONE:
    default: {
    }
    }

    AffineTransform atrans = new AffineTransform();
    atrans.rotate(angle, x, jasperPrint.getPageHeight() - y);
    pdfContentByte.transform(atrans);

    if (text.getMode() == JRElement.MODE_OPAQUE) {
        Color backcolor = text.getBackcolor();
        pdfContentByte.setRGBColorStroke(backcolor.getRed(), backcolor.getGreen(), backcolor.getBlue());
        pdfContentByte.setRGBColorFill(backcolor.getRed(), backcolor.getGreen(), backcolor.getBlue());
        pdfContentByte.setLineWidth(1f);
        pdfContentByte.setLineDash(0f);
        pdfContentByte.rectangle(x + xFillCorrection, jasperPrint.getPageHeight() - y + yFillCorrection,
                width - 1, -height + 1);
        pdfContentByte.fillStroke();
    } else {
        /*
         * pdfContentByte.setRGBColorStroke( text.getForecolor().getRed(),
         * text.getForecolor().getGreen(), text.getForecolor().getBlue() );
         * pdfContentByte.setLineWidth(0.1f);
         * pdfContentByte.setLineDash(0f); pdfContentByte.rectangle(
         * text.getX() + offsetX, jasperPrint.getPageHeight() - text.getY() -
         * offsetY, text.getWidth(), - text.getHeight() );
         * pdfContentByte.stroke();
         */
    }

    if (textLength > 0) {
        int horizontalAlignment = Element.ALIGN_LEFT;
        switch (text.getHorizontalAlignment()) {
        case JRAlignment.HORIZONTAL_ALIGN_LEFT: {
            if (text.getRunDirection() == JRPrintText.RUN_DIRECTION_LTR) {
                horizontalAlignment = Element.ALIGN_LEFT;
            } else {
                horizontalAlignment = Element.ALIGN_RIGHT;
            }
            break;
        }
        case JRAlignment.HORIZONTAL_ALIGN_CENTER: {
            horizontalAlignment = Element.ALIGN_CENTER;
            break;
        }
        case JRAlignment.HORIZONTAL_ALIGN_RIGHT: {
            if (text.getRunDirection() == JRPrintText.RUN_DIRECTION_LTR) {
                horizontalAlignment = Element.ALIGN_RIGHT;
            } else {
                horizontalAlignment = Element.ALIGN_LEFT;
            }
            break;
        }
        case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED: {
            horizontalAlignment = Element.ALIGN_JUSTIFIED;
            break;
        }
        default: {
            horizontalAlignment = Element.ALIGN_LEFT;
        }
        }

        float verticalOffset = 0f;
        switch (text.getVerticalAlignment()) {
        case JRAlignment.VERTICAL_ALIGN_TOP: {
            verticalOffset = 0f;
            break;
        }
        case JRAlignment.VERTICAL_ALIGN_MIDDLE: {
            verticalOffset = (height - topPadding - bottomPadding - text.getTextHeight()) / 2f;
            break;
        }
        case JRAlignment.VERTICAL_ALIGN_BOTTOM: {
            verticalOffset = height - topPadding - bottomPadding - text.getTextHeight();
            break;
        }
        default: {
            verticalOffset = 0f;
        }
        }

        float llx = x + leftPadding;
        float lly = jasperPrint.getPageHeight() - y - topPadding - verticalOffset - text.getLeadingOffset();
        float urx = x + width - rightPadding;
        float ury = jasperPrint.getPageHeight() - y - height + bottomPadding;

        //?,???
        if (this.jasperMoveXY != null) {
            llx = x + leftPadding + jasperMoveXY.getX();
            lly = jasperPrint.getPageHeight() - y - topPadding - verticalOffset - text.getLeadingOffset()
                    - jasperMoveXY.getY();
            urx = x + width - rightPadding + jasperMoveXY.getX();
            ury = jasperPrint.getPageHeight() - y - height + bottomPadding - jasperMoveXY.getY();
        }

        boolean isOver = false;
        int status = ColumnText.START_COLUMN;
        Phrase phrase = getPhrase(styledText, text);

        ColumnText colText = new ColumnText(pdfContentByte);
        colText.setSimpleColumn(phrase, llx, lly, urx, ury, 0, // text.getLineSpacingFactor(),//
                // *
                // text.getFont().getSize(),
                horizontalAlignment);

        colText.setLeading(0, text.getLineSpacingFactor());// *
        // text.getFont().getSize());
        colText.setRunDirection(
                text.getRunDirection() == JRPrintText.RUN_DIRECTION_LTR ? PdfWriter.RUN_DIRECTION_LTR
                        : PdfWriter.RUN_DIRECTION_RTL);

        float yLine = colText.getYLine();

        // ColumnText
        while (colText.hasMoreText(status)) {
            status = colText.go(true);
            colText.setYLine(yLine);

            // ??,true
            if (status == ColumnText.NO_MORE_COLUMN) {
                isOver = true;
                break;
            }
        }

        // ,ColumnText
        if (!isOver) {
            colText.setText(phrase);
            status = ColumnText.START_COLUMN;

            while (colText.hasMoreText(status)) {
                status = colText.go();
                colText.setYLine(yLine);
            }

        } else {
            // TextField,??
            String key = text.getKey();// PdfTextid
            if (alreadyExistFields.contains(key)) { // ?
                key = createUniqueName();
            }
            alreadyExistFields.add(key);
            TextField tf = new TextField(pdfContentByte.getPdfWriter(), new Rectangle(llx, lly, urx, ury), key);
            tf.setAlignment(horizontalAlignment);
            tf.setText(text.getText());
            tf.setFont(PdfUtils.createSongLightBaseFont());

            // styledText ??,?
            if (!text.isStyledText()) {
                tf.setOptions(TextField.MULTILINE);
            }

            try {
                pdfContentByte.getPdfWriter().addAnnotation(tf.getTextField());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    atrans = new AffineTransform();
    atrans.rotate(-angle, x, jasperPrint.getPageHeight() - y);
    pdfContentByte.transform(atrans);

    /*   */
    exportBox(text, text);
}

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;
    }/*  w  w  w . j  av  a 2 s  .  com*/
    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: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]);
    try {/*from   w ww. j  a  v  a 2 s  .  c o m*/
        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);
    }
}

From source file:questions.objects.NewPageColumns.java

public static void main(String[] args) {

    // step 1/* w  ww  . j  a  va  2 s  .  com*/
    Document document = new Document(PageSize.A6);
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        // step 3
        document.open();
        // step 4
        for (int i = 0; i < 25; i++)
            document.add(new Paragraph("Hello paragraph 1." + i));
        document.newPage();
        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(PageSize.A6.getLeft(36), PageSize.A6.getBottom(36), PageSize.A6.getRight(36),
                PageSize.A6.getTop(36));
        for (int i = 0; i < 20; i++)
            column.addElement(new Paragraph("Hello column 1." + i));
        int status = column.go();
        while (ColumnText.hasMoreText(status)) {
            document.newPage();
            column.setYLine(PageSize.A6.getTop(36));
            status = column.go();
        }
        document.newPage();
        for (int i = 0; i < 10; i++)
            column.addElement(new Paragraph("Hello column 2." + i));
        status = column.go();
        while (ColumnText.hasMoreText(status)) {
            document.newPage();
            column.setYLine(PageSize.A6.getTop(36));
            status = column.go();
        }
        document.newPage();
        for (int i = 0; i < 5; i++)
            document.add(new Paragraph("Hello paragraph 2." + i));
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
}

From source file:questions.stamppages.StampAndAddColumns.java

public static void main(String[] args) {
    try {//from  ww  w  .ja  va  2s  . c  o m
        PdfReader reader = new PdfReader(RESOURCE);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        AcroFields form = stamper.getAcroFields();
        form.setField("Who", "world");
        ColumnText ct = new ColumnText(stamper.getOverContent(1));
        String line;
        Phrase p;
        BufferedReader br = new BufferedReader(new FileReader(TXT));
        while ((line = br.readLine()) != null) {
            p = new Phrase(line + "\n");
            ct.addText(p);
        }
        int status = ColumnText.START_COLUMN;
        ct.setSimpleColumn(100, 700, 495, 100);
        status = ct.go();
        int pageCt = 1;
        while (ColumnText.hasMoreText(status)) {
            stamper.insertPage(++pageCt, reader.getPageSize(1));
            ct.setYLine(700);
            ct.setCanvas(stamper.getOverContent(pageCt));
            status = ct.go();
        }
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}