Example usage for com.lowagie.text.pdf PdfContentByte setTextMatrix

List of usage examples for com.lowagie.text.pdf PdfContentByte setTextMatrix

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfContentByte setTextMatrix.

Prototype

public void setTextMatrix(float x, float y) 

Source Link

Document

Changes the text matrix.

Usage

From source file:org.eclipse.birt.report.engine.emitter.pdf.PDFPage.java

License:Open Source License

private void setTextMatrix(PdfContentByte cb, FontInfo fi, float x, float y) {
    cb.concatCTM(1, 0, 0, 1, x, y);//from   w w w .  ja v  a 2s.  c  om
    if (!fi.getSimulation()) {
        cb.setTextMatrix(0, 0);
        return;
    }
    switch (fi.getFontStyle()) {
    case Font.ITALIC: {
        simulateItalic(cb);
        break;
    }
    case Font.BOLD: {
        simulateBold(cb, fi.getFontWeight());
        break;
    }
    case Font.BOLDITALIC: {
        simulateBold(cb, fi.getFontWeight());
        simulateItalic(cb);
        break;
    }
    }
}

From source file:org.eclipse.birt.report.engine.emitter.pdf.PDFPage.java

License:Open Source License

private void simulateBold(PdfContentByte cb, int fontWeight) {
    cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
    if (fontWeightLineWidthMap.containsKey(fontWeight)) {
        cb.setLineWidth(fontWeightLineWidthMap.get(fontWeight));
    } else {/*from w ww.j  a v  a  2  s.  c om*/
        cb.setLineWidth(0.225f);
    }
    cb.setTextMatrix(0, 0);
}

From source file:org.eclipse.birt.report.engine.layout.pdf.font.FontCacheUtils.java

License:Open Source License

static void createUnicodePDF(String format, Locale locale, String fileName) throws Exception {
    FontMappingManager manager = FontMappingManagerFactory.getInstance().getFontMappingManager(format, locale);

    // step 1: creation of a document-object
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(fileName));
    document.open();/*from www.  ja va2 s  .c o  m*/
    for (int seg = 0; seg < 0xFF; seg++) {
        PdfContentByte cb = writer.getDirectContent();
        cb.beginText();
        for (int hi = 0; hi < 16; hi++) {
            for (int lo = 0; lo < 16; lo++) {
                int x = 100 + hi * 32;
                int y = 100 + lo * 32;
                char ch = (char) (seg * 0xFF + hi * 16 + lo);

                String fontFamily = manager.getDefaultPhysicalFont(ch);
                BaseFont bf = manager.createFont(fontFamily, Font.NORMAL);
                cb.setFontAndSize(bf, 16);
                cb.setTextMatrix(x, y);
                cb.showText(new String(new char[] { ch }));
            }
        }
        cb.endText();
    }
    document.close();
}

From source file:org.kuali.kfs.fp.document.service.impl.CashReceiptCoverSheetServiceImpl.java

License:Open Source License

/**
 *
 * This method writes out the value provided to the output provided and aligns the value outputted using the xPos float
 * provided./*from   ww  w  .  j  a  va 2  s.  co m*/
 * @param output The content byte used to write out the field to the PDF template.
 * @param xPos The x coordinate of the starting point on the document where the value will be written to.
 * @param fieldValue The value to be written to the PDF cover sheet.
 */
protected void writeCheckField(PdfContentByte output, float xPos, String fieldValue) {
    output.beginText();
    output.setTextMatrix(xPos, getCurrentRenderingYPosition());
    output.newlineShowText(fieldValue);
    output.endText();
}

From source file:org.kuali.kfs.module.purap.pdf.PurapPdf.java

License:Open Source License

