Example usage for com.lowagie.text Document bottom

List of usage examples for com.lowagie.text Document bottom

Introduction

In this page you can find the example usage for com.lowagie.text Document bottom.

Prototype


public float bottom() 

Source Link

Document

Returns the lower left y-coordinate.

Usage

From source file:org.meveo.admin.action.billing.BillingAccountBean.java

License:Open Source License

public void generatePDF(long invoiceId) {
    Invoice invoice = invoiceService.findById(invoiceId);
    byte[] invoicePdf = invoice.getPdf();
    FacesContext context = FacesContext.getCurrentInstance();
    String invoiceFilename = null;
    BillingRun billingRun = invoice.getBillingRun();
    invoiceFilename = invoice.getInvoiceNumber() + ".pdf";
    if (billingRun != null && billingRun.getStatus() != BillingRunStatusEnum.VALIDATED) {
        invoiceFilename = "unvalidated-invoice.pdf";
    }//w  ww  . j  a va2 s .com

    HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
    response.setContentType("application/pdf"); // fill in
    response.setHeader("Content-disposition", "attachment; filename=" + invoiceFilename);

    try {
        OutputStream os = response.getOutputStream();
        Document document = new Document(PageSize.A4);
        if (billingRun != null && invoice.getBillingRun().getStatus() != BillingRunStatusEnum.VALIDATED) {
            // Add watemark image
            PdfReader reader = new PdfReader(invoicePdf);
            int n = reader.getNumberOfPages();
            PdfStamper stamp = new PdfStamper(reader, os);
            PdfContentByte over = null;
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.5f);
            int i = 1;
            while (i <= n) {
                over = stamp.getOverContent(i);
                over.setGState(gs);
                over.beginText();
                System.out.println("top=" + document.top() + ",bottom=" + document.bottom());
                over.setTextMatrix(document.top(), document.bottom());
                over.setFontAndSize(bf, 150);
                over.setColorFill(Color.GRAY);
                over.showTextAligned(Element.ALIGN_CENTER, "TEST", document.getPageSize().getWidth() / 2,
                        document.getPageSize().getHeight() / 2, 45);
                over.endText();
                i++;
            }

            stamp.close();
        } else {
            os.write(invoicePdf); // fill in PDF with bytes
        }

        // contentType
        os.flush();
        os.close();
        context.responseComplete();
    } catch (IOException e) {
        log.error("failed to generate PDF ", e);
    } catch (DocumentException e) {
        log.error("error in generation PDF ", e);
    }
}

From source file:org.opentestsystem.delivery.testreg.rest.view.PdfReportPageEventHelper.java

License:Open Source License

@Override
public void onEndPage(final PdfWriter writer, final Document document) {
    PdfContentByte cb = writer.getDirectContent();
    if (document.getPageNumber() == 1) {
        ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(""),
                (document.right() - document.left()) / 2 + document.leftMargin(), document.top() - 5, 0f);
    }//from w  w  w  . j  a v  a 2 s  .  co  m
    ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(""), document.left(), document.bottom() - 15,
            0f);

    int pageN = writer.getPageNumber();
    String text = "Page " + pageN + " of ";
    cb.beginText();
    cb.setFontAndSize(helv, 11);
    cb.setTextMatrix(document.right() - document.rightMargin() - 10, document.bottom() - 15);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(template, document.right(), document.bottom() - 15);

}

From source file:org.oscarehr.casemgmt.service.PageNumberStamper.java

License:Open Source License

public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();//from  w  ww. ja v  a2s. c om

    String text = "Page " + writer.getPageNumber() + " of ";

    // height where text starts 
    float textBase = document.bottom() - getBaseOffset();
    float textSize = getFont().getWidthPoint(text, getFontSize());
    float width = document.getPageSize().getWidth();
    float center = width / 2.0f;

    cb.beginText();
    cb.setFontAndSize(getFont(), getFontSize());

    cb.setTextMatrix(document.left(), textBase);
    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text, center, textBase, 0);
    cb.endText();
    cb.addTemplate(total, center + (textSize / 2.0f), textBase);

    cb.restoreState();
}

