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.vectorprint.report.itext.style.stylers.Shadow.java

License:Open Source License

@Override
public void draw(Rectangle rect, String genericTag) throws VectorPrintException {
    if (genericTag == null) {
        if (log.isLoggable(Level.FINE)) {
            log.fine("not drawing shadow because genericTag is null (no data for shadow)");
        }/*from  w ww  .j  a v a2  s . co  m*/
        return;
    }
    DelayedData delayed = getDelayed(genericTag);
    PdfContentByte canvas = getPreparedCanvas();
    try {
        com.itextpdf.text.Font f = delayed.getChunk().getFont();
        if (f.getBaseFont() == null) {
            throw new VectorPrintRuntimeException(
                    "font " + f.getFamilyname() + " does not have a basefont, check your fontloading");
        }
        /*
         * print as much of the text as fits in the width of the rectangle
         */
        String toPrint = delayed.getStringData();
        int i = toPrint.length() + 1;
        do {
            toPrint = toPrint.substring(0, --i);
        } while (ItextHelper.getTextWidth(toPrint, f.getBaseFont(), f.getSize()) > rect.getWidth() + 1);
        if (i < delayed.getStringData().length()) {
            String nextPart = delayed.getStringData().substring(i).replaceFirst(" *", "");
            if (log.isLoggable(Level.FINE)) {
                log.fine(String.format("event %s, printed shadow %s of %s, left %s for next event", genericTag,
                        toPrint, delayed.getData(), nextPart));
            }
            delayed.setData(nextPart);
        }

        canvas.setFontAndSize(f.getBaseFont(), f.getSize());
        canvas.setColorFill((getColor() == null) ? f.getColor() : itextHelper.fromColor(getColor()));
        canvas.setColorStroke((getColor() == null) ? f.getColor() : itextHelper.fromColor(getColor()));

        canvas.beginText();
        HashMap<String, Object> attributes = delayed.getChunk().getAttributes();
        if (attributes != null && attributes.containsKey(Chunk.SKEW)) {
            float[] skew = (float[]) attributes.get(Chunk.SKEW);
            canvas.setTextMatrix(1, skew[0], skew[1], 1, rect.getLeft() + calculateShift(getShiftx(), f),
                    rect.getBottom() - calculateShift(getShifty(), f));
        } else {
            canvas.setTextMatrix(1, 0, 0, 1, rect.getLeft() + calculateShift(getShiftx(), f),
                    rect.getBottom() - calculateShift(getShifty(), f));
        }
        canvas.setTextRise(delayed.getChunk().getTextRise());
        canvas.showText(toPrint);
        canvas.endText();
    } catch (Exception ex) {
        resetCanvas(canvas);
        throw new VectorPrintException(ex);
    }
    resetCanvas(canvas);
}

From source file:com.vectorprint.report.itext.style.stylers.Text.java

License:Open Source License

@Override
protected void draw(PdfContentByte canvas, float x, float y, float width, float height, String genericTag)
        throws VectorPrintException {
    BaseFont bf = FontFactory.getFont(getAlias()).getBaseFont();
    if (bf == null) {
        throw new VectorPrintRuntimeException(
                "font " + getAlias() + " does not have a basefont, check your fontloading");
    }//from w w  w . j a  v a2  s.co  m
    canvas.setFontAndSize(bf, getSize());
    canvas.setColorFill(itextHelper.fromColor((isDrawShadow()) ? getShadowColor() : getColor()));
    canvas.setColorStroke(itextHelper.fromColor((isDrawShadow()) ? getShadowColor() : getColor()));
    canvas.beginText();
    canvas.showTextAligned(Element.ALIGN_LEFT, getData(), x, y, getRotate());
    canvas.endText();
    if (getSettings().getBooleanProperty(Boolean.FALSE, ReportConstants.DEBUG)) {
        DebugHelper.styleLink(canvas, getStyleClass(), " (styling)", x, y, getSettings(), getLayerManager());
    }
}

From source file:Controlador.ControladorCrearPase.java

private static void absText(PdfWriter writer, String text, int x, int y) throws DocumentException, IOException {

    PdfContentByte cb = writer.getDirectContent();
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    cb.saveState();/*from   w w  w .j av a 2s.  c  o  m*/
    cb.beginText();
    cb.moveText(x, y);
    cb.setFontAndSize(bf, 12);
    cb.showText(text);
    cb.endText();
    cb.restoreState();

}

From source file:dbedit.actions.ExportPdfAction.java

License:Open Source License

/**
 * Print page numbers on right bottom corner
 * @param writer//from ww w  . ja  v  a2s. c  om
 * @param document
 */
@Override
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    String text = String.format("Page %d of ", writer.getPageNumber());
    float textSize = BASE_FONT.getWidthPoint(text, 12);
    float textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(BASE_FONT, 12);
    float adjust = BASE_FONT.getWidthPoint("000", 12);
    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(pdfTemplate, document.right() - adjust, textBase);
}

From source file:de.jost_net.JVerein.io.FormularAufbereitung.java

License:Open Source License

private void goFormularfeld(PdfContentByte contentByte, Formularfeld feld, Object val)
        throws DocumentException, IOException {
    BaseFont bf = null;/*from w w  w . j  a  va 2 s. c  om*/
    if (feld.getFont().startsWith("FreeSans")) {
        String filename = "/fonts/FreeSans";
        if (feld.getFont().length() > 8) {
            filename += feld.getFont().substring(9);
        }
        bf = BaseFont.createFont(filename + ".ttf", BaseFont.IDENTITY_H, true);
    } else {
        bf = BaseFont.createFont(feld.getFont(), BaseFont.CP1250, false);
    }

    float x = mm2point(feld.getX().floatValue());
    float y = mm2point(feld.getY().floatValue());
    if (val == null) {
        return;
    }
    buendig = links;
    String stringVal = getString(val);
    stringVal = stringVal.replace("\\n", "\n");
    stringVal = stringVal.replaceAll("\r\n", "\n");
    String[] ss = stringVal.split("\n");
    for (String s : ss) {
        contentByte.setFontAndSize(bf, feld.getFontsize().floatValue());
        contentByte.beginText();
        float offset = 0;
        if (buendig == rechts) {
            offset = contentByte.getEffectiveStringWidth(s, true);
        }
        contentByte.moveText(x - offset, y);
        contentByte.showText(s);
        contentByte.endText();
        y -= feld.getFontsize().floatValue() + 3;
    }
}

From source file:de.mat.utils.pdftools.PdfAddPageNum.java

License:Mozilla Public License

/**
 * <h4>FeatureDomain:</h4>//from www.  ja  va  2s.  com
 *     PublishingTools
 * <h4>FeatureDescription:</h4>
 *     adds pagenum with stamper to pages from reader 
 * <h4>FeatureResult:</h4>
 *   <ul>
 *     <li>updates stamper - updates the stamper
 *   </ul> 
 * <h4>FeatureKeywords:</h4>
 *     PDF Publishing
 * @param reader - reader with the pages
 * @param stamper - stamper to add the canvas
 * @param pageOffset - add to pagenumber
 * @throws DocumentException
 * @throws IOException
 */
public void addPageNumber(PdfReader reader, PdfStamper stamper, int pageOffset)
        throws DocumentException, IOException {
    // ierate all pages from reader
    for (int zaehler = 1; zaehler <= reader.getNumberOfPages(); zaehler++) {
        // read pagesize
        Rectangle pageSize = reader.getPageSize(zaehler);
        float xpos = pageSize.getLeft() + pageSize.getWidth() / 2;
        float ypos = 20;
        float fontSize = 7;

        // Default-Positions for --page-width 150mm --page-height 212mm == 601px
        if (pageSize.getHeight() > 602 || pageSize.getHeight() < 598) {
            // correct it relative
            float factor = pageSize.getHeight() / 601;
            if (LOGGER.isDebugEnabled())
                LOGGER.debug(" PageHeight:" + pageSize.getHeight() + " Factor:" + factor);
            ypos = ypos * factor;
            fontSize = fontSize * factor;
        }

        // add pagenumber-canvas
        PdfContentByte canvas = stamper.getOverContent(zaehler);
        BaseFont bf_helv = BaseFont.createFont(BaseFont.HELVETICA, "Cp1252", false);
        canvas.setFontAndSize(bf_helv, fontSize);
        canvas.beginText();
        canvas.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + new Integer(zaehler + pageOffset - 1), xpos,
                ypos, 0);
        canvas.endText();
    }
}

From source file:dk.dma.epd.common.util.FALPDFGenerator.java

License:Apache License

public void generateFal1Form(FALForm1 fal1form, String filename) {

    try {/*  ww  w.  j  av a  2 s  .c o  m*/

        PdfReader pdfReader = new PdfReader("FALForm1.pdf");

        FileOutputStream fileWriteStream = new FileOutputStream(filename);

        PdfStamper pdfStamper = new PdfStamper(pdfReader, fileWriteStream);

        for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {

            PdfContentByte content = pdfStamper.getUnderContent(i);

            // Text over the existing page
            BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.EMBEDDED);
            content.beginText();
            content.setFontAndSize(bf, 8);

            int xFirstColum = 68;
            int xSecondColum = 314;

            int startYFirstColumn = 659;

            int startYSecondColumn = 659;

            // Arrival Depature
            if (fal1form.isArrival()) {
                content.showTextAligned(PdfContentByte.ALIGN_LEFT, "X", 316, 690, 0);
            } else {
                // Departure
                content.showTextAligned(PdfContentByte.ALIGN_LEFT, "X", 380, 690, 0);
            }

            // Name and Type of ship
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getNameAndTypeOfShip(), xFirstColum,
                    startYFirstColumn, 0);

            // IMO Number
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getImoNumber(), xSecondColum,
                    startYSecondColumn, 0);

            // Call Sign
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getCallSign(), xFirstColum,
                    startYFirstColumn - 30, 0);

            // Voyage Number
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getVoyageNumber(), xSecondColum,
                    startYSecondColumn - 30, 0);

            // Port of Arrival/depature
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getPortOfArrivalDeapture(), xFirstColum,
                    startYFirstColumn - 60, 0);

            // Date and time of arrival/depature
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getDateAndTimeOfArrivalDepature(),
                    xSecondColum, startYFirstColumn - 60, 0);

            // Flag State of ship
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getFlagStateOfShip(), xFirstColum,
                    startYFirstColumn - 90, 0);

            // Name of Master
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getNameOfMaster(), xFirstColum + 135,
                    startYFirstColumn - 90, 0);

            // Last port of call/next port of all
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getLastPortOfCall(), xSecondColum,
                    startYFirstColumn - 90, 0);

            // Certificate of registry
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getCertificateOfRegistry(), xFirstColum,
                    startYFirstColumn - 120, 0);

            String nameAndContact = fal1form.getNameAndContactDetalsOfShipsAgent();

            addMultiLine(nameAndContact, startYFirstColumn, xSecondColum, content, 54, 120);

            // Gross Tonnage
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getGrossTonnage(), xFirstColum,
                    startYFirstColumn - 150, 0);

            // Net Tonnage
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getNetTonnage(), xFirstColum + 135,
                    startYFirstColumn - 150, 0);

            // Position of the ship in the port
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getPositionOfTheShip(), xFirstColum,
                    startYFirstColumn - 180, 0);

            // Brief particulars of voyage
            String briefVoyageParticulars = fal1form.getBriefParticulars();

            addMultiLine(briefVoyageParticulars, startYFirstColumn, xFirstColum, content, 140, 210);

            // Brief particulars of cargo
            String briefCargoParticulars = fal1form.getBriefDescriptionOfCargo();

            addMultiLine(briefCargoParticulars, startYFirstColumn, xFirstColum, content, 140, 257);

            // Number of Crew
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getNumberOfCrew(), xFirstColum,
                    startYFirstColumn - 305, 0);

            // Number of Passengers
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, fal1form.getNumberOfPassengers(),
                    xFirstColum + 130, startYFirstColumn - 305, 0);

            // Remarks
            String remarks = fal1form.getRemarks();
            addMultiLine(remarks, startYFirstColumn, xSecondColum, content, 54, 305);

            // Ship waste requirements
            String wasteRequirements = fal1form.getShipWasteRequirements();
            addMultiLine(wasteRequirements, startYFirstColumn, xSecondColum, content, 54, 405);

            content.endText();
        }

        pdfStamper.close();
        fileWriteStream.close();
        fileWriteStream.flush();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }

}

From source file:es.clinica.veterinaria.facturas.FacturaPdf.java

private void createHeadings(PdfContentByte cb, float x, float y, String text, int tam) {
    cb.beginText();/* w w w  . j av a  2s.co m*/
    cb.setFontAndSize(bfBold, tam);
    cb.setFontAndSize(bfBold, tam);
    cb.setTextMatrix(x, y);
    cb.showText(text.trim());
    cb.endText();
}

From source file:es.clinica.veterinaria.facturas.FacturaPdf.java

private void createCliente(PdfContentByte cb, float x, float y, String text, int tam) {
    cb.beginText();/*  w ww  .j  a va2s .c om*/
    cb.setFontAndSize(bf, tam);
    cb.setFontAndSize(bf, tam);
    cb.setTextMatrix(x, y);
    cb.showText(text.trim());
    cb.endText();
}

From source file:es.sm2.openppm.front.utils.DocumentUtils.java

License:Open Source License

/**
 * // ww w . j a  v a  2 s . c  o m
 * @param pagActual
 * @param pagTotal
 * @param headerImg
 * @param footerImg
 * @param reader
 * @param stamper
 * @param idioma
 * @throws DocumentException
 * @throws IOException
 */
private static void setHeaderFooter(int pagActual, int pagTotal, Image headerImg, Image footerImg,
        PdfReader reader, PdfStamper stamper, ResourceBundle idioma) throws DocumentException, IOException {

    PdfContentByte content = stamper.getUnderContent(pagActual);
    content.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 8);
    headerImg.setAbsolutePosition(74f, 745f);
    content.addImage(headerImg);
    footerImg.setAbsolutePosition(75f, 20f);
    content.addImage(footerImg);
    content.beginText();
    content.showTextAligned(Element.ALIGN_BOTTOM,
            "_____________________________________________________________________________________________________",
            75f, 55f, 0f);
    content.showTextAligned(Element.ALIGN_BOTTOM, "2010 OPEN PPM - Projet Portfolio Management. (Open PPM)",
            75f, 45f, 0f);
    content.showTextAligned(Element.ALIGN_BOTTOM, "www.sourceforce.net/openppm", 75f, 35f, 0f);
    content.showTextAligned(Element.ALIGN_BOTTOM,
            new ParamResourceBundle("pdf.pagination", pagActual, pagTotal).toString(idioma), 475f, 45f, 0f);
    content.endText();
    content.stroke();
}