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

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

Introduction

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

Prototype

public void endText() 

Source Link

Document

Ends the writing of text and makes the current font invalid.

Usage

From source file:com.example.admin.avoidq.billGenerated.java

private void createHeadings(PdfContentByte cb, float x, float y, String text) {

    cb.beginText();/*from  w  ww .  j  a  v  a 2 s . com*/
    cb.setFontAndSize(bfBold, 8);
    cb.setTextMatrix(x, y);
    cb.showText(text.trim());
    cb.endText();

}

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   w  w  w . j  a v a2 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  w w  . ja  v a  2s. c  om*/
 * @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.
 * /*ww w . ja  v  a  2  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 {/*from ww  w . j  a v  a  2  s  .c  om*/
        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. ja  v  a2 s . 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 ww w.  jav  a2 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();//from w w  w  .java  2  s  .com
    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  ava  2 s .co 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 void debugBackground(PdfContentByte canvas, Rectangle rect, BaseColor color, String prefix,
        EnhancedMap settings, LayerManager layerAware) {
    canvas = canvas.getPdfWriter().getDirectContentUnder();
    int rgb = color.getRed() + color.getBlue() + color.getGreen();
    rect.setBackgroundColor(color);/* ww w .j a  v a2  s .  co  m*/
    canvas.rectangle(rect);
    layerAware.startLayerInGroup(DEBUG, canvas);
    debugFont(canvas, settings);
    BaseColor txtColor = (rgb < 150) ? color.brighter().brighter() : color.darker().darker();
    canvas.setColorFill(txtColor);
    canvas.setColorStroke(txtColor);
    canvas.beginText();
    canvas.showTextAligned(Element.ALIGN_LEFT, prefix + color.toString().replace(Color.class.getName(), ""),
            rect.getLeft() + rect.getWidth() / 2, rect.getTop() - rect.getHeight() / 2, 0);
    canvas.endText();
    canvas.endLayer();
}