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

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

Introduction

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

Prototype

int ALIGN_CENTER

To view the source code for com.lowagie.text.pdf PdfContentByte ALIGN_CENTER.

Click Source Link

Document

The alignment is center

Usage

From source file:oscar.oscarLab.ca.all.pageUtil.LabPDFCreator.java

License:Open Source License

public void onEndPage(PdfWriter writer, Document document) {
    try {// w ww  .  j  av  a2  s .c om

        Rectangle page = document.getPageSize();
        PdfContentByte cb = writer.getDirectContent();
        BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        int pageNum = document.getPageNumber();
        float width = page.getWidth();
        float height = page.getHeight();

        //add patient name header for every page but the first.
        if (pageNum > 1) {
            cb.beginText();
            cb.setFontAndSize(bf, 8);
            cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, handler.getPatientName(), 575, height - 30, 0);
            cb.endText();

        }

        //add footer for every page
        cb.beginText();
        cb.setFontAndSize(bf, 8);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "-" + pageNum + "-", width / 2, 30, 0);
        cb.endText();

        // add promotext as footer if it is enabled
        if (OscarProperties.getInstance().getProperty("FORMS_PROMOTEXT") != null) {
            cb.beginText();
            cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED),
                    6);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
                    OscarProperties.getInstance().getProperty("FORMS_PROMOTEXT"), width / 2, 19, 0);
            cb.endText();
        }

        // throw any exceptions
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:oscar.oscarPrevention.pageUtil.PreventionPrintPdf.java

License:Open Source License

public void printPdf(String[] headerIds, HttpServletRequest request, OutputStream outputStream)
        throws IOException, DocumentException {
    //make sure we have data to print
    //String[] headerIds = request.getParameterValues("printHP");
    if (headerIds == null)
        throw new DocumentException();

    //Create the document we are going to write to
    document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    document.setPageSize(PageSize.LETTER);
    document.open();/* ww  w  . j  av  a  2  s . c o  m*/

    //Create the font we are going to print to
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    Font font = new Font(bf, FONTSIZE, Font.NORMAL);
    float leading = font.getCalculatedLeading(LINESPACING);

    //set up document title and header
    String title = "Preventions for " + request.getParameter("nameAge");
    String hin = "HIN: " + request.getParameter("hin");
    String mrp = request.getParameter("mrp");
    if (mrp != null) {
        Properties prop = (Properties) request.getSession().getAttribute("providerBean");
        mrp = "MRP: " + prop.getProperty(mrp, "unknown");
    }

    ClinicData clinicData = new ClinicData();
    clinicData.refreshClinicData();
    String[] clinic = new String[] { clinicData.getClinicName(), clinicData.getClinicAddress(),
            clinicData.getClinicCity() + ", " + clinicData.getClinicProvince(), clinicData.getClinicPostal(),
            clinicData.getClinicPhone(), title, hin, mrp };

    //Header will be printed at top of every page beginning with p2
    Phrase headerPhrase = new Phrase(LEADING, title, font);
    HeaderFooter header = new HeaderFooter(headerPhrase, false);
    header.setAlignment(HeaderFooter.ALIGN_CENTER);
    document.setHeader(header);

    //Write title with top and bottom borders on p1
    cb = writer.getDirectContent();
    cb.setColorStroke(new Color(0, 0, 0));
    cb.setLineWidth(0.5f);

    cb.moveTo(document.left(), document.top());
    cb.lineTo(document.right(), document.top());
    cb.stroke();
    cb.setFontAndSize(bf, FONTSIZE);

    upperYcoord = document.top() - (font.getCalculatedLeading(LINESPACING) * 2f);
    cb.beginText();
    for (int idx = 0; idx < clinic.length; ++idx) {
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, clinic[idx], document.right() / 2f, upperYcoord, 0f);
        upperYcoord -= font.getCalculatedLeading(LINESPACING);
    }

    cb.endText();
    cb.moveTo(document.left(), upperYcoord);
    cb.lineTo(document.right(), upperYcoord);
    cb.stroke();

    //get top y-coord for starting to print columns
    upperYcoord = cb.getYTLM() - font.getCalculatedLeading(LINESPACING * 2f);

    int subIdx;
    String preventionHeader, procedureAge, procedureDate;

    //1 - obtain number of lines of incoming prevention data
    numLines = 0;
    for (int idx = 0; idx < headerIds.length; ++idx) {
        ++numLines;
        subIdx = 0;
        while (request.getParameter("preventProcedureAge" + headerIds[idx] + "-" + subIdx) != null) {
            ++subIdx;
            numLines += 3;
        }
        numLines += 2;
    }

    //2 - calculate max num of lines a page can hold and number of pages of data we have
    pageHeight = upperYcoord - document.bottom();
    maxLines = (int) Math.floor(pageHeight / (font.getCalculatedLeading(LINESPACING) + 4d));
    numPages = (int) Math.ceil(numLines / ((double) maxLines * NUMCOLS));

    //3 - Start the column
    ct = new ColumnText(cb);
    ct.setSimpleColumn(document.left(), document.bottom(), document.right() / 2f, upperYcoord);

    linesToBeWritten = linesWritten = 0;
    boolean pageBreak = false;

    curPage = 1;
    totalLinesWritten = 0;

    //add promotext to current page
    addPromoText();

    //if we have > 1 element but less than a page of data, shrink maxLines so we can try to balance text in columns
    if (headerIds.length > 1) {
        if (curPage == numPages) {
            maxLines = numLines / NUMCOLS;
        }
    }

    //now we can start to print the prevention data
    for (int idx = 0; idx < headerIds.length; ++idx) {

        linesToBeWritten = 4; //minimum lines for header and one prevention item
        pageBreak = checkColumnFill(ct, "", font, pageBreak); //if necessary break before we print prevention header

        preventionHeader = request.getParameter("preventionHeader" + headerIds[idx]);
        Phrase procHeader = new Phrase(LEADING, "Prevention " + preventionHeader + "\n", font);
        ct.addText(procHeader);
        subIdx = 0;

        while ((procedureAge = request
                .getParameter("preventProcedureAge" + headerIds[idx] + "-" + subIdx)) != null) {
            procedureDate = request.getParameter("preventProcedureDate" + headerIds[idx] + "-" + subIdx);

            linesToBeWritten = 3;
            pageBreak = checkColumnFill(ct, preventionHeader, font, pageBreak);

            Phrase procedure = new Phrase(LEADING, "     " + procedureAge + "\n", font);
            procedure.add("     " + procedureDate + "\n\n");
            ct.addText(procedure);
            ct.go();
            linesWritten += ct.getLinesWritten();
            totalLinesWritten += ct.getLinesWritten();
            ++subIdx;
        }

    }

    ColumnText.showTextAligned(cb, Phrase.ALIGN_CENTER, new Phrase("-" + curPage + "-"), document.right() / 2f,
            document.bottom() - (document.bottomMargin() / 2f), 0f);

    document.close();
}

From source file:oscar.oscarPrevention.pageUtil.PreventionPrintPdf.java

License:Open Source License

private void addPromoText() throws DocumentException, IOException {
    if (OscarProperties.getInstance().getProperty("FORMS_PROMOTEXT") != null) {
        cb.beginText();/*from w w  w .j  a  v  a 2  s.com*/
        cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 6);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
                OscarProperties.getInstance().getProperty("FORMS_PROMOTEXT"), PageSize.LETTER.getWidth() / 2, 5,
                0);
        cb.endText();
    }
}

From source file:papertoolkit.pattern.output.PDFPatternGenerator.java

License:BSD License

/**
 * Render the given pattern starting at the designated origin. Also render a white box underneath the
 * pattern, if we were asked to do so... This helps the pattern stand out better.
 * /*from   ww w  . j a va 2s.  co m*/
 * @param pattern
 * @param xOrigin
 * @param yOrigin
 */
public void renderPattern(TiledPattern pattern, Units xOrigin, Units yOrigin) {
    // flip the transform so that the top left of the page is 0,0

    float heightOfPDF = (float) height.getValueInPoints();

    // convert the origins to Points
    final double xOrigInPoints = xOrigin.getValueInPoints();
    final double yOrigInPoints = yOrigin.getValueInPoints();

    final int numRows = pattern.getNumTotalRows();
    final int numCols = pattern.getNumTotalColumns();

    // this actually mirrors everything
    // text will display upside down!
    // this doesn't matter for symmetrical dots, though
    // content.concatCTM(1f, 0f, 0f, -1f, 0f, heightOfPDF);

    // work in hundredths of a millimeter
    final float heightInHundredths = (float) (heightOfPDF * convertPointsToHundredthsOfMM);

    // if we use the font approach
    if (!useTemplateInsteadOfFont) {
        content.beginText();
        // GRAY, etc. do not work! The printer will do halftoning, which messes things up.
        content.setFontAndSize(patternFont, fontSize);
    }

    content.setColorFill(patternColor);
    // content.setColorFill(Color.CYAN);
    // content.setColorFill(Color.BLACK);

    final int initX = MathUtils.rint(xOrigInPoints * convertPointsToHundredthsOfMM);

    int gridXPosition = initX;
    int gridYPosition = MathUtils.rint(yOrigInPoints * convertPointsToHundredthsOfMM);

    // DebugUtils.println("PDFPatternGenerator: Dot Position is " + gridXPosition + " " + gridYPosition);

    int xJitter = 0;
    int yJitter = 0;
    char currentJitterDirection;

    for (int row = 0; row < numRows; row++) {

        final String patternRow = pattern.getPatternOnRow(row);
        final int rowLength = patternRow.length();

        for (int i = 0; i < rowLength; i++) {

            // read the direction
            currentJitterDirection = patternRow.charAt(i);

            // reset the jitters (this is key!)
            xJitter = 0;
            yJitter = 0;

            switch (currentJitterDirection) {
            case PatternJitter.DOWN:
                // System.out.print("d");
                yJitter = DEFAULT_JITTER;
                break;
            case PatternJitter.UP:
                // System.out.print("u");
                yJitter = -DEFAULT_JITTER;
                break;
            case PatternJitter.LEFT:
                // System.out.print("l");
                xJitter = -DEFAULT_JITTER;
                break;
            case PatternJitter.RIGHT:
                // System.out.print("r");
                xJitter = DEFAULT_JITTER;
                break;
            }

            if (useTemplateInsteadOfFont) {
                content.addTemplate(dotTemplate, gridXPosition + xJitter, //
                        heightInHundredths - (gridYPosition + yJitter));
            } else {
                content.showTextAligned(PdfContentByte.ALIGN_CENTER, dotSymbol, //
                        gridXPosition + xJitter + X_FONT_OFFSET, //
                        heightInHundredths - (gridYPosition + yJitter + Y_FONT_OFFSET), 0);
            }

            gridXPosition += DEFAULT_PADDING;

        }
        gridXPosition = initX;
        gridYPosition += DEFAULT_PADDING;
        // System.out.println();
    }

    if (!useTemplateInsteadOfFont) {
        content.endText();
    }
}