/**
 * Overrides the method in PdfPageEventHelper from itext to write the headerTable, compose the footer and show the
 * footer.//from  w w w .  j  a va2s  .c o  m
 *
 * @param writer    The PdfWriter for this document.
 * @param document  The document.
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
@Override
public void onEndPage(PdfWriter writer, Document document) {
    LOG.debug("onEndPage() started.");
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    // write the headerTable
    headerTable.setTotalWidth(document.right() - document.left());
    headerTable.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 10, cb);
    // compose the footer
    String text = "Page " + writer.getPageNumber() + " of ";
    float textSize = helv.getWidthPoint(text, 12);
    float textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(helv, 12);
    // show the footer
    float adjust = helv.getWidthPoint("0", 12);
    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(tpl, document.right() - adjust, textBase);
    cb.saveState();
}

From source file:org.kuali.kfs.module.purap.pdf.PurchaseOrderQuoteRequestsPdf.java

License:Open Source License

/**
 * Overrides the method in PdfPageEventHelper from itext to compose the footer and show the
 * footer./*w  w  w  . j a v  a 2  s .  co  m*/
 *
 * @param writer    The PdfWriter for this document.
 * @param document  The document.
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onEndPage(PdfWriter writer, Document document) {
    LOG.debug("onEndPage() started.");
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    // compose the footer
    String text = "Page " + writer.getPageNumber() + " of ";
    float textSize = helv.getWidthPoint(text, 12);
    float textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(helv, 12);
    // show the footer
    float adjust = helv.getWidthPoint("0", 12);
    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(tpl, document.right() - adjust, textBase);
    cb.saveState();
}

From source file:org.kuali.ole.module.purap.pdf.PurapPdf.java

License:Educational Community License

/**
 * Overrides the method in PdfPageEventHelper from itext to write the headerTable, compose the footer and show the
 * footer.//from  www  . jav  a  2 s  .c  o m
 *
 * @param writer   The PdfWriter for this document.
 * @param document The document.
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onEndPage(PdfWriter writer, Document document) {
    LOG.debug("onEndPage() started.");
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    // write the headerTable
    headerTable.setTotalWidth(document.right() - document.left());
    headerTable.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 10, cb);
    // compose the footer
    String text = "Page " + writer.getPageNumber() + " of ";
    float textSize = helv.getWidthPoint(text, 12);
    float textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(helv, 12);
    // show the footer
    float adjust = helv.getWidthPoint("0", 12);
    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(tpl, document.right() - adjust, textBase);
    cb.saveState();
}

From source file:org.mapfish.print.map.renderers.vector.LabelRenderer.java

License:Open Source License

static void applyStyle(RenderingContext context, PdfContentByte dc, PJsonObject style, Geometry geometry,
        AffineTransform affineTransform) {
    /*/*from  w w  w .ja  v  a 2  s  .  c  o m*/
     * See Feature/Vector.js for more information about labels
     */
    String label = style.optString("label");

    if (label != null && label.length() > 0) {
        /*
         * Valid values for horizontal alignment: "l"=left, "c"=center,
         * "r"=right. Valid values for vertical alignment: "t"=top,
         * "m"=middle, "b"=bottom.
         */
        String labelAlign = style.optString("labelAlign", "cm");
        float labelXOffset = style.optFloat("labelXOffset", (float) 0.0);
        float labelYOffset = style.optFloat("labelYOffset", (float) 0.0);
        String fontColor = style.optString("fontColor", "#000000");
        /* Supported itext fonts: COURIER, HELVETICA, TIMES_ROMAN */
        String fontFamily = style.optString("fontFamily", "HELVETICA");
        if (!"COURIER".equalsIgnoreCase(fontFamily) || !"HELVETICA".equalsIgnoreCase(fontFamily)
                || !"TIMES_ROMAN".equalsIgnoreCase(fontFamily)) {

            LOGGER.info("Font: '" + fontFamily + "' not supported, supported fonts are 'HELVETICA', "
                    + "'COURIER', 'TIMES_ROMAN', defaults to 'HELVETICA'");
            fontFamily = "HELVETICA";
        }
        String fontSize = style.optString("fontSize", "12");
        String fontWeight = style.optString("fontWeight", "normal");
        Coordinate center = geometry.getCentroid().getCoordinate();
        center = GeometriesRenderer.transformCoordinate(center, affineTransform);
        float f = context.getStyleFactor();
        BaseFont bf = PDFUtils.getBaseFont(fontFamily, fontSize, fontWeight);
        float fontHeight = (float) Double.parseDouble(fontSize.toLowerCase().replaceAll("px", "")) * f;
        dc.setFontAndSize(bf, fontHeight);
        dc.setColorFill(ColorWrapper.convertColor(fontColor));
        dc.beginText();
        dc.setTextMatrix((float) center.x + labelXOffset * f, (float) center.y + labelYOffset * f);
        dc.showTextAligned(PDFUtils.getHorizontalAlignment(labelAlign), label,
                (float) center.x + labelXOffset * f,
                (float) center.y + labelYOffset * f - PDFUtils.getVerticalOffset(labelAlign, fontHeight), 0);
        dc.endText();
    }
}

