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

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

Introduction

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

Prototype

public void setFontAndSize(final BaseFont bf, final float size) 

Source Link

Document

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

Usage

From source file:com.github.albfernandez.joinpdf.JoinPdf.java

License:Open Source License

private void writePageNumber(final PdfContentByte cb) throws DocumentException, IOException {
    if (isPrintPageNumbers()) {
        Rectangle size = cb.getPdfDocument().getPageSize();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        this.actualPage++;
        cb.beginText();/*from www . j  a v  a  2 s . c  o  m*/
        cb.setFontAndSize(bf, 9);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, this.actualPage + " of " + this.totalPages,
                size.getWidth() / 2, 10, 0);
        cb.endText();
    }
}

From source file:com.github.ossdevs.jhocr.converter.HocrPageProcessor.java

License:Open Source License

/**
 * TODO add documentation describing what this method does actually do.
 *
 * @param cb       TODO describe this parameter &/ it's purpose.
 * @param hocrLine is used to process the {@link com.itextpdf.text.pdf.PdfContentByte}
 * @throws IOException//from   w  ww. j av  a2 s .com
 * @throws DocumentException
 * @see https://code.google.com/p/jhocr/issues/detail?id=4
 */
private void processHocrLine(PdfContentByte cb, HocrLine hocrLine) {

    try {
        String lineText = hocrLine.getText();

        /**
         * TODO add documentation
         */
        if (!lineText.isEmpty()) {

            float lineHeightPt = hocrLine.getBbox().getHeight() / getDotsPerPointY();

            float fontSize = Math.round(lineHeightPt) - 0.8f; // Coloquei para o limite de erro

            if (fontSize == 0) {
                fontSize = 0.5f;
            }

            cb.setFontAndSize(baseFont, fontSize);
            cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
            cb.beginText();

            int t = hocrLine.getWords().size();

            /**
             * TODO add documentation
             *
             * @see <a>https://code.google.com/p/jhocr/issues/detail?id=4</a>
             */
            for (int i = 0; i < t; i++) {

                HocrWord hocrWord = hocrLine.getWords().get(i);

                float wordWidthPt = hocrWord.getBbox().getWidth() / getDotsPerPointX();

                processHocrWordCharacterSpacing(cb, hocrWord, wordWidthPt);

                float y = (getHocrPage().getBbox().getHeight() + lineHeightPt / 2
                        - hocrLine.getBbox().getBottom()) / dotsPerPointY;
                float x = hocrWord.getBbox().getLeft() / getDotsPerPointX();

                cb.showTextAligned(PdfContentByte.ALIGN_LEFT, hocrWord.getText(), x, y, 0);
            }

            cb.endText();

        }

    } catch (Exception e) {
        // TODO log error
        e.printStackTrace();
    }
}

From source file:com.googlecode.jhocr.converter.HocrPageProcessor.java

License:Open Source License

/**
 * TODO add documentation describing what this method does actually do.
 * //from  w w  w .j  av  a2  s .  c  o  m
 * @see https://code.google.com/p/jhocr/issues/detail?id=4
 * @param cb
 *            TODO describe this parameter &/ it's purpose.
 * @param hocrLine
 *            is used to process the {@link com.itextpdf.text.pdf.PdfContentByte}
 * @throws IOException
 * @throws DocumentException
 */
private void processHocrLine(PdfContentByte cb, HocrLine hocrLine) {

    try {
        String lineText = hocrLine.getText();

        /**
         * TODO add documentation
         */
        if (!lineText.isEmpty()) {

            float lineHeightPt = hocrLine.getBbox().getHeight() / getDotsPerPointY();

            float fontSize = Math.round(lineHeightPt) - 0.8f; // Coloquei para o limite de erro

            if (fontSize == 0) {
                fontSize = 0.5f;
            }

            cb.setFontAndSize(baseFont, fontSize);
            cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
            cb.beginText();

            int t = hocrLine.getWords().size();

            /**
             * TODO add documentation
             * 
             * @see <a>https://code.google.com/p/jhocr/issues/detail?id=4</a>
             */
            for (int i = 0; i < t; i++) {

                HocrWord hocrWord = hocrLine.getWords().get(i);

                float wordWidthPt = hocrWord.getBbox().getWidth() / getDotsPerPointX();

                processHocrWordCharacterSpacing(cb, hocrWord, wordWidthPt);

                float y = (getHocrPage().getBbox().getHeight() + lineHeightPt / 2
                        - hocrLine.getBbox().getBottom()) / dotsPerPointY;
                float x = hocrWord.getBbox().getLeft() / getDotsPerPointX();

                cb.showTextAligned(PdfContentByte.ALIGN_LEFT, hocrWord.getText(), x, y, 0);
            }

            cb.endText();

        }

    } catch (Exception e) {
        // TODO log error
        e.printStackTrace();
    }
}

