Example usage for com.lowagie.text.pdf PdfWriter RUN_DIRECTION_RTL

List of usage examples for com.lowagie.text.pdf PdfWriter RUN_DIRECTION_RTL

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter RUN_DIRECTION_RTL.

Prototype

int RUN_DIRECTION_RTL

To view the source code for com.lowagie.text.pdf PdfWriter RUN_DIRECTION_RTL.

Click Source Link

Document

Use bidirectional reordering with right-to-left preferential run direction.

Usage

From source file:com.jk.framework.desktop.swing.dao.TableModelPdfBuilder.java

License:Apache License

/**
 *
 * @return
 */
private int getRunDirection() {
    return isRtl() ? PdfWriter.RUN_DIRECTION_RTL : PdfWriter.RUN_DIRECTION_LTR;
}

From source file:com.jk.framework.pdf.JKPdfPTable.java

License:Apache License

/**
 * Instantiates a new JK pdf P table.//from   www  .  j a  va2 s.c  om
 *
 * @param col
 *            the col
 * @param rtl
 *            the rtl
 */
public JKPdfPTable(final int col, final boolean rtl) {
    super(col);
    setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
}

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

License:Apache License

/**
 * ?.????.//from  w w w .ja v a2  s.c om
 * <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:net.sf.jasperreports.engine.export.JRPdfExporter.java

License:LGPL

/**
 *
 *//*from   w  w  w  . j  a v a  2 s  .c  om*/
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.getLineBox().getTopPadding().intValue();
    int leftPadding = text.getLineBox().getLeftPadding().intValue();
    int bottomPadding = text.getLineBox().getBottomPadding().intValue();
    int rightPadding = text.getLineBox().getRightPadding().intValue();

    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.setRGBColorFill(backcolor.getRed(), backcolor.getGreen(), backcolor.getBlue());
        pdfContentByte.rectangle(x + xFillCorrection, jasperPrint.getPageHeight() - y + yFillCorrection, width,
                -height);
        pdfContentByte.fill();
    }

    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;
        }
        }

        ColumnText colText = new ColumnText(pdfContentByte);
        colText.setSimpleColumn(getPhrase(styledText, text), x + leftPadding,
                jasperPrint.getPageHeight() - y - topPadding - verticalOffset - text.getLeadingOffset(),
                //+ text.getLineSpacingFactor() * text.getFont().getSize(),
                x + width - rightPadding, jasperPrint.getPageHeight() - y - height + bottomPadding, 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);

        colText.go();
    }

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

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

From source file:net.sf.jasperreports.engine.export.PdfTextRenderer.java

License:Open Source License

/**
 * /* w  w  w.j  a  v  a  2 s .co  m*/
 */
public void draw() {
    TabSegment segment = segments.get(segmentIndex);

    float advance = segment.layout.getAdvance();

    ColumnText colText = new ColumnText(pdfContentByte);
    colText.setSimpleColumn(pdfExporter.getPhrase(segment.as, segment.text, text),
            x + drawPosX + leftOffsetFactor * advance, // + leftPadding
            pdfExporter.getCurrentJasperPrint().getPageHeight() - y - topPadding - verticalAlignOffset
            //- text.getLeadingOffset()
                    + lineHeight - drawPosY,
            x + drawPosX + segment.layout.getAdvance() + rightOffsetFactor * advance, // + leftPadding
            pdfExporter.getCurrentJasperPrint().getPageHeight() - y - topPadding - verticalAlignOffset
            //- text.getLeadingOffset()
                    - 400//+ lineHeight//FIXMETAB
                    - drawPosY,
            0, //text.getLineSpacingFactor(),// * text.getFont().getSize(),
            horizontalAlignment);

    //colText.setLeading(0, text.getLineSpacingFactor());// * text.getFont().getSize());
    colText.setLeading(lineHeight);
    colText.setRunDirection(text.getRunDirectionValue() == RunDirectionEnum.LTR ? PdfWriter.RUN_DIRECTION_LTR
            : PdfWriter.RUN_DIRECTION_RTL);

    try {
        colText.go();
    } catch (DocumentException e) {
        throw new JRRuntimeException(e);
    }
}

From source file:net.sf.jasperreports.engine.export.SimplePdfTextRenderer.java

License:Open Source License

/**
 * /*  www .jav a  2s.c  o m*/
 */
public void render() {
    ColumnText colText = new ColumnText(pdfContentByte);
    colText.setSimpleColumn(getPhrase(styledText, text), x + leftPadding,
            pdfExporter.getCurrentJasperPrint().getPageHeight() - y - topPadding - verticalAlignOffset
                    - text.getLeadingOffset(),
            //+ text.getLineSpacingFactor() * text.getFont().getSize(),
            x + width - rightPadding,
            pdfExporter.getCurrentJasperPrint().getPageHeight() - y - height + bottomPadding, 0, //text.getLineSpacingFactor(),// * text.getFont().getSize(),
            horizontalAlignment == Element.ALIGN_JUSTIFIED_ALL ? Element.ALIGN_JUSTIFIED : horizontalAlignment);

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

    try {
        colText.go();
    } catch (DocumentException e) {
        throw new JRRuntimeException(e);
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfLogicalPageDrawable.java

License:Open Source License

private float computeLeadingSafetyMargin(final RenderableComplexText node) {
    if (paragraphContext == null) {
        return -SAFETY_MARGIN;
    }//from  ww w  .j  a v a  2s .com

    ElementAlignment alignment;
    if (node.getNext() == null) {
        alignment = paragraphContext.getLastLineAlignment();
    } else {
        alignment = paragraphContext.getTextAlignment();
    }
    if (ElementAlignment.LEFT.equals(alignment)) {
        return 0;
    } else if (ElementAlignment.RIGHT.equals(alignment)) {
        return -SAFETY_MARGIN * 2;
    } else if (ElementAlignment.CENTER.equals(alignment)) {
        return -SAFETY_MARGIN;
    } else {
        if (computeRunDirection(node) == PdfWriter.RUN_DIRECTION_RTL) {
            return -SAFETY_MARGIN * 2;
        } else {
            return 0f;
        }
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfLogicalPageDrawable.java

License:Open Source License

private float computeTrailingSafetyMargin(final RenderableComplexText node) {
    if (paragraphContext == null) {
        return SAFETY_MARGIN;
    }//from  ww w  . j  a  v  a  2 s .c  om

    ElementAlignment alignment;
    if (node.getNext() == null) {
        alignment = paragraphContext.getLastLineAlignment();
    } else {
        alignment = paragraphContext.getTextAlignment();
    }
    if (ElementAlignment.LEFT.equals(alignment)) {
        return SAFETY_MARGIN * 2;
    } else if (ElementAlignment.RIGHT.equals(alignment)) {
        return 0;
    } else if (ElementAlignment.CENTER.equals(alignment)) {
        return SAFETY_MARGIN;
    } else {
        if (computeRunDirection(node) == PdfWriter.RUN_DIRECTION_RTL) {
            return 0f;
        } else {
            return SAFETY_MARGIN * 2;
        }
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfLogicalPageDrawable.java

License:Open Source License

private int computeRunDirection(final RenderableComplexText node) {
    Object styleProperty = node.getStyleSheet().getStyleProperty(TextStyleKeys.DIRECTION, TextDirection.LTR);
    if (TextDirection.RTL.equals(styleProperty)) {
        return PdfWriter.RUN_DIRECTION_RTL;
    } else {//from   w  w w . java2  s .c om
        return PdfWriter.RUN_DIRECTION_LTR;
    }
}

From source file:questions.graphics2D.ArabicText.java

public static void main(String[] args) {
    Document document = new Document(PageSize.A4);
    try {/*from w w w. j a  va 2s.co  m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        String text1 = "\u0634\u0627\u062f\u062c\u0645\u0647\u0648\u0631";
        String text2 = "\u0634";
        java.awt.Font font = new java.awt.Font("arial", 0, 12);
        PdfContentByte cb = writer.getDirectContent();

        java.awt.Graphics2D g2Shapes = cb.createGraphicsShapes(PageSize.A4.getWidth(), PageSize.A4.getHeight());
        g2Shapes.setFont(font);
        g2Shapes.drawString("text1, expected to render RTL", 50, 100);
        g2Shapes.drawString(text1, 50, 120);
        g2Shapes.drawString("text2, expected to match right-most glyph above", 50, 140);
        g2Shapes.drawString(text2, 50, 160);
        g2Shapes.dispose();

        ColumnText text = new ColumnText(cb);
        Font f = new Font(
                BaseFont.createFont("c://windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED),
                12);
        text.setSimpleColumn(50, 620, 545, 50);
        text.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
        text.setText(new Phrase(text1, f));
        text.go();
        text.setText(new Phrase(text2, f));
        text.go();

        FontMapper arialuni = new FontMapper() {
            public BaseFont awtToPdf(java.awt.Font font) {
                try {
                    return BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H,
                            BaseFont.EMBEDDED);
                } catch (DocumentException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            public java.awt.Font pdfToAwt(BaseFont font, int size) {
                return null;
            }

        };
        java.awt.Graphics2D g = cb.createGraphics(PageSize.A4.getWidth(), PageSize.A4.getHeight(), arialuni);
        g.setFont(null);
        g.drawString("text1, not expected to render RTL", 50, 180);
        g.drawString(text1, 50, 200);
        g.drawString("text2, not expected to match right-most glyph above", 50, 220);
        g.drawString(text2, 50, 240);
        g.drawString("to your right you see what it SHOULD look like:", 50, 260);
        g.drawString("If it doesn't, the problem is in the JDK, it's not an iText problem.", 50, 280);
        g.dispose();
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}