From source file:org.mapfish.print.map.renderers.vector.PointRenderer.java

License:Open Source License

protected void renderImpl(RenderingContext context, PdfContentByte dc, PJsonObject style, Point geometry) {
    PdfGState state = new PdfGState();
    final Coordinate coordinate = geometry.getCoordinate();
    float pointRadius = style.optFloat("pointRadius", 4.0f);
    final float f = context.getStyleFactor();

    String graphicName = style.optString("graphicName");
    float width = style.optFloat("graphicWidth", pointRadius * 2.0f);
    float height = style.optFloat("graphicHeight", pointRadius * 2.0f);
    float offsetX = style.optFloat("graphicXOffset", -width / 2.0f);
    float offsetY = style.optFloat("graphicYOffset", -height / 2.0f);
    // See Feature/Vector.js for more information about labels
    String label = style.optString("label");
    String labelAlign = style.optString("labelAlign", "lb");
    /*//from   ww w  .ja v a  2  s. co m
     * Valid values for horizontal alignment: "l"=left, "c"=center, "r"=right. 
     * Valid values for vertical alignment: "t"=top, "m"=middle, "b"=bottom.
     */
    float labelXOffset = style.optFloat("labelXOffset", (float) 0.0);
    float labelYOffset = style.optFloat("labelYOffset", (float) 0.0);
    String fontColor = style.optString("fontColor", "#000000");
    /* Supported itext fonts: COURIER, HELVETICA, TIMES_ROMAN */
    String fontFamily = style.optString("fontFamily", "HELVETICA");
    String fontSize = style.optString("fontSize", "12");
    String fontWeight = style.optString("fontWeight", "normal");

    if (style.optString("externalGraphic") != null) {
        float opacity = style.optFloat("graphicOpacity", style.optFloat("fillOpacity", 1.0f));
        state.setFillOpacity(opacity);
        state.setStrokeOpacity(opacity);
        dc.setGState(state);
        try {
            Image image = PDFUtils.createImage(context, width * f, height * f,
                    new URI(style.getString("externalGraphic")), 0.0f);
            image.setAbsolutePosition((float) coordinate.x + offsetX * f, (float) coordinate.y + offsetY * f);
            dc.addImage(image);
        } catch (BadElementException e) {
            context.addError(e);
        } catch (URISyntaxException e) {
            context.addError(e);
        } catch (DocumentException e) {
            context.addError(e);
        }

    } else if (graphicName != null && !graphicName.equalsIgnoreCase("circle")) {
        PolygonRenderer.applyStyle(context, dc, style, state);
        float[] symbol = SYMBOLS.get(graphicName);
        if (symbol == null) {
            throw new InvalidValueException("graphicName", graphicName);
        }
        dc.setGState(state);
        dc.moveTo((float) coordinate.x + symbol[0] * width * f + offsetX * f,
                (float) coordinate.y + symbol[1] * height * f + offsetY * f);
        for (int i = 2; i < symbol.length - 2; i += 2) {
            dc.lineTo((float) coordinate.x + symbol[i] * width * f + offsetX * f,
                    (float) coordinate.y + symbol[i + 1] * height * f + offsetY * f);

        }
        dc.closePath();
        dc.fillStroke();

    } else if (label != null && label.length() > 0) {
        BaseFont bf = PDFUtils.getBaseFont(fontFamily, fontSize, fontWeight);
        float fontHeight = (float) Double.parseDouble(fontSize.toLowerCase().replaceAll("px", "")) * f;
        dc.setFontAndSize(bf, fontHeight);
        dc.setColorFill(ColorWrapper.convertColor(fontColor));
        state.setFillOpacity((float) 1.0);
        dc.setGState(state);
        dc.beginText();
        dc.setTextMatrix((float) coordinate.x + labelXOffset * f, (float) coordinate.y + labelYOffset * f);
        dc.setGState(state);
        dc.showTextAligned(PDFUtils.getHorizontalAlignment(labelAlign), label,
                (float) coordinate.x + labelXOffset * f,
                (float) coordinate.y + labelYOffset * f - PDFUtils.getVerticalOffset(labelAlign, fontHeight),
                0);
        dc.endText();
    } else {
        PolygonRenderer.applyStyle(context, dc, style, state);
        dc.setGState(state);

        dc.circle((float) coordinate.x, (float) coordinate.y, pointRadius * f);
        dc.fillStroke();
    }
}