From source file:com.innoviu.signature.Signature.java

public static void main(String[] args) {
    boolean isEncrypted = false;
    boolean isFailed = false;
    try {// ww w. j a v a  2  s .com
        if (args.length < 2) {
            throw new FileNotFoundException();
        }
        PdfReader reader = new PdfReader(args[0]);
        isEncrypted = reader.isEncrypted();
        String suffix = ".pdf";
        if (isEncrypted) {
            System.out.println("Encrypted");
            String[] cmd = { "pdftk", args[0], "output", args[0] + ".pdftk.pdf" };
            try {
                Process proc = Runtime.getRuntime().exec(cmd);
                proc.waitFor();
            } catch (Exception e) {
                System.out.println("Exception is:" + e);
            }
            reader = new PdfReader(args[0] + ".pdftk.pdf");
            suffix = ".dec.pdf";
        }
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(args[0] + suffix));
        PdfContentByte over = stamper.getOverContent(1);
        String type = args[2];
        int xpos = 0;
        //int xpos = (type == "in") ? 120 : 10;
        if ("in".equals(type)) {
            xpos = 0;
        } else {
            xpos = 120;
        }
        over.setColorFill(BaseColor.WHITE);
        over.rectangle(xpos + 10, 8, 120, 8);
        over.fill();
        over.beginText();
        BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", false);
        over.setFontAndSize(bf_times, 6);
        over.setColorFill(BaseColor.BLACK);
        over.showTextAligned(PdfContentByte.ALIGN_RIGHT, args[1], 120 + xpos, 10, 0);
        over.endText();
        stamper.close();
        if (isEncrypted) {
            File file = new File(args[0] + ".pdftk.pdf");
            file.delete();
        }
    } catch (FileNotFoundException e) {
        isFailed = true;
        e.printStackTrace();
    } catch (DocumentException e) {
        isFailed = true;
        e.printStackTrace();
    } catch (IOException e) {
        isFailed = true;
        e.printStackTrace();
    } finally {
        if (isEncrypted) {
            maintain(args[0]);
        } else if (isFailed) {
            fail(args[0]);
        } else {

        }
    }
}

From source file:com.masscustsoft.service.ToPdf.java

License:Open Source License