From source file:org.oscarehr.casemgmt.service.PromoTextStamper.java

License:Open Source License

/**
 * Adds promo text, date and current page number to each page
 * /* w w  w . j  a  v  a  2 s  .c o  m*/
 * @param writer
 * @param document
 */
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();

    float textBase = document.bottom() - getBaseOffset();
    float width = document.getPageSize().getWidth();
    float center = width / 2.0f;

    cb.beginText();
    cb.setFontAndSize(getFont(), getFontSize());

    cb.setTextMatrix(document.left(), textBase);
    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text, center, textBase, 0);
    cb.endText();
    cb.restoreState();
}

From source file:org.posterita.core.PDFReportPageEventHelper.java

License:Open Source License

public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();//w w  w.j ava2s . c om
    // write the headertable
    table.setTotalWidth(document.right() - document.left());
    table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 50, cb);
    // compose the footer
    String text = "Page " + writer.getPageNumber() + " of ";
    float textSize = PAGE_FOOTER_FONT.getBaseFont().getWidthPoint(text, 10);
    float textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(PAGE_FOOTER_FONT.getBaseFont(), 10);

    float adjust = PAGE_FOOTER_FONT.getBaseFont().getWidthPoint("0", 10);
    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(tpl, document.right() - adjust, textBase);

    cb.saveState();

    text = "Report Generated on : " + dateAndTime;

    textSize = PAGE_FOOTER_FONT.getBaseFont().getWidthPoint(text, 10);
    textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(PAGE_FOOTER_FONT.getBaseFont(), 10);

    adjust = PAGE_FOOTER_FONT.getBaseFont().getWidthPoint("0", 10);
    cb.setTextMatrix(MARGIN, textBase);
    cb.showText(text);
    cb.endText();

    cb.saveState();

}

From source file:org.revager.export.PDFPageEventHelper.java

License:Open Source License

