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

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

Introduction

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

Prototype

public void showText(PdfTextArray text) 

Source Link

Document

Show an array of text.

Usage

From source file:classroom.intro.HelloWorld08.java

public static void main(String[] args) {
    Document.compress = false;//from  w  ww  .  j  ava 2s .co m
    // step 1
    Document document = new Document();
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.moveText(88.66f, 788);
        cb.showText("ld");
        cb.moveText(-22f, 0);
        cb.showText("Wor");
        cb.moveText(-15.33f, 0);
        cb.showText("llo");
        cb.endText();
        PdfTemplate tmp = cb.createTemplate(250, 25);
        tmp.beginText();
        tmp.setFontAndSize(bf, 12);
        tmp.moveText(0, 7);
        tmp.showText("He");
        tmp.endText();
        cb.addTemplate(tmp, 36, 781);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
}

From source file:classroom.intro.HelloWorld09.java

public static void main(String[] args) {
    Document.compress = false;/*from   w  w  w. ja v  a  2 s  .co m*/
    BaseFont bf = null;
    // step 1
    Document document = new Document();
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT1));
        // step 3
        document.open();
        // step 4
        PdfContentByte cb = writer.getDirectContent();
        bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.moveText(88.66f, 788);
        cb.showText("ld");
        cb.moveText(-22f, 0);
        cb.showText("Wor");
        cb.endText();
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();

    try {
        PdfReader reader = new PdfReader(RESULT1);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT2));
        PdfContentByte cb1 = stamper.getUnderContent(1);
        cb1.beginText();
        cb1.setFontAndSize(bf, 12);
        cb1.setTextMatrix(51.33f, 788);
        cb1.showText("llo");
        cb1.endText();
        PdfContentByte cb2 = stamper.getOverContent(1);
        PdfTemplate tmp = cb2.createTemplate(250, 25);
        tmp.beginText();
        tmp.setFontAndSize(bf, 12);
        tmp.moveText(0, 7);
        tmp.showText("He");
        tmp.endText();
        cb2.addTemplate(tmp, 36, 781);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

}

From source file:com.aryjr.nheengatu.testes.AbsolutePosition.java

License:Open Source License

public static void main(final String[] args) {
    //      System.out.println("My First PdfPTable");
    final Document document = new Document();
    try {/*  w ww .j a v  a  2 s. c  om*/
        final PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("/home/aryjr/MyFirstTable.pdf"));
        document.open();
        // PdfPTable table = createTable();
        // document.add(table);
        // table.writeSelectedRows(0, -1, 50, 200,
        // writer.getDirectContent());
        final PdfContentByte cb = writer.getDirectContent();
        cb.concatCTM(1f, 0f, 0f, -1f, 0f, 0f);
        // Paragraph text = new Paragraph("Ary Junior",
        // FontFactory.getFont(Style.DEFAULT_FONT_FAMILY,
        // Style.DEFAULT_FONT_SIZE, Font.NORMAL, Style.DEFAULT_FONT_COLOR));
        final BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.showText("Ary Junior");
        cb.endText();
        Runtime.getRuntime().exec("acroread /home/aryjr/MyFirstTable.pdf");
    } catch (final DocumentException de) {
        System.err.println(de.getMessage());
    } catch (final IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}

From source file:com.estate.pdf.PageBorder.java