private void getDirectContent(PdfContentByte cb, Rectangle ps, Map it) throws Exception {
    BaseColor color = getColor(it, "fillColor");
    if (color != null)
        cb.setColorFill(color);/*from   w  w  w.  jav  a  2s.c  o m*/

    float x = MapUtil.getFloat(it, "x", 0f);
    float y = MapUtil.getFloat(it, "y", 0f);
    float w = MapUtil.getFloat(it, "w", 0f);
    float h = MapUtil.getFloat(it, "h", 0f);

    float xPer = MapUtil.getFloat(it, "xPer", 0f);
    float yPer = MapUtil.getFloat(it, "yPer", 0f);
    float wPer = MapUtil.getFloat(it, "wPer", 0f);
    float hPer = MapUtil.getFloat(it, "hPer", 0f);

    String pos = MapUtil.getStr(it, "position", "bottom");
    switch (pos) {
    case "top":
        y += ps.getHeight();
        break;
    case "right":
        x += ps.getWidth();
        break;
    }

    float xx = x + ps.getWidth() * xPer / 100f;
    float yy = y + ps.getWidth() * yPer / 100f;
    float ww = ps.getWidth() * wPer / 100f + w;
    float hh = ps.getHeight() * hPer / 100f + h;

    int font = MapUtil.getInt(it, "fontSize", 8);
    cb.setFontAndSize(getDefaultFont(), font);

    cb.beginText();

    String cls = MapUtil.getStr(it, "cls", "");

    if (cls.equals("image")) {
        Image img = getImage(it);
        cb.addImage(img, img.getWidth(), 0, 0, img.getHeight(), xx, yy);
    } else {
        String text = LightUtil.macro(MapUtil.getStr(it, "text", ""), '$').toString();
        float degree = MapUtil.getFloat(it, "rotateDegree", 0f);
        boolean kerned = MapUtil.getBool(it, "kerned", false);
        int align = getAlignment(it, "alignment");
        x = xx;
        y = yy;
        switch (align) {
        case Element.ALIGN_CENTER:
            x = xx + ww / 2;
            break;
        case Element.ALIGN_RIGHT:
            x = xx + ww;
            break;
        default:
            align = Element.ALIGN_LEFT;
            break;
        }
        if (kerned)
            cb.showTextAlignedKerned(align, text, x, y, degree);
        else
            cb.showTextAligned(align, text, x, y, degree);
    }

    cb.endText();
}

From source file:com.mycompany.mavenproject1.Createpdf.java

private void printPageNumber(PdfContentByte cb) {

    cb.beginText();/*from  w ww.  ja  v a 2  s .  c  o  m*/
    cb.setFontAndSize(bfBold, 8);
    cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "Page No. " + (pageNumber + 1), 570, 25, 0);
    cb.endText();

    pageNumber++;

}

From source file:com.mycompany.mavenproject1.Createpdf.java

private void createContent(PdfContentByte cb, float x, float y, String text, int align) {

    cb.beginText();//  w  w  w.j  a va  2 s  . co m
    cb.setFontAndSize(bf, 8);
    cb.showTextAligned(align, text.trim(), x, y, 0);
    cb.endText();

}

From source file:com.qmetric.document.watermark.strategy.MessageWatermarkStrategy.java

License:Open Source License

private void addTextToPage(final Rectangle page, final PdfContentByte overContent, final String watermarkText)
        throws DocumentException, IOException {
    // Add text - adapted from example found at
    // http://footheory.com/blogs/donnfelker/archive/2008/05/11/using-itextsharp-to-watermark-write-text-to-existing-pdf-s.aspx
    overContent.beginText();/*from   w  ww. j a v  a 2s  .  c o  m*/

    final BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, false);
    overContent.setFontAndSize(baseFont, 14);
    overContent.setRGBColorFill(RED, 0, 0);

    overContent.showTextAligned(PdfContentByte.ALIGN_LEFT, watermarkText, xAxisPosition(page),
            yAxisPosition(page), NO_ROTATION);
    overContent.endText();
}

From source file:com.vectorprint.report.itext.debug.DebugHelper.java

License:Open Source License

public static BaseFont debugFont(PdfContentByte canvas, EnhancedMap settings) {
    BaseFont bf = FontFactory.getFont(FontFactory.HELVETICA).getBaseFont();
    canvas.setFontAndSize(bf, 8);
    canvas.setColorFill(//from w w w. j  a  v  a2 s.  c  o m
            itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));
    canvas.setColorStroke(
            itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));
    return bf;
}

From source file:com.vectorprint.report.itext.debug.DebugHelper.java

License:Open Source License

/**
 * This method will append to the pdf a legend explaining the visual feedback in the pdf, an overview of the styles
 * used and an overview of the properties used.
 *
 * @throws DocumentException/*from ww  w .  j av a  2 s. co m*/
 */