From source file:org.meveo.admin.action.billing.BillingAccountBean.java

License:Open Source License

public void generatePDF(long invoiceId) {
    Invoice invoice = invoiceService.findById(invoiceId);
    byte[] invoicePdf = invoice.getPdf();
    FacesContext context = FacesContext.getCurrentInstance();
    String invoiceFilename = null;
    BillingRun billingRun = invoice.getBillingRun();
    invoiceFilename = invoice.getInvoiceNumber() + ".pdf";
    if (billingRun != null && billingRun.getStatus() != BillingRunStatusEnum.VALIDATED) {
        invoiceFilename = "unvalidated-invoice.pdf";
    }/*from  ww  w .  j av a2  s .  c om*/

    HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
    response.setContentType("application/pdf"); // fill in
    response.setHeader("Content-disposition", "attachment; filename=" + invoiceFilename);

    try {
        OutputStream os = response.getOutputStream();
        Document document = new Document(PageSize.A4);
        if (billingRun != null && invoice.getBillingRun().getStatus() != BillingRunStatusEnum.VALIDATED) {
            // Add watemark image
            PdfReader reader = new PdfReader(invoicePdf);
            int n = reader.getNumberOfPages();
            PdfStamper stamp = new PdfStamper(reader, os);
            PdfContentByte over = null;
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.5f);
            int i = 1;
            while (i <= n) {
                over = stamp.getOverContent(i);
                over.setGState(gs);
                over.beginText();
                System.out.println("top=" + document.top() + ",bottom=" + document.bottom());
                over.setTextMatrix(document.top(), document.bottom());
                over.setFontAndSize(bf, 150);
                over.setColorFill(Color.GRAY);
                over.showTextAligned(Element.ALIGN_CENTER, "TEST", document.getPageSize().getWidth() / 2,
                        document.getPageSize().getHeight() / 2, 45);
                over.endText();
                i++;
            }

            stamp.close();
        } else {
            os.write(invoicePdf); // fill in PDF with bytes
        }

        // contentType
        os.flush();
        os.close();
        context.responseComplete();
    } catch (IOException e) {
        log.error("failed to generate PDF ", e);
    } catch (DocumentException e) {
        log.error("error in generation PDF ", e);
    }
}