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

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

Introduction

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

Prototype

public void setFontAndSize(BaseFont bf, float size) 

Source Link

Document

Set the font and the size for the subsequent text writing.

Usage

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

License:Open Source License

/**
 * Responsible for creating a new PDF page and workspace through <code>{@link PdfContentByte}</code> for direct writing to the
 * PDF.//from w  w  w  .jav a 2s . c  o  m
 *
 * @param writer The PDF writer used to write to the new page with.
 * @param reader The PDF reader used to read information from the PDF file.
 * @param pageNumber The current number of pages in the PDF file, which will be incremented by one inside this method.
 *
 * @return The PDFContentByte used to access the new PDF page.
 * @exception DocumentException
 * @exception IOException
 */
protected PdfContentByte startNewPage(PdfWriter writer, PdfReader reader, ModifiableInteger pageNumber)
        throws DocumentException, IOException {
    PdfContentByte retval;
    PdfContentByte under;
    Rectangle pageSize;
    Document pdfDoc;
    PdfImportedPage newPage;

    pageNumber.increment();
    pageSize = reader.getPageSize(FRONT_PAGE);
    retval = writer.getDirectContent();
    // under = writer.getDirectContentUnder();

    if (pageNumber.getInt() > FRONT_PAGE) {
        newPage = writer.getImportedPage(reader, CHECK_PAGE_NORMAL);
        setCurrentRenderingYPosition(pageSize.top(TOP_MARGIN + CHECK_DETAIL_HEADING_HEIGHT));
    } else {
        newPage = writer.getImportedPage(reader, FRONT_PAGE);
        setCurrentRenderingYPosition(pageSize.top(TOP_FIRST_PAGE));
    }

    pdfDoc = retval.getPdfDocument();
    pdfDoc.newPage();
    retval.addTemplate(newPage, 0, 0);
    retval.setFontAndSize(getTextFont(), 8);

    return retval;
}

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

License:Open Source License

/**
 * Overrides the method in PdfPageEventHelper from itext to include our watermark text to indicate that
 * this is a Test document and include the environment, if the environment is not a production environment.
 *
 * @param writer    The PdfWriter for this document.
 * @param document  The document.//from   ww w  .  j a v  a 2  s. c o m
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
@Override
public void onStartPage(PdfWriter writer, Document document) {
    if (!KRADUtils.isProductionEnvironment()) {
        PdfContentByte cb = writer.getDirectContentUnder();
        cb.saveState();
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        String watermarkText = "Test document (" + environment + ")";
        cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2,
                document.getPageSize().height() / 2, 45);
        cb.endText();
        cb.restoreState();
    }
    if (GlobalVariables.getUserSession() != null
            && GlobalVariables.getUserSession().retrieveObject("isPreview") != null) {
        GlobalVariables.getUserSession().removeObject("isPreview");
        PdfContentByte cb = writer.getDirectContentUnder();
        cb.saveState();
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        String watermarkText = "DRAFT";
        cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2,
                document.getPageSize().height() / 2, 45);
        cb.endText();
        cb.restoreState();
    }
}

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.// ww  w.ja va  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)
 */
@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./*from  w  w w  . j  a v  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();
    // 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.sys.PdfFormFillerUtil.java

License:Open Source License

/**
 * This method creates a Final watermark on the input Stream.
 *
 * @param templateStream/*  w  w  w. j av a2 s. c  o  m*/
 * @param finalmarkText
 * @return
 * @throws IOException
 * @throws DocumentException
 */
public static byte[] createFinalmarkOnFile(byte[] templateStream, String finalmarkText)
        throws IOException, DocumentException {
    // Create a PDF reader for the template
    PdfReader pdfReader = new PdfReader(templateStream);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // Create a PDF writer
    PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
    int n = pdfReader.getNumberOfPages();
    int i = 1;
    PdfContentByte over;
    BaseFont bf;
    try {
        bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
        PdfGState gstate = new PdfGState();
        while (i <= n) {
            // Watermark under the existing page
            Rectangle pageSize = pdfReader.getPageSizeWithRotation(i);
            over = pdfStamper.getOverContent(i);
            over.beginText();
            over.setFontAndSize(bf, 8);
            over.setGState(gstate);
            over.setColorFill(Color.BLACK);
            over.showTextAligned(Element.ALIGN_CENTER, finalmarkText, (pageSize.width() / 2),
                    (pageSize.height() - 10), 0);
            over.endText();
            i++;
        }
        pdfStamper.close();
    } catch (DocumentException ex) {
        throw new IOException("iText error creating final watermark on PDF", ex);
    } catch (IOException ex) {
        throw new IOException("IO error creating final watermark on PDF", ex);
    }
    return outputStream.toByteArray();
}

From source file:org.kuali.kfs.sys.PdfFormFillerUtil.java

License:Open Source License

/**
 * This Method creates a custom watermark on the File.
 *
 * @param templateStream//from  w w  w  . j av  a 2s  .  c  o  m
 * @param watermarkText
 * @return
 * @throws IOException
 * @throws DocumentException
 */