public static void appendDebugInfo(PdfWriter writer, Document document, EnhancedMap settings,
        StylerFactory stylerFactory) throws DocumentException, VectorPrintException {

    PdfContentByte canvas = writer.getDirectContent();
    canvas.setFontAndSize(FontFactory.getFont(FontFactory.COURIER).getBaseFont(), 8);
    canvas.setColorFill(
            itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));
    canvas.setColorStroke(
            itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));

    Font f = FontFactory.getFont(FontFactory.COURIER, 8);

    f.setColor(itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR)));

    float top = document.getPageSize().getTop();

    document.add(new Phrase(new Chunk("table: ", f).setLocalDestination(DEBUGPAGE)));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("cell: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("image: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("text: ", f));
    document.add(Chunk.NEWLINE);
    document.add(new Phrase("background color is shown in a small rectangle", f));
    document.add(Chunk.NEWLINE);
    canvas.setLineWidth(2);
    canvas.setLineDash(new float[] { 0.3f, 5 }, 0);
    float left = document.leftMargin();
    canvas.rectangle(left + 80, top - 25, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineWidth(0.3f);
    canvas.setLineDash(new float[] { 2, 2 }, 0);
    canvas.rectangle(left + 80, top - 37, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineDash(new float[] { 1, 0 }, 0);
    canvas.rectangle(left + 80, top - 50, left + 80, 8);
    canvas.closePathStroke();
    canvas.setLineDash(new float[] { 0.3f, 5 }, 0);
    canvas.rectangle(left + 80, top - 63, left + 80, 8);
    canvas.closePathStroke();
    document.add(Chunk.NEWLINE);

    document.add(new Phrase("fonts available: " + FontFactory.getRegisteredFonts(), f));

    document.add(Chunk.NEWLINE);

    if (settings.getBooleanProperty(false, DEBUG)) {
        document.add(new Phrase("OVERVIEW OF STYLES FOR THIS REPORT", f));
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
    }

    Font b = new Font(f);
    b.setStyle("bold");
    Set<Map.Entry<String, String>> entrySet = stylerFactory.getStylerSetup().entrySet();
    for (Map.Entry<String, String> styleInfo : entrySet) {
        String key = styleInfo.getKey();
        document.add(new Chunk(key, b).setLocalDestination(key));
        document.add(new Chunk(": " + styleInfo.getValue(), f));
        document.add(Chunk.NEWLINE);
        document.add(new Phrase("   styling configured by " + key + ": ", f));
        for (BaseStyler st : (Collection<BaseStyler>) stylerFactory.getBaseStylersFromCache((key))) {
            document.add(Chunk.NEWLINE);
            document.add(new Chunk("      ", f));
            document.add(new Chunk(st.getClass().getSimpleName(), DebugHelper.debugFontLink(canvas, settings))
                    .setLocalGoto(st.getClass().getSimpleName()));
            document.add(new Chunk(":", f));
            document.add(Chunk.NEWLINE);
            document.add(new Phrase("         non default parameters for " + st.getClass().getSimpleName()
                    + ": " + getParamInfo(st), f));
            document.add(Chunk.NEWLINE);
            if (!st.getConditions().isEmpty()) {
                document.add(new Phrase("      conditions for this styler: ", f));
            }
            for (StylingCondition sc : (Collection<StylingCondition>) st.getConditions()) {
                document.add(Chunk.NEWLINE);
                document.add(new Chunk("         ", f));
                document.add(
                        new Chunk(sc.getClass().getSimpleName(), DebugHelper.debugFontLink(canvas, settings))
                                .setLocalGoto(sc.getClass().getSimpleName()));
                document.add(Chunk.NEWLINE);
                document.add(new Phrase("            non default parameters for "
                        + sc.getClass().getSimpleName() + ": " + getParamInfo(sc), f));
            }
        }
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
    }

    document.newPage();

    document.add(new Phrase("Properties used for the report", f));
    document.add(Chunk.NEWLINE);

    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(bo);

    settings.listProperties(ps);
    ps.close();
    document.add(new Paragraph(bo.toString(), f));

    document.newPage();

    try {
        bo = new ByteArrayOutputStream();
        ps = new PrintStream(bo);
        Help.printHelpHeader(ps);
        ps.close();
        document.add(new Paragraph(bo.toString(), f));

        Help.printStylerHelp(document, f);

        Help.printConditionrHelp(document, f);

    } catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException
            | NoSuchMethodException | InvocationTargetException ex) {
        log.log(Level.SEVERE, null, ex);
    }
}