@Override
public void onEndPage(PdfWriter writer, Document document) {
    int columnNumber;

    try {//from  w  w  w  .  jav  a  2  s .  c o  m
        Rectangle page = document.getPageSize();
        float pageWidth = page.getWidth() - document.leftMargin() - document.rightMargin();

        /*
         * Write marks
         */
        setMarks(writer, document);

        /*
         * Define fonts
         */
        headBaseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        Font headFont = new Font(headBaseFont, headFontSize);

        footBaseFont = BaseFont.createFont(BaseFont.HELVETICA_OBLIQUE, BaseFont.CP1252, BaseFont.EMBEDDED);
        Font footFont = new Font(footBaseFont, footFontSize);

        /*
         * Cell fill for space between head/foot and content
         */
        PdfPCell cellFill = new PdfPCell();
        cellFill.setMinimumHeight(PDFTools.cmToPt(0.8f));
        cellFill.setBorderWidth(0);

        /*
         * Write head
         */
        if (headLogoPath != null) {
            columnNumber = 2;
        } else {
            columnNumber = 1;
        }

        PdfPTable head = new PdfPTable(columnNumber);

        Phrase phraseTitle = new Phrase(headTitle, headFont);

        PdfPCell cellTitle = new PdfPCell(phraseTitle);
        cellTitle.setHorizontalAlignment(Element.ALIGN_LEFT);
        cellTitle.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cellTitle.setPaddingTop(0);
        cellTitle.setPaddingBottom(PDFTools.cmToPt(0.2f));
        cellTitle.setPaddingLeft(0);
        cellTitle.setPaddingRight(0);
        cellTitle.setBorderWidthTop(0);
        cellTitle.setBorderWidthBottom(0.5f);
        cellTitle.setBorderWidthLeft(0);
        cellTitle.setBorderWidthRight(0);

        head.addCell(cellTitle);

        if (headLogoPath != null) {
            Image headLogo = Image.getInstance(headLogoPath);
            headLogo.scaleToFit(PDFTools.cmToPt(5.0f), PDFTools.cmToPt(1.1f));

            PdfPCell cellLogo = new PdfPCell(headLogo);
            cellLogo.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cellLogo.setVerticalAlignment(Element.ALIGN_BOTTOM);
            cellLogo.setPaddingTop(0);
            cellLogo.setPaddingBottom(PDFTools.cmToPt(0.15f));
            cellLogo.setPaddingLeft(0);
            cellLogo.setPaddingRight(0);
            cellLogo.setBorderWidthTop(0);
            cellLogo.setBorderWidthBottom(0.5f);
            cellLogo.setBorderWidthLeft(0);
            cellLogo.setBorderWidthRight(0);

            head.addCell(cellLogo);

            head.addCell(cellFill);
        }

        head.addCell(cellFill);

        head.setTotalWidth(pageWidth);
        head.writeSelectedRows(0, -1, document.leftMargin(),
                page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());

        /*
         * Write foot
         */
        if (footText == null) {
            footText = " ";
        }

        PdfPTable foot = new PdfPTable(1);

        foot.addCell(cellFill);

        PdfPCell cellFootText = new PdfPCell(new Phrase(footText, footFont));
        cellFootText.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cellFootText.setVerticalAlignment(Element.ALIGN_TOP);
        cellFootText.setPaddingTop(PDFTools.cmToPt(0.15f));
        cellFootText.setPaddingBottom(0);
        cellFootText.setPaddingLeft(0);
        cellFootText.setPaddingRight(0);
        cellFootText.setBorderWidthTop(0.5f);
        cellFootText.setBorderWidthBottom(0);
        cellFootText.setBorderWidthLeft(0);
        cellFootText.setBorderWidthRight(0);

        foot.addCell(cellFootText);

        /*
         * Print page numbers
         */
        PdfContentByte contentByte = writer.getDirectContent();
        contentByte.saveState();

        String text = MessageFormat.format(translate("Page {0} of") + " ", writer.getPageNumber());

        float textSize = footBaseFont.getWidthPoint(text, footFontSize);
        float textBase = document.bottom() - PDFTools.cmToPt(1.26f);
        contentByte.beginText();
        contentByte.setFontAndSize(footBaseFont, footFontSize);

        float adjust;
        if (footText.trim().equals("")) {
            adjust = (pageWidth / 2) - (textSize / 2) - footBaseFont.getWidthPoint("0", footFontSize);
        } else {
            adjust = 0;
        }

        contentByte.setTextMatrix(document.left() + adjust, textBase);
        contentByte.showText(text);
        contentByte.endText();
        contentByte.addTemplate(template, document.left() + adjust + textSize, textBase);

        contentByte.stroke();
        contentByte.restoreState();

        foot.setTotalWidth(pageWidth);
        foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                writer.getDirectContent());
    } catch (Exception e) {
        /*
         * Not part of unit testing because this exception is only thrown if
         * an internal error occurs.
         */
        throw new ExceptionConverter(e);
    }
}

From source file:org.sonar.report.pdf.Events.java

License:Open Source License

