Example usage for com.lowagie.text Phrase Phrase

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

Introduction

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

Prototype

private Phrase(boolean dummy) 

Source Link

Document

Constructs a Phrase that can be used in the static getInstance() method.

Usage

From source file:net.bull.javamelody.internal.web.pdf.PdfDocumentFactory.java

License:Apache License

private void createWriter(Document document, String title) throws DocumentException, IOException {
    final PdfWriter writer = PdfWriter.getInstance(document, output);
    //writer.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);

    // title/* w  w w  . j a v  a 2  s .c  om*/
    final HeaderFooter header = new HeaderFooter(new Phrase(title), false);
    header.setAlignment(Element.ALIGN_LEFT);
    header.setBorder(Rectangle.NO_BORDER);
    document.setHeader(header);

    // simple page numbers : x
    //HeaderFooter footer = new HeaderFooter(new Phrase(), true);
    //footer.setAlignment(Element.ALIGN_RIGHT);
    //footer.setBorder(Rectangle.TOP);
    //document.setFooter(footer);

    // add the event handler for advanced page numbers : x/y
    writer.setPageEvent(new PdfAdvancedPageNumberEvents());
}

From source file:net.bull.javamelody.internal.web.pdf.PdfSessionInformationsReport.java

License:Apache License

private void writeCountry(SessionInformations session) throws IOException, BadElementException {
    final String country = session.getCountry();
    if (country == null) {
        addCell("");
    } else {//from  www .j ava  2s  . c  o m
        final String fileName = "flags/" + country + ".gif";
        final Image image = getImageByFileName(fileName);
        if (image == null) {
            addCell(country);
        } else {
            addCell(new Phrase(new Chunk(image, 0, 0)));
        }
    }
}

From source file:net.bull.javamelody.internal.web.pdf.PdfSessionInformationsReport.java

License:Apache License

private void writeBrowserAndOs(SessionInformations session) throws IOException, BadElementException {
    final String browser = session.getBrowser();
    if (browser == null) {
        addCell("");
    } else {/*from   www  . ja  v  a 2s  .  c o m*/
        final String browserIconName = HtmlSessionInformationsReport.getBrowserIconName(browser);
        final String fileName = "browsers/" + browserIconName;
        final Image image = getImageByFileName(fileName);
        if (image == null) {
            addCell(browser);
        } else {
            addCell(new Phrase(new Chunk(image, 0, 0)));
        }
    }

    final String os = session.getOs();
    if (os == null) {
        addCell("");
    } else {
        final String osIconName = HtmlSessionInformationsReport.getOSIconName(os);
        final String fileName = "servers/" + osIconName;
        final Image image = getImageByFileName(fileName);
        if (image == null) {
            addCell(os);
        } else {
            addCell(new Phrase(new Chunk(image, 0, 0)));
        }
    }
}

From source file:net.bull.javamelody.PdfCoreReport.java

License:Apache License

private void writeGraphs(Collection<JRobin> jrobins, Map<String, byte[]> mySmallGraphs)
        throws IOException, DocumentException {
    if (collector.isStopped()) {
        // pas de graphs, ils seraient en erreur sans timer
        // mais un message d'avertissement  la place
        final String message = getString("collect_server_misusage");
        final Paragraph jrobinParagraph = new Paragraph(message, PdfFonts.BOLD.getFont());
        jrobinParagraph.setAlignment(Element.ALIGN_CENTER);
        addToDocument(jrobinParagraph);/*from  ww  w.ja va 2  s  .c  o  m*/
        return;
    }
    final Paragraph jrobinParagraph = new Paragraph("",
            FontFactory.getFont(FontFactory.HELVETICA, 9f, Font.NORMAL));
    jrobinParagraph.setAlignment(Element.ALIGN_CENTER);
    jrobinParagraph.add(new Phrase("\n\n\n\n"));
    int i = 0;
    if (mySmallGraphs != null) {
        // si les graphiques ont t prinitialiss (en Swing) alors on les utilise
        for (final byte[] imageData : mySmallGraphs.values()) {
            if (i % 3 == 0 && i != 0) {
                // un retour aprs httpSessions et avant activeThreads pour l'alignement
                jrobinParagraph.add(new Phrase("\n\n\n\n\n"));
            }
            final Image image = Image.getInstance(imageData);
            image.scalePercent(50);
            jrobinParagraph.add(new Phrase(new Chunk(image, 0, 0)));
            jrobinParagraph.add(new Phrase(" "));
            i++;
        }
    } else {
        if (jrobins.isEmpty()) {
            return;
        }
        for (final JRobin jrobin : jrobins) {
            if (i % 3 == 0 && i != 0) {
                // un retour aprs httpSessions et avant activeThreads pour l'alignement
                jrobinParagraph.add(new Phrase("\n\n\n\n\n"));
            }
            final Image image = Image.getInstance(jrobin.graph(range, SMALL_GRAPH_WIDTH, SMALL_GRAPH_HEIGHT));
            image.scalePercent(50);
            jrobinParagraph.add(new Phrase(new Chunk(image, 0, 0)));
            jrobinParagraph.add(new Phrase(" "));
            i++;
        }
    }
    jrobinParagraph.add(new Phrase("\n"));
    addToDocument(jrobinParagraph);
}