public static byte[] createWatermarkOnFile(byte[] templateStream, String watermarkText)
        throws IOException, DocumentException {
    // Create a PDF reader for the template
    PdfReader pdfReader = new PdfReader(templateStream);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // Create a PDF writer
    PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
    int n = pdfReader.getNumberOfPages();
    int i = 1;
    PdfContentByte over;
    BaseFont bf;
    try {
        bf = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED);
        PdfGState gstate = new PdfGState();
        gstate.setFillOpacity(0.5f);
        while (i <= n) {
            // Watermark under the existing page
            Rectangle pageSize = pdfReader.getPageSizeWithRotation(i);
            over = pdfStamper.getOverContent(i);
            over.beginText();
            over.setFontAndSize(bf, 200);
            over.setGState(gstate);
            over.setColorFill(Color.LIGHT_GRAY);
            over.showTextAligned(Element.ALIGN_CENTER, watermarkText, (pageSize.width() / 2),
                    (pageSize.height() / 2), 45);
            over.endText();
            i++;
        }
        pdfStamper.close();
    } catch (DocumentException ex) {
        throw new IOException("iText error creating watermark on PDF", ex);
    } catch (IOException ex) {
        throw new IOException("IO error creating watermark on PDF", ex);
    }
    return outputStream.toByteArray();
}

From source file:org.kuali.kra.printing.service.impl.WatermarkServiceImpl.java

License:Educational Community License

/**
 * This method is for setting the properties of watermark Text.
 * //ww  w  .  jav  a 2  s  . c  o  m
 * @param pdfContentByte
 * @param pageWidth
 * @param pageHeight
 * @param watermarkBean
 */
private void decoratePdfWatermarkText(PdfContentByte pdfContentByte, int pageWidth, int pageHeight,
        WatermarkBean watermarkBean) {
    float x, y, x1, y1, angle;
    try {
        if (watermarkBean.getType().equalsIgnoreCase(WatermarkConstants.WATERMARK_TYPE_TEXT)) {
            pdfContentByte.beginText();
            pdfContentByte.setFontAndSize(watermarkBean.getFont().getBaseFont(),
                    watermarkBean.getFont().getSize());
            Color fillColor = watermarkBean.getFont().getColor() == null
                    ? WatermarkConstants.DEFAULT_WATERMARK_COLOR
                    : watermarkBean.getFont().getColor();
            pdfContentByte.setColorFill(fillColor);
            int textWidth = (int) pdfContentByte.getEffectiveStringWidth(watermarkBean.getText(), false);
            int diagonal = (int) Math.sqrt((pageWidth * pageWidth) + (pageHeight * pageHeight));
            int pivotPoint = (diagonal - textWidth) / 2;

            angle = (float) Math.atan((float) pageHeight / pageWidth);

            x = (float) (pivotPoint * pageWidth) / diagonal;
            y = (float) (pivotPoint * pageHeight) / diagonal;

            x1 = (float) (((float) watermarkBean.getFont().getSize() / 2) * Math.sin(angle));
            y1 = (float) (((float) watermarkBean.getFont().getSize() / 2) * Math.cos(angle));

            pdfContentByte.showTextAligned(Element.ALIGN_LEFT, watermarkBean.getText(), x + x1, y - y1,
                    (float) Math.toDegrees(angle));
            pdfContentByte.endText();
        }

    } catch (Exception exception) {
        LOG.error("Exception occured in WatermarkServiceImpl. Water mark Exception: " + exception.getMessage());
    }

}

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

License:Educational Community License

/**
 * Overrides the method in PdfPageEventHelper from itext to include our watermark text to indicate that
 * this is a Test document and include the environment, if the environment is not a production environment.
 *
 * @param writer   The PdfWriter for this document.
 * @param document The document.//w ww.  ja v a  2 s  . co  m
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onStartPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onStartPage(PdfWriter writer, Document document) {
    if (!KRADUtils.isProductionEnvironment()) {
        PdfContentByte cb = writer.getDirectContentUnder();
        cb.saveState();
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        String watermarkText = "Test document (" + environment + ")";
        cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2,
                document.getPageSize().height() / 2, 45);
        cb.endText();
        cb.restoreState();
    }
    if (GlobalVariables.getUserSession() != null
            && GlobalVariables.getUserSession().retrieveObject("isPreview") != null) {
        GlobalVariables.getUserSession().removeObject("isPreview");
        PdfContentByte cb = writer.getDirectContentUnder();
        cb.saveState();
        cb.beginText();
        cb.setFontAndSize(helv, 48);
        String watermarkText = "DRAFT";
        cb.showTextAligned(Element.ALIGN_CENTER, watermarkText, document.getPageSize().width() / 2,
                document.getPageSize().height() / 2, 45);
        cb.endText();
        cb.restoreState();
    }
}

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 w  ww . ja  v a2 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();
    // 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) {
    /*/* w w  w . j  a  va  2s.  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();
    }
}