public void draw(Document doc, int iconNum, String pageNum, String toolName) {
    try {//from   w w w. j a va  2  s  . co  m
        PdfContentByte cb = writer.getDirectContentUnder();
        BaseFont fontBold = BaseFont.createFont(Locations.getFontLocation() + "timesbd.ttf", BaseFont.CP1252,
                BaseFont.EMBEDDED);
        BaseFont font = BaseFont.createFont(Locations.getFontLocation() + "times.ttf", BaseFont.CP1252,
                BaseFont.EMBEDDED);
        Rectangle rct = new Rectangle(doc.getPageSize());
        float iconBase = (1.25f * 72); // This is the base of the icons on
        // the page
        Image icon = Image.getInstance(Locations.getImageLocation() + icons[iconNum]);
        // Image box = Image.getInstance(Locations.ImageLocation() +
        // "blueBOX.png");
        icon.scalePercent(23);
        float scale = .23f;
        float iconLeft = (icon.getWidth() / 2) * scale;
        float boxSize = (.1875f * 72);

        // Adjust the top
        rct.setTop(rct.getTop() - (.5f * 72));

        // Place the Icon
        icon.setAbsolutePosition((1.25f * 72) - iconLeft, rct.getTop() - iconBase);
        doc.add(icon);

        // Set our line color
        cb.setRGBColorStroke(0, 72, 117);

        cb.setLineWidth(.75f);

        // do the bottom line
        cb.moveTo(1.25f * 72, (.5f * 72));
        cb.lineTo(rct.getRight() - (.5f * 72), (.5f * 72));

        cb.moveTo(1.25f * 72, (.5f * 72));
        cb.lineTo(1.25f * 72, rct.getTop());

        // stroke the lines
        cb.stroke();

        // Do the lower left box
        cb.rectangle((1.25f - 0.09375f) * 72, (.5f - 0.09375f) * 72, boxSize, boxSize);
        cb.setRGBColorFill(0, 72, 117);
        cb.closePathFillStroke();

        // Do the lower right box
        Rectangle pnRect = new Rectangle(0, 0);
        pnRect.setLeft(rct.getRight() - ((.5f + 0.09375f) * 72));
        pnRect.setTop((.5f - 0.09375f) * 72);
        pnRect.setRight(pnRect.getLeft() + boxSize);
        pnRect.setBottom(pnRect.getTop() - boxSize);

        // cb.rectangle(rct.getRight() - ((.5f + 0.09375f) * 72), (.5f -
        // 0.09375f) * 72, (.1875f * 72), (.1875f * 72));
        cb.rectangle(pnRect.getLeft(), pnRect.getTop(), boxSize, boxSize);
        cb.setRGBColorFill(0, 72, 117);
        cb.closePathFillStroke();

        // Now we do the page number if one is supplied.
        if (pageNum.length() > 0) {
            float pnHeight = fontBold.getAscentPoint(pageNum, 9) - fontBold.getDescentPoint(pageNum, 9);
            float pnWidth = fontBold.getWidthPoint(pageNum, 9);

            float l = pnRect.getLeft() + ((boxSize - pnWidth) / 2);
            float b = pnRect.getTop() + ((boxSize - pnHeight) / 2);

            cb.beginText();
            cb.setFontAndSize(fontBold, 9);
            cb.setRGBColorFill(255, 255, 255);
            cb.setTextMatrix(l, b);
            cb.showText(pageNum);
            cb.endText();

        }

        // Display the copyright
        SimpleDateFormat df = new SimpleDateFormat("yyyy");
        char cs = 0x00a9; // Unicode for the copyright symbol
        String copyRight = com.estate.constants.StringConstants.copyRight + cs + " " + df.format(new Date());
        float crWidth;
        float crLeft;

        crWidth = font.getWidthPoint(copyRight, 8);
        cb.beginText();

        cb.setFontAndSize(font, 8);
        cb.setRGBColorFill(0, 0, 0);

        crLeft = (doc.getPageSize().getRight() - crWidth) / 2;
        cb.setTextMatrix(crLeft, .375f * 72); // Place the base of the
        // copyright at 3/8" up from
        // the bottom
        cb.showText(copyRight);
        cb.endText();

        if (toolName.length() > 0) {
            cb.beginText();

            cb.setFontAndSize(font, 8);
            cb.setRGBColorFill(0, 0, 0);

            cb.setTextMatrix((1.25f * 72) + boxSize, .375f * 72);
            cb.showText(toolName);
            cb.endText();
        }

        // Fix a licensee at left
        cb.beginText();
        cb.setFontAndSize(font, 8);
        cb.setRGBColorFill(0, 0, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, " Licensee: " + getLicense(), (10.2f * 72), .375f * 72,
                0);
        cb.endText();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:com.estate.pdf.PageBorder.java

public void drawNoBorder(Document doc, String pageNum) {
    try {//from   w w w  .  ja v a 2s  .c o m
        PdfContentByte cb = writer.getDirectContentUnder();
        BaseFont fontBold = BaseFont.createFont(Locations.getFontLocation() + "timesbd.ttf", BaseFont.CP1252,
                BaseFont.EMBEDDED);
        BaseFont font = BaseFont.createFont(Locations.getFontLocation() + "times.ttf", BaseFont.CP1252,
                BaseFont.EMBEDDED);
        Rectangle rct = new Rectangle(doc.getPageSize());
        float boxSize = (.1875f * 72);

        // Adjust the top
        rct.setTop(rct.getTop() - (.5f * 72));

        // Do the lower right box
        Rectangle pnRect = new Rectangle(0, 0);
        pnRect.setLeft(rct.getRight() - ((.5f + 0.09375f) * 72));
        pnRect.setTop(((.5f - 0.09375f) * 72) - 9);
        pnRect.setRight(pnRect.getLeft() + boxSize);
        pnRect.setBottom(pnRect.getTop() - boxSize);

        cb.rectangle(pnRect.getLeft(), pnRect.getTop(), boxSize, boxSize);
        cb.setRGBColorFill(0, 72, 117);
        cb.closePathFillStroke();

        // Now we do the page number if one is supplied.
        if (pageNum.length() > 0) {
            float pnHeight = fontBold.getAscentPoint(pageNum, 9) - fontBold.getDescentPoint(pageNum, 9) - 4.5f;
            float pnWidth = fontBold.getWidthPoint(pageNum, 9);

            float l = pnRect.getLeft() + ((boxSize - pnWidth) / 2);
            float b = pnRect.getTop() + ((boxSize - pnHeight) / 2) - 4.5f;

            cb.beginText();
            cb.setFontAndSize(fontBold, 9);
            cb.setRGBColorFill(255, 255, 255);
            cb.setTextMatrix(l, b);
            cb.showText(pageNum);
            cb.endText();
        }

        // Display the copyright
        SimpleDateFormat df = new SimpleDateFormat("yyyy");
        char cs = 0x00a9; // Unicode for the copyright symbol
        String copyRight = "Advanced Practice Network " + cs + " " + df.format(new Date());
        float crWidth;
        float crLeft;

        crWidth = font.getWidthPoint(copyRight, 8);
        cb.beginText();

        cb.setFontAndSize(font, 8);
        cb.setRGBColorFill(0, 0, 0);

        crLeft = (doc.getPageSize().getRight() - crWidth) / 2;
        cb.setTextMatrix(crLeft, .375f * 72); // Place the base of the
        // copyright at 3/8" up from
        // the bottom
        cb.showText(copyRight);
        cb.endText();

        if (license != null) {
            // Fix a licensee at left
            cb.beginText();
            cb.setFontAndSize(font, 8);
            cb.setRGBColorFill(0, 0, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, " Licensee: " + getLicense(), (10.2f * 72),
                    .375f * 72, 0);
            cb.endText();
        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:com.estate.report.Scenario.java

public void drawLifeExp() {
    PdfContentByte cb = writer.getDirectContent();
    char c = userInfo.getClientFirstName().charAt(0);
    char s = ' ';

    if (!userInfo.isSingle()) {
        s = userInfo.getSpouseFirstName().charAt(0);
    }/*from  ww  w  .j a  va  2  s .c o m*/
    String cText = Character.toString(c) + ":" + userInfo.getClientFirstName() + " actuarial life expectancy - "
            + Integer.toString(userInfo.getClientLifeExpectancy()) + " years";

    String sText = Character.toString(s) + ":" + userInfo.getSpouseFirstName() + " actuarial life expectancy - "
            + Integer.toString(userInfo.getSpouseLifeExpectancy()) + " years";

    try {
        BaseFont font = BaseFont.createFont(Locations.getFontLocation() + "times.ttf", BaseFont.CP1252,
                BaseFont.EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(font, 8);
        cb.setRGBColorFill(255, 0, 0);
        cb.setTextMatrix(72 * .5f, .5f * 72);
        cb.showText(cText);
        cb.endText();
        if (!userInfo.isSingle()) {
            cb.beginText();
            cb.setFontAndSize(font, 8);
            cb.setRGBColorFill(255, 0, 0);
            cb.setTextMatrix(72 * .5f, .35f * 72);
            cb.showText(sText);
            cb.endText();
        }
    } catch (Exception e) {
        System.err.println("Error printing Life Expectancy");
    }
}

From source file:com.logiware.accounting.reports.ArDisputeReportCreator.java

@Override
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();//from w ww. j av a2  s.  com
    String text = "Page " + writer.getPageNumber() + " of ";
    float textBase = document.bottom() - 20;
    float textSize = helvFont.getWidthPoint(text, 12);
    cb.beginText();
    cb.setFontAndSize(helvFont, 12);
    cb.setTextMatrix((document.right() / 2) - (textSize / 2), textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(pageTemplate, (document.right() / 2) + (textSize / 2), textBase);
    cb.restoreState();
}

From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java

License:Open Source License

private void buildFooter(final PdfWriter writer, final Document document) {
    PdfContentByte cb = writer.getDirectContent();

    cb.saveState();//from www . ja  v a 2  s. c o  m

    String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " ";

    float textBase = document.bottom() - 25;
    float textSize = FontUtils.getDejavu().getWidthPoint(text, 7);

    cb.setColorFill(ColorUtils.getLightColor());
    cb.setColorStroke(ColorUtils.getLightColor());
    cb.setLineWidth(1);
    cb.setLineDash(2, 2, 1);
    cb.moveTo(document.left(), document.bottom() - 10);
    cb.lineTo(document.right(), document.bottom() - 10);
    cb.stroke();
    cb.beginText();
    cb.setFontAndSize(FontUtils.getDejavu(), 7);

    float adjust = FontUtils.getDejavu().getWidthPoint("0", 7);

    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);

    textSize = FontUtils.getDejavu().getWidthPoint(footer.getGeneratedBy(), 7);

    cb.setTextMatrix(document.right() - textSize, textBase - 10);
    cb.showText(footer.getGeneratedBy());

    textSize = FontUtils.getDejavu().getWidthPoint(generationDate, 7);

    cb.setTextMatrix(document.right() - textSize, textBase - 20);
    cb.showText(generationDate);
    cb.endText();

    try {
        textSize = FontUtils.getDejavu().getWidthPoint(footer.getAdditionalText(), 7);

        ColumnText ct = new ColumnText(cb);

        ct.setSimpleColumn(new Phrase(footer.getAdditionalText(), FontUtils.getDejavuRegular7Light()),
                document.left() + 240, textBase + 10, document.left() + 390, textBase - 25, 10,
                Element.ALIGN_LEFT);
        ct.go();
    } catch (DocumentException e) {
        LOG.warn("Problem with additional text generation in report footer.");
    }

    try {
        ColumnText ct = new ColumnText(cb);

        ct.setSimpleColumn(document.left(), textBase + 10, document.left() + 230, textBase - 25, 10,
                Element.ALIGN_LEFT);
        ct.addText(new Phrase(footer.getCompanyName() + "\n", FontUtils.getDejavuRegular7Light()));

        if (!"".equals(footer.getAddress())) {
            ct.addText(new Phrase(footer.getAddress() + "\n", FontUtils.getDejavuRegular7Light()));
        }
        if (!"".equals(footer.getPhoneEmail())) {
            ct.addText(new Phrase(footer.getPhoneEmail(), FontUtils.getDejavuRegular7Light()));
        }

        ct.go();
    } catch (DocumentException e) {
        LOG.warn("Problem with company text generation in report footer.");
    }

    cb.addTemplate(total, document.right() - adjust, textBase);
    cb.restoreState();

}

From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java

License:Open Source License

private void buildHeader(final PdfWriter writer, final Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();/*from  w  w w .  j a v a  2 s  .c om*/
    String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " ";

    float textBase = document.top() + 22;
    float textSize = FontUtils.getDejavu().getWidthPoint(text, 7);

    cb.setColorFill(ColorUtils.getLightColor());
    cb.setColorStroke(ColorUtils.getLightColor());
    cb.beginText();
    cb.setFontAndSize(FontUtils.getDejavu(), 7);

    float adjust = FontUtils.getDejavu().getWidthPoint("0", 7);

    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(total, document.right() - adjust, textBase);
    cb.setLineWidth(1);
    cb.setLineDash(2, 2, 1);
    cb.moveTo(document.left(), document.top() + 12);
    cb.lineTo(document.right(), document.top() + 12);
    cb.stroke();
    cb.restoreState();

}

From source file:com.teag.reports.CashFlows.java

private void drawLifeExp() {
    PdfContentByte cb = writer.getDirectContentUnder();
    char c = userInfo.getClientFirstName().charAt(0);
    char s = ' ';
    if (!userInfo.isSingle()) {
        s = userInfo.getSpouseFirstName().charAt(0);
    }//ww  w . j a v a2  s  .  c om

    String cText = Character.toString(c) + ": " + userInfo.getClientFirstName()
            + " actuarial life expectancy - " + userInfo.getClientLifeExpectancy() + " years";
    String sText = Character.toString(s) + ": " + userInfo.getSpouseFirstName()
            + " actuarial life expectancy - " + userInfo.getSpouseLifeExpectancy() + " years";
    try {
        BaseFont font = BaseFont.createFont(Locations.getFontLocation() + "times.ttf", BaseFont.CP1252,
                BaseFont.EMBEDDED);

        cb.beginText();
        cb.setFontAndSize(font, 8);
        cb.setRGBColorFill(255, 0, 0);
        cb.setTextMatrix(72 * .5F, .5f * 72);
        cb.showText(cText);
        cb.endText();
        if (!userInfo.isSingle()) {
            cb.beginText();
            cb.setFontAndSize(font, 8);
            cb.setRGBColorFill(255, 0, 0);
            cb.setTextMatrix(72 * .5F, .35f * 72);
            cb.showText(sText);
            cb.endText();
        }
    } catch (Exception e) {
        System.err.println("Error printing Life Expectancy");
    }
}