From source file:net.bull.javamelody.PdfSessionInformationsReport.java

License:Apache License

private void writeCountry(SessionInformations session) throws IOException, BadElementException {
    final String country = session.getCountry();
    if (country == null) {
        addCell("");
    } else {/*from  w  ww . ja  v  a2s .  c  om*/
        final Image image = getCountryImage(country);
        if (image == null) {
            addCell(country);
        } else {
            addCell(new Phrase(new Chunk(image, 0, 0)));
        }
    }
}

From source file:net.bull.javamelody.swing.print.MPdfWriter.java

License:Apache License

/**
 * We create a writer that listens to the document and directs a PDF-stream to out
 *
 * @param table/* w ww  . ja v  a 2 s .  c o  m*/
 *           MBasicTable
 * @param document
 *           Document
 * @param out
 *           OutputStream
 * @return DocWriter
 * @throws DocumentException
 *            e
 */
protected DocWriter createWriter(final MBasicTable table, final Document document, final OutputStream out)
        throws DocumentException {
    final PdfWriter writer = PdfWriter.getInstance(document, out);
    // writer.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);

    // title
    if (table.getName() != null) {
        final HeaderFooter header = new HeaderFooter(new Phrase(table.getName()), false);
        header.setAlignment(Element.ALIGN_LEFT);
        header.setBorder(Rectangle.NO_BORDER);
        document.setHeader(header);
        document.addTitle(table.getName());
    }

    // simple page numbers : x
    // HeaderFooter footer = new HeaderFooter(new Phrase(), true);
    // footer.setAlignment(Element.ALIGN_RIGHT);
    // footer.setBorder(Rectangle.TOP);
    // document.setFooter(footer);

    // add the event handler for advanced page numbers : x/y
    writer.setPageEvent(new AdvancedPageNumberEvents());

    return writer;
}

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

License:LGPL

protected void writePageAnchor(int pageIndex) throws DocumentException {
    Map pdfFontAttrs = getDefaultPdfFontAttributes();
    Chunk chunk;/* w ww.j a v  a  2  s .c  o m*/
    if (pdfFontAttrs == null) {
        chunk = new Chunk(" ");
    } else {
        Font pdfFont = getFont(pdfFontAttrs);
        chunk = new Chunk(" ", pdfFont);
    }

    chunk.setLocalDestination(JR_PAGE_ANCHOR_PREFIX + reportIndex + "_" + (pageIndex + 1));

    ColumnText colText = new ColumnText(pdfContentByte);
    colText.setSimpleColumn(new Phrase(chunk), 0, jasperPrint.getPageHeight(), 1, 1, 0, Element.ALIGN_LEFT);

    colText.go();
}

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

License:LGPL

/**
 *
 *///from  ww w .j a  v  a  2  s .c  om