private void printPageNumber(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();//from   ww  w  .  j  av  a2s. c om
    float textBase = document.bottom() - 20;
    try {
        cb.setFontAndSize(BaseFont.createFont("Helvetica", BaseFont.WINANSI, false), 12);
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    cb.beginText();
    cb.setTextMatrix(document.right() - 10, textBase);
    cb.showText(String.valueOf(writer.getPageNumber()));
    cb.endText();
    cb.saveState();
}

From source file:org.sonarqube.report.extendedpdf.ExtendedEvents.java

License:Open Source License

private void printPageNumber(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();// ww w .  j av  a  2s  . c  om
    float textBase = document.bottom() - 45;
    try {
        cb.setFontAndSize(BaseFont.createFont("Helvetica", BaseFont.WINANSI, false), 12);
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    cb.beginText();
    cb.setTextMatrix(document.right() - 10, textBase);
    cb.showText(String.valueOf(writer.getPageNumber()));
    cb.endText();
    cb.saveState();
}

From source file:questions.objects.DifferentLeadings.java

public static void main(String[] args) {
    Document document = new Document(PageSize.A7);
    try {//  w  ww.  j  a  v a  2 s. c  om
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        Chunk space = new Chunk(' ');
        String text = "Quick brown fox jumps over the lazy dog.";
        Phrase phrase1 = new Phrase(text, new Font(Font.HELVETICA, 12));
        Phrase phrase2 = new Phrase(new Chunk(text, new Font(Font.TIMES_ROMAN, 24)));
        Phrase phrase3 = new Phrase(text, new Font(Font.COURIER, 8));
        Phrase phrase4 = new Phrase(text, new Font(Font.HELVETICA, 4));
        Paragraph paragraph = new Paragraph();
        paragraph.add(phrase1);
        paragraph.add(space);
        paragraph.add(phrase2);
        paragraph.add(space);
        paragraph.add(phrase3);
        paragraph.add(space);
        paragraph.add(phrase4);
        paragraph.setMultipliedLeading(1.5f);
        paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(document.left(), document.bottom(), document.right(), document.top());
        column.addElement(paragraph);
        column.go();
        document.newPage();
        document.add(paragraph);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}

From source file:questions.separators.TabbedWords2.java

public static void main(String[] args) {
    Document document = new Document();
    try {/*from  w w w. ja  v  a2 s.co m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();

        Paragraph p;
        Chunk tab1 = new Chunk(new VerticalPositionMark(), 100, true);
        Chunk tab2 = new Chunk(new LineSeparator(), 120, true);
        Chunk tab3 = new Chunk(new DottedLineSeparator(), 200, true);
        Chunk tab4 = new Chunk(new VerticalPositionMark(), 240, true);
        Chunk tab5 = new Chunk(new VerticalPositionMark(), 300, true);

        PdfContentByte canvas = writer.getDirectContent();
        ColumnText column = new ColumnText(canvas);
        for (int i = 0; i < 40; i++) {
            p = new Paragraph("TEST");
            p.add(tab1);
            p.add(new Chunk(String.valueOf(i)));
            p.add(tab2);
            p.add(new Chunk(String.valueOf(i * 2)));
            p.add(tab3);
            p.add(new Chunk(SeparatedWords2.WORDS[39 - i]));
            p.add(tab4);
            p.add(new Chunk(String.valueOf(i * 4)));
            p.add(tab5);
            p.add(new Chunk(SeparatedWords2.WORDS[i]));
            column.addElement(p);
        }
        column.setSimpleColumn(60, 36, 400, 806);
        canvas.moveTo(60, 36);
        canvas.lineTo(60, 806);
        canvas.moveTo(160, 36);
        canvas.lineTo(160, 806);
        canvas.moveTo(180, 36);
        canvas.lineTo(180, 806);
        canvas.moveTo(260, 36);
        canvas.lineTo(260, 806);
        canvas.moveTo(300, 36);
        canvas.lineTo(300, 806);
        canvas.moveTo(360, 36);
        canvas.lineTo(360, 806);
        canvas.moveTo(400, 36);
        canvas.lineTo(400, 806);
        canvas.stroke();
        column.go();

        document.setMargins(60, 195, 36, 36);
        document.newPage();

        canvas.moveTo(document.left(), document.bottom());
        canvas.lineTo(document.left(), document.top());
        canvas.moveTo(document.right(), document.bottom());
        canvas.lineTo(document.right(), document.top());
        canvas.stroke();
        for (int i = 0; i < 40; i++) {
            p = new Paragraph("TEST");
            p.add(tab1);
            p.add(new Chunk(String.valueOf(i)));
            p.add(tab2);
            p.add(new Chunk(String.valueOf(i * 2)));
            p.add(tab3);
            p.add(new Chunk(SeparatedWords2.WORDS[39 - i]));
            p.add(tab4);
            p.add(new Chunk(String.valueOf(i * 4)));
            p.add(tab5);
            p.add(new Chunk(SeparatedWords2.WORDS[i]));
            document.add(p);
        }
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}