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

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

Introduction

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

Prototype

public void showTextAligned(final int alignment, final String text, final float x, final float y,
        final float rotation) 

Source Link

Document

Shows text right, left or center aligned with rotation.

Usage

From source file:com.vectorprint.report.itext.TocOutputStream.java

License:Open Source License

private void printToc(Document d, PdfWriter w, VectorPrintDocument vpd)
        throws VectorPrintException, InstantiationException, IllegalAccessException, DocumentException {
    DocumentStyler ds = outer.getStylerFactory().getDocumentStyler();
    if (ds.getValue(DocumentSettings.TOCAPPEND, Boolean.class)) {
        d.add(Chunk.NEXTPAGE);//from   w w  w . j ava  2s. com
    }
    if (outer.isWasDebug()) {
        outer.getSettings().put(ReportConstants.DEBUG, Boolean.TRUE.toString());
        PdfContentByte canvas = w.getDirectContent();
        outer.startLayerInGroup(ReportConstants.DEBUG, canvas);
        BaseFont bf = DebugHelper.debugFont(canvas, outer.getSettings());
        canvas.showTextAligned(Element.ALIGN_RIGHT,
                "FOR DEBUG INFO IN THE DOCUMENT TURN OFF TOC (-DocumentSettings.toc=false)", d.right(),
                d.getPageSize().getHeight() - ItextHelper.getTextHeight("F", bf, 8), 0);
        canvas.endLayer();
    }
    ElementProducer ep = outer.getElementProducer();
    StylerFactory sf = outer.getStylerFactory();
    PdfPTable tocTable = ep.createElement(null, PdfPTable.class, sf.getStylers(DocumentSettings.TOCTABLEKEY));
    for (Map.Entry<Integer, List<Section>> e : vpd.getToc().entrySet()) {
        String link = null;
        for (Section s : e.getValue()) {
            if (ds.isParameterSet(DocumentSettings.TOCMAXDEPTH)
                    && ds.getValue(DocumentSettings.TOCMAXDEPTH, Integer.class) < s.getDepth()) {
                continue;
            }
            if (link == null) {
                link = s.getTitle().getContent();
            }
            Chunk c = ep.createElement(s.getTitle().getContent(), Chunk.class,
                    sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY));
            if (ds.getValue(DocumentSettings.TOCDOTS, Boolean.class)) {
                float tw = ItextHelper.getTextWidth(c);
                float cw = tocTable.getAbsoluteWidths()[0];
                float dw = ItextHelper.getTextWidth(
                        ep.createElement(".", Chunk.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY)))
                        * 1.5f;
                int numDots = (int) ((cw > tw) ? (cw - tw) / dw : 0);
                char[] dots = new char[numDots];
                Arrays.fill(dots, '.');
                c = ep.createElement(s.getTitle().getContent() + "  " + String.valueOf(dots), Chunk.class,
                        sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY));
            }
            c.setLocalGoto(link);
            tocTable.addCell(
                    ep.createElement(c, PdfPCell.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY)));
            c = ep.createElement(e.getKey(), Chunk.class, sf.getStylers(DocumentSettings.TOCNRSTYLEKEY));
            c.setLocalGoto(link);
            tocTable.addCell(
                    ep.createElement(c, PdfPCell.class, sf.getStylers(DocumentSettings.TOCNRSTYLEKEY)));
        }
    }
    d.add(tocTable);
}

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

License:Mozilla Public License

/**
 * <h4>FeatureDomain:</h4>/*from  ww  w  .  j  ava  2s. c o  m*/
 *     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 {/*  w  w  w. java 2  s  .co  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:dk.dma.epd.common.util.FALPDFGenerator.java

License:Apache License

private void addMultiLine(String text, int startYFirstColumn, int xSecondColum, PdfContentByte content,
        int width, int modifier) {

    String parsedtext = "";
    int length = text.length();
    int startPosition = startYFirstColumn - modifier;

    while (text.length() > width) {
        parsedtext = text.substring(0, width);
        if (!parsedtext.subSequence(parsedtext.length() - 1, parsedtext.length()).equals(" ")) {
            parsedtext = parsedtext + "-";
        }/* w w  w . ja v  a2  s . co  m*/

        // Name and contact detals of ships agent
        content.showTextAligned(PdfContentByte.ALIGN_LEFT, parsedtext, xSecondColum, startPosition, 0);

        text = text.substring(width, length);

        length = text.length();
        startPosition = startPosition - 10;
    }

    // Name and contact detals of ships agent last part
    content.showTextAligned(PdfContentByte.ALIGN_LEFT, text, xSecondColum, startPosition, 0);
}

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

License:Open Source License

/**
 * //w w  w  .  j a  v a 2 s .c  om
 * @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();
}

From source file:fattura.Fattura.java

public void inserisciDatiMaglie(String cliente, String data, PdfStamper s)
        throws SQLException, IOException, DocumentException {
    ArrayMaglia arraymaglia = new ArrayMaglia();
    arraymaglia.ArrayIDmaglia(cliente, data);
    float importo;

    PdfContentByte content = s.getUnderContent(1);//1 for the first page
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED);
    content.setFontAndSize(bf, 7);/*w  ww.java2  s . c  o  m*/

    for (int j = 0; j < arraymaglia.IDmaglie.length; j++) {
        arraymaglia.accessoMaglie(j);
        importo = arraymaglia.importoMaglia(j);
        String convertitore = String.valueOf(importo); //Serve per convertire il ritorno del metodo importoUnitarioMaglia (float) a string per stamparlo,  solo un appoggio
        importi.add(importo); //salva l'importo maglia nella prima posizione array i cui elementi poi andranno sommati per calcolare il totale

        for (int z = 0; z < arraymaglia.riga.length; z++) {
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, arraymaglia.riga[z] + "   ", 95 + x, 538 - y, 0);
            x += 97; //sposta la coordinata y ad ogni stampa
        }

        content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Maglia", 95 + x, 538 - y, 0);
        x += 96;
        content.showTextAligned(PdfContentByte.ALIGN_LEFT, convertitore, 95 + x, 538 - y, 0);

        x = 0;
        y += 20;
    }

}

From source file:fattura.Fattura.java

public void inserisciDatiBorse(String cliente, String data, PdfStamper s)
        throws SQLException, IOException, DocumentException {
    ArrayBorsa arrayborsa = new ArrayBorsa();
    arrayborsa.ArrayIDborsa(cliente, data);
    float importo;

    PdfContentByte content = s.getUnderContent(1);//1 for the first page
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED);
    content.setFontAndSize(bf, 7);/*from w w  w  .  ja  v a  2s.  c om*/

    for (int j = 0; j < arrayborsa.IDborse.length; j++) {
        arrayborsa.accessoBorse(j);
        importo = arrayborsa.importoBorsa(j);
        String convertitore = String.valueOf(importo); //Serve per convertire il ritorno del metodo importoUnitarioMaglia (float) a string per stamparlo,  solo un appoggio
        importi.add(importo);
        for (int z = 0; z < arrayborsa.riga.length; z++) {
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, arrayborsa.riga[z] + "   ", 95 + x, 538 - y, 0);
            x += 97; //sposta la coordinata y ad ogni stampa

        }
        content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Borsa", 95 + x, 538 - y, 0);
        x += 97;
        content.showTextAligned(PdfContentByte.ALIGN_LEFT, convertitore, 95 + x, 538 - y, 0);
        x = 0;
        y += 20;
    }
    //MI calcolar il prezzo totale man mano che calcolo i preventivi lo agggiungo!

}

From source file:fattura.Fattura.java

public void inserisciDatiPanta(String cliente, String data, PdfStamper s)
        throws SQLException, IOException, DocumentException {
    ArrayPantalone arraypanta = new ArrayPantalone();
    arraypanta.ArrayIDpantalone(cliente, data);
    float importo = (float) 0.00;

    PdfContentByte content = s.getUnderContent(1);//1 for the first page
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED);
    content.setFontAndSize(bf, 7);//  ww  w  . j a  v a 2s .  com

    for (int j = 0; j < arraypanta.IDpantaloni.length; j++) {
        arraypanta.accessoPantaloni(j);
        importo = arraypanta.importoPanta(j);
        String convertitore = String.valueOf(importo); //Serve per convertire il ritorno del metodo importoUnitarioMaglia (float) a string per stamparlo,  solo un appoggio
        importi.add(importo);
        for (int z = 0; z < arraypanta.riga.length; z++) {
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, arraypanta.riga[z] + "   ", 95 + x, 538 - y, 0);
            x += 97; //sposta la coordinata y ad ogni stampa

        }
        content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Pantalone", 95 + x, 538 - y, 0);
        x += 97;
        content.showTextAligned(PdfContentByte.ALIGN_LEFT, convertitore, 95 + x, 538 - y, 0);
        x = 0;
        y += 20;
    }
}

From source file:fattura.Fattura.java

public void inserisciDatiGiubb(String cliente, String data, PdfStamper s)
        throws SQLException, IOException, DocumentException {
    ArrayGiubbotto arraygiubb = new ArrayGiubbotto();
    arraygiubb.ArrayIDgiubbotto(cliente, data);
    float importo;

    PdfContentByte content = s.getUnderContent(1);//1 for the first page
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED);
    content.setFontAndSize(bf, 7);// w ww.  jav  a 2  s  . c  o  m

    for (int j = 0; j < arraygiubb.IDgiubbotti.length; j++) {
        arraygiubb.accessoGiubbotti(j);
        importo = arraygiubb.importoGiubbotto(j);
        String convertitore = String.valueOf(importo); //Serve per convertire il ritorno del metodo importoUnitarioMaglia (float) a string per stamparlo,  solo un appoggio
        importi.add(importo);
        for (int z = 0; z < arraygiubb.riga.length; z++) {
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, arraygiubb.riga[z] + "   ", 95 + x, 538 - y, 0);
            x += 97; //sposta la coordinata y ad ogni stampa

        }
        content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Giubbotto", 95 + x, 538 - y, 0);
        x += 97;
        content.showTextAligned(PdfContentByte.ALIGN_LEFT, convertitore, 95 + x, 538 - y, 0);
        x = 0;
        y += 20;
    }

}

From source file:fattura.Fattura.java

public void inserisciDatiFelpe(String cliente, String data, PdfStamper s)
        throws SQLException, IOException, DocumentException {
    ArrayFelpa arrayfelpa = new ArrayFelpa();
    arrayfelpa.ArrayIDfelpa(cliente, data);
    float importo;

    PdfContentByte content = s.getUnderContent(1);//1 for the first page
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED);
    content.setFontAndSize(bf, 7);/*from   w w  w .  j a va2  s  .  c om*/

    for (int j = 0; j < arrayfelpa.IDfelpe.length; j++) {
        arrayfelpa.accessoFelpe(j);
        importo = arrayfelpa.importoFelpa(j);
        String convertitore = String.valueOf(importo); //Serve per convertire il ritorno del metodo importoUnitarioMaglia (float) a string per stamparlo,  solo un appoggio
        importi.add(importo);
        for (int z = 0; z < arrayfelpa.riga.length; z++) {
            content.showTextAligned(PdfContentByte.ALIGN_LEFT, arrayfelpa.riga[z] + "   ", 95 + x, 538 - y, 0);
            x += 97; //sposta la coordinata y ad ogni stampa

        }
        content.showTextAligned(PdfContentByte.ALIGN_LEFT, "Felpa", 95 + x, 538 - y, 0);
        x += 97;
        content.showTextAligned(PdfContentByte.ALIGN_LEFT, convertitore, 95 + x, 538 - y, 0);
        x = 0;
        y += 20;
    }
    //MI calcolar il prezzo totale man mano che calcolo i preventivi lo agggiungo!

}