protected void exportImage(JRPrintImage printImage) throws DocumentException, IOException, JRException {
    if (printImage.getMode() == JRElement.MODE_OPAQUE) {
        pdfContentByte.setRGBColorFill(printImage.getBackcolor().getRed(), printImage.getBackcolor().getGreen(),
                printImage.getBackcolor().getBlue());
        pdfContentByte.rectangle(printImage.getX() + getOffsetX(),
                jasperPrint.getPageHeight() - printImage.getY() - getOffsetY(), printImage.getWidth(),
                -printImage.getHeight());
        pdfContentByte.fill();
    }

    int topPadding = printImage.getLineBox().getTopPadding().intValue();
    int leftPadding = printImage.getLineBox().getLeftPadding().intValue();
    int bottomPadding = printImage.getLineBox().getBottomPadding().intValue();
    int rightPadding = printImage.getLineBox().getRightPadding().intValue();

    int availableImageWidth = printImage.getWidth() - leftPadding - rightPadding;
    availableImageWidth = (availableImageWidth < 0) ? 0 : availableImageWidth;

    int availableImageHeight = printImage.getHeight() - topPadding - bottomPadding;
    availableImageHeight = (availableImageHeight < 0) ? 0 : availableImageHeight;

    JRRenderable renderer = printImage.getRenderer();

    if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) {
        if (renderer.getType() == JRRenderable.TYPE_IMAGE) {
            // Image renderers are all asked for their image data at some point. 
            // Better to test and replace the renderer now, in case of lazy load error.
            renderer = JRImageRenderer.getOnErrorRendererForImageData(renderer, printImage.getOnErrorType());
        }
    } else {
        renderer = null;
    }

    if (renderer != null) {
        int xoffset = 0;
        int yoffset = 0;

        Chunk chunk = null;

        float scaledWidth = availableImageWidth;
        float scaledHeight = availableImageHeight;

        if (renderer.getType() == JRRenderable.TYPE_IMAGE) {
            com.lowagie.text.Image image = null;

            float xalignFactor = getXAlignFactor(printImage);
            float yalignFactor = getYAlignFactor(printImage);

            switch (printImage.getScaleImage()) {
            case JRImage.SCALE_IMAGE_CLIP: {
                // Image load might fail, from given image data. 
                // Better to test and replace the renderer now, in case of lazy load error.
                renderer = JRImageRenderer.getOnErrorRendererForDimension(renderer,
                        printImage.getOnErrorType());
                if (renderer == null) {
                    break;
                }

                int normalWidth = availableImageWidth;
                int normalHeight = availableImageHeight;

                Dimension2D dimension = renderer.getDimension();
                if (dimension != null) {
                    normalWidth = (int) dimension.getWidth();
                    normalHeight = (int) dimension.getHeight();
                }

                xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
                yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));

                int minWidth = Math.min(normalWidth, availableImageWidth);
                int minHeight = Math.min(normalHeight, availableImageHeight);

                BufferedImage bi = new BufferedImage(minWidth, minHeight, BufferedImage.TYPE_INT_ARGB);

                Graphics2D g = bi.createGraphics();
                if (printImage.getMode() == JRElement.MODE_OPAQUE) {
                    g.setColor(printImage.getBackcolor());
                    g.fillRect(0, 0, minWidth, minHeight);
                }
                renderer.render(g, new java.awt.Rectangle((xoffset > 0 ? 0 : xoffset),
                        (yoffset > 0 ? 0 : yoffset), normalWidth, normalHeight));
                g.dispose();

                xoffset = (xoffset < 0 ? 0 : xoffset);
                yoffset = (yoffset < 0 ? 0 : yoffset);

                //awtImage = bi.getSubimage(0, 0, minWidth, minHeight);

                //image = com.lowagie.text.Image.getInstance(awtImage, printImage.getBackcolor());
                image = com.lowagie.text.Image.getInstance(bi, null);

                break;
            }
            case JRImage.SCALE_IMAGE_FILL_FRAME: {
                if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer)) {
                    image = (com.lowagie.text.Image) loadedImagesMap.get(renderer);
                } else {
                    try {
                        image = com.lowagie.text.Image.getInstance(renderer.getImageData());
                        imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
                    } catch (Exception e) {
                        JRImageRenderer tmpRenderer = JRImageRenderer.getOnErrorRendererForImage(
                                JRImageRenderer.getInstance(renderer.getImageData()),
                                printImage.getOnErrorType());
                        if (tmpRenderer == null) {
                            break;
                        }
                        java.awt.Image awtImage = tmpRenderer.getImage();
                        image = com.lowagie.text.Image.getInstance(awtImage, null);
                    }

                    if (printImage.isUsingCache()) {
                        loadedImagesMap.put(renderer, image);
                    }
                }

                image.scaleAbsolute(availableImageWidth, availableImageHeight);
                break;
            }
            case JRImage.SCALE_IMAGE_RETAIN_SHAPE:
            default: {
                if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer)) {
                    image = (com.lowagie.text.Image) loadedImagesMap.get(renderer);
                } else {
                    try {
                        image = com.lowagie.text.Image.getInstance(renderer.getImageData());
                        imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0);
                    } catch (Exception e) {
                        JRImageRenderer tmpRenderer = JRImageRenderer.getOnErrorRendererForImage(
                                JRImageRenderer.getInstance(renderer.getImageData()),
                                printImage.getOnErrorType());
                        if (tmpRenderer == null) {
                            break;
                        }
                        java.awt.Image awtImage = tmpRenderer.getImage();
                        image = com.lowagie.text.Image.getInstance(awtImage, null);
                    }

                    if (printImage.isUsingCache()) {
                        loadedImagesMap.put(renderer, image);
                    }
                }

                image.scaleToFit(availableImageWidth, availableImageHeight);

                xoffset = (int) (xalignFactor * (availableImageWidth - image.plainWidth()));
                yoffset = (int) (yalignFactor * (availableImageHeight - image.plainHeight()));

                xoffset = (xoffset < 0 ? 0 : xoffset);
                yoffset = (yoffset < 0 ? 0 : yoffset);

                break;
            }
            }

            if (image != null) {
                chunk = new Chunk(image, 0, 0);

                scaledWidth = image.scaledWidth();
                scaledHeight = image.scaledHeight();
            }
        } else {
            double normalWidth = availableImageWidth;
            double normalHeight = availableImageHeight;

            double displayWidth = availableImageWidth;
            double displayHeight = availableImageHeight;

            double ratioX = 1f;
            double ratioY = 1f;

            Rectangle2D clip = null;

            Dimension2D dimension = renderer.getDimension();
            if (dimension != null) {
                normalWidth = dimension.getWidth();
                normalHeight = dimension.getHeight();
                displayWidth = normalWidth;
                displayHeight = normalHeight;

                float xalignFactor = getXAlignFactor(printImage);
                float yalignFactor = getYAlignFactor(printImage);

                switch (printImage.getScaleImage()) {
                case JRImage.SCALE_IMAGE_CLIP: {
                    xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
                    yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));
                    clip = new Rectangle2D.Double(-xoffset, -yoffset, availableImageWidth,
                            availableImageHeight);
                    break;
                }
                case JRImage.SCALE_IMAGE_FILL_FRAME: {
                    ratioX = availableImageWidth / normalWidth;
                    ratioY = availableImageHeight / normalHeight;
                    normalWidth *= ratioX;
                    normalHeight *= ratioY;
                    xoffset = 0;
                    yoffset = 0;
                    break;
                }
                case JRImage.SCALE_IMAGE_RETAIN_SHAPE:
                default: {
                    ratioX = availableImageWidth / normalWidth;
                    ratioY = availableImageHeight / normalHeight;
                    ratioX = ratioX < ratioY ? ratioX : ratioY;
                    ratioY = ratioX;
                    normalWidth *= ratioX;
                    normalHeight *= ratioY;
                    xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth));
                    yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight));
                    break;
                }
                }
            }

            PdfTemplate template = pdfContentByte.createTemplate((float) displayWidth, (float) displayHeight);

            Graphics2D g = forceSvgShapes
                    ? template.createGraphicsShapes((float) displayWidth, (float) displayHeight)
                    : template.createGraphics(availableImageWidth, availableImageHeight, new LocalFontMapper());

            if (clip != null) {
                g.setClip(clip);
            }

            if (printImage.getMode() == JRElement.MODE_OPAQUE) {
                g.setColor(printImage.getBackcolor());
                g.fillRect(0, 0, (int) displayWidth, (int) displayHeight);
            }

            Rectangle2D rectangle = new Rectangle2D.Double(0, 0, displayWidth, displayHeight);

            renderer.render(g, rectangle);
            g.dispose();

            pdfContentByte.saveState();
            pdfContentByte.addTemplate(template, (float) ratioX, 0f, 0f, (float) ratioY,
                    printImage.getX() + getOffsetX() + xoffset, jasperPrint.getPageHeight() - printImage.getY()
                            - getOffsetY() - (int) normalHeight - yoffset);
            pdfContentByte.restoreState();

            Image image = getPxImage();
            image.scaleAbsolute(availableImageWidth, availableImageHeight);
            chunk = new Chunk(image, 0, 0);
        }

        /*
        image.setAbsolutePosition(
           printImage.getX() + offsetX + borderOffset,
           jasperPrint.getPageHeight() - printImage.getY() - offsetY - image.scaledHeight() - borderOffset
           );
                
        pdfContentByte.addImage(image);
        */

        if (chunk != null) {
            setAnchor(chunk, printImage, printImage);
            setHyperlinkInfo(chunk, printImage);

            ColumnText colText = new ColumnText(pdfContentByte);
            int upperY = jasperPrint.getPageHeight() - printImage.getY() - topPadding - getOffsetY() - yoffset;
            int lowerX = printImage.getX() + leftPadding + getOffsetX() + xoffset;
            colText.setSimpleColumn(new Phrase(chunk), lowerX, upperY - scaledHeight, lowerX + scaledWidth,
                    upperY, scaledHeight, Element.ALIGN_LEFT);

            colText.go();
        }
    }

    if (printImage.getLineBox().getTopPen().getLineWidth().floatValue() <= 0f
            && printImage.getLineBox().getLeftPen().getLineWidth().floatValue() <= 0f
            && printImage.getLineBox().getBottomPen().getLineWidth().floatValue() <= 0f
            && printImage.getLineBox().getRightPen().getLineWidth().floatValue() <= 0f) {
        if (printImage.getLinePen().getLineWidth().floatValue() > 0f) {
            exportPen(printImage.getLinePen(), printImage);
        }
    } else {
        /*   */
        exportBox(printImage.getLineBox(), printImage);
    }
}

From source file:nl.dykema.jxmlnote.spikes.Spacing.java

License:Open Source License

/**
 * Main method.// ww  w  .j a v a  2s .c  om
 * @param    args    no arguments needed
 * @throws DocumentException 
 * @throws IOException
 */
public static void main(String[] args) throws DocumentException, IOException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    // step 3
    document.open();
    // step 4
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    Phrase p = new Phrase(
            "Dr. iText or: How I Learned to Stop Worrying " + "and Love the Portable Document Format.");
    PdfPCell cell = new PdfPCell(p);
    table.addCell("default leading / spacing");
    table.addCell(cell);
    table.addCell("absolute leading: 20");
    cell.setLeading(20f, 0f);
    table.addCell(cell);
    table.addCell("absolute leading: 3; relative leading: 1.2");
    cell.setLeading(3f, 1.2f);
    table.addCell(cell);
    table.addCell("absolute leading: 0; relative leading: 1.2");
    cell.setLeading(0f, 1.2f);
    table.addCell(cell);
    table.addCell("no leading at all");
    cell.setLeading(0f, 0f);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Dr. iText or: How I Learned to Stop Worrying and Love PDF"));
    table.addCell("padding 10");
    cell.setPadding(10);
    table.addCell(cell);
    table.addCell("padding 0");
    cell.setPadding(0);
    table.addCell(cell);
    table.addCell("different padding for left, right, top and bottom");
    cell.setPaddingLeft(20);
    cell.setPaddingRight(50);
    cell.setPaddingTop(0);
    cell.setPaddingBottom(5);
    table.addCell(cell);
    p = new Phrase("iText in Action Second Edition");
    table.getDefaultCell().setPadding(2);
    table.getDefaultCell().setUseAscender(false);
    table.getDefaultCell().setUseDescender(false);
    table.addCell("padding 2; no ascender, no descender");
    table.addCell(p);
    table.getDefaultCell().setUseAscender(true);
    table.getDefaultCell().setUseDescender(false);
    table.addCell("padding 2; ascender, no descender");
    table.addCell(p);
    table.getDefaultCell().setUseAscender(false);
    table.getDefaultCell().setUseDescender(true);
    table.addCell("padding 2; descender, no ascender");
    table.addCell(p);
    table.getDefaultCell().setUseAscender(true);
    table.getDefaultCell().setUseDescender(true);
    table.addCell("padding 2; ascender and descender");
    cell.setPadding(2);
    cell.setUseAscender(true);
    cell.setUseDescender(true);
    table.addCell(p);
    document.add(table);
    // step 5
    document.close();
}

From source file:nl.knaw.dans.common.lang.pdf.PdfPageLayouter.java

License:Apache License

public void onEndPage(final PdfWriter writer, final Document document) {
    final float bottom = document.bottom();
    final float centerX = getCenterX(document);
    final PdfContentByte canvas = writer.getDirectContent();
    final int rotation = 0;

    final PdfPTable table = new PdfPTable(5);
    table.setTotalWidth(PAGE_NUMBER_TABLE_WIDTH);
    table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
    table.getDefaultCell().setBorder(0);

    table.addCell(TO_FIRST);/*from  w w  w.  j a  va  2s.c o  m*/
    table.addCell(TO_PREV);
    table.addCell(new Phrase(new Chunk("" + document.getPageNumber(), FONT)));
    table.addCell(TO_NEXT);
    table.addCell(TO_LAST);

    table.writeSelectedRows(0, -1, centerX - (table.getTotalWidth() / 2), bottom - FOOTER_POSITION, canvas);
    if (footerPhrase == null)
        return;
    showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase(footerPhrase), centerX, bottom - FOOTER_POSITION,
            rotation);
}