Example usage for com.lowagie.text Font getCalculatedLeading

List of usage examples for com.lowagie.text Font getCalculatedLeading

Introduction

In this page you can find the example usage for com.lowagie.text Font getCalculatedLeading.

Prototype

public float getCalculatedLeading(float linespacing) 

Source Link

Document

Gets the leading that can be used with this font.

Usage

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();/*w w w . ja v  a2  s  .com*/

    //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 boolean checkColumnFill(ColumnText ct, String preventionHeader, Font font, boolean pageBreak) {
    if (linesWritten + linesToBeWritten > maxLines) {

        //reset upperYcoord for pages 2 and above
        if (curPage == 1 && pageBreak) {
            upperYcoord = document.top() - font.getCalculatedLeading(LINESPACING);
            pageHeight = upperYcoord - document.bottom();
            maxLines = (int) Math.floor(pageHeight / (font.getCalculatedLeading(LINESPACING) + 4d));
            numPages = (int) Math.ceil(((double) numLines - totalLinesWritten) / ((double) maxLines * NUMCOLS))
                    + 1;/*from  w w  w.j a v  a  2s  .c o m*/
        }
        pageBreak = breakPage(pageBreak, upperYcoord);
        linesWritten = 0;
        if (preventionHeader.length() > 0) {
            Phrase procHeaderContd = new Phrase(LEADING, "Prevention " + preventionHeader + " cont'd\n", font);
            ct.addText(procHeaderContd);
            ++linesToBeWritten;
        }

    }

    return pageBreak;
}