Example usage for com.lowagie.text.pdf PdfWriter getPageNumber

List of usage examples for com.lowagie.text.pdf PdfWriter getPageNumber

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter getPageNumber.

Prototype


public int getPageNumber() 

Source Link

Document

Gets the pagenumber of this document.

Usage

From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java

License:Open Source License

private void buildFooter(final PdfWriter writer, final Document document) {
    PdfContentByte cb = writer.getDirectContent();

    cb.saveState();/*  www .  j  a  v a  2 s  .  co m*/

    String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " ";

    float textBase = document.bottom() - 25;
    float textSize = FontUtils.getDejavu().getWidthPoint(text, 7);

    cb.setColorFill(ColorUtils.getLightColor());
    cb.setColorStroke(ColorUtils.getLightColor());
    cb.setLineWidth(1);
    cb.setLineDash(2, 2, 1);
    cb.moveTo(document.left(), document.bottom() - 10);
    cb.lineTo(document.right(), document.bottom() - 10);
    cb.stroke();
    cb.beginText();
    cb.setFontAndSize(FontUtils.getDejavu(), 7);

    float adjust = FontUtils.getDejavu().getWidthPoint("0", 7);

    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);

    textSize = FontUtils.getDejavu().getWidthPoint(footer.getGeneratedBy(), 7);

    cb.setTextMatrix(document.right() - textSize, textBase - 10);
    cb.showText(footer.getGeneratedBy());

    textSize = FontUtils.getDejavu().getWidthPoint(generationDate, 7);

    cb.setTextMatrix(document.right() - textSize, textBase - 20);
    cb.showText(generationDate);
    cb.endText();

    try {
        textSize = FontUtils.getDejavu().getWidthPoint(footer.getAdditionalText(), 7);

        ColumnText ct = new ColumnText(cb);

        ct.setSimpleColumn(new Phrase(footer.getAdditionalText(), FontUtils.getDejavuRegular7Light()),
                document.left() + 240, textBase + 10, document.left() + 390, textBase - 25, 10,
                Element.ALIGN_LEFT);
        ct.go();
    } catch (DocumentException e) {
        LOG.warn("Problem with additional text generation in report footer.");
    }

    try {
        ColumnText ct = new ColumnText(cb);

        ct.setSimpleColumn(document.left(), textBase + 10, document.left() + 230, textBase - 25, 10,
                Element.ALIGN_LEFT);
        ct.addText(new Phrase(footer.getCompanyName() + "\n", FontUtils.getDejavuRegular7Light()));

        if (!"".equals(footer.getAddress())) {
            ct.addText(new Phrase(footer.getAddress() + "\n", FontUtils.getDejavuRegular7Light()));
        }
        if (!"".equals(footer.getPhoneEmail())) {
            ct.addText(new Phrase(footer.getPhoneEmail(), FontUtils.getDejavuRegular7Light()));
        }

        ct.go();
    } catch (DocumentException e) {
        LOG.warn("Problem with company text generation in report footer.");
    }

    cb.addTemplate(total, document.right() - adjust, textBase);
    cb.restoreState();

}

From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java

License:Open Source License

private void buildHeader(final PdfWriter writer, final Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();// ww w  .  j a va 2  s  .  c o  m
    String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " ";

    float textBase = document.top() + 22;
    float textSize = FontUtils.getDejavu().getWidthPoint(text, 7);

    cb.setColorFill(ColorUtils.getLightColor());
    cb.setColorStroke(ColorUtils.getLightColor());
    cb.beginText();
    cb.setFontAndSize(FontUtils.getDejavu(), 7);

    float adjust = FontUtils.getDejavu().getWidthPoint("0", 7);

    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(total, document.right() - adjust, textBase);
    cb.setLineWidth(1);
    cb.setLineDash(2, 2, 1);
    cb.moveTo(document.left(), document.top() + 12);
    cb.lineTo(document.right(), document.top() + 12);
    cb.stroke();
    cb.restoreState();

}

From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java

License:Open Source License

/**
 * @see com.lowagie.text.pdf.PdfPageEvent#onCloseDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 *///from   w ww . j  a  v  a 2 s . c om
@Override
public void onCloseDocument(final PdfWriter writer, final Document document) {
    total.beginText();
    total.setFontAndSize(FontUtils.getDejavu(), 7);
    total.setTextMatrix(0, 0);
    total.showText(String.valueOf(writer.getPageNumber() - 1));
    total.endText();
}

From source file:de.tr1.cooperator.manager.web.CreateSubscriberListActionHelper.java

License:Open Source License

/**
 * After the content of the page is written, we put page X / Y
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 *///  w ww.  j a  v  a2 s. c  om
public void onEndPage(PdfWriter writer, Document document) {
    int pageN = writer.getPageNumber();
    String text = "" + pageN;
    String separator = " / ";
    float text_len = bf.getWidthPoint(text, 8);
    float sep_len = bf.getWidthPoint(separator, 8);
    cb.beginText();
    cb.setFontAndSize(bf, 8);
    float absMiddle = (document.right() - document.left()) / 2 + document.left();
    cb.setTextMatrix(absMiddle - text_len - (sep_len / 2), 30);
    cb.showText(text);
    cb.setTextMatrix(absMiddle - (sep_len / 2), 30);
    cb.showText(separator);
    cb.endText();
    cb.addTemplate(template, absMiddle + (sep_len / 2), 30);

    // write the headertable
    tHeader.setTotalWidth(document.right() - document.left());
    tHeader.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 20, cb);
}

From source file:de.tr1.cooperator.manager.web.CreateSubscriberListActionHelper.java

License:Open Source License

/**
 * Just before the document is closed, we add the final number of pages to
 * the template.//  w w  w  .j  av a  2 s.c o  m
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onCloseDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onCloseDocument(PdfWriter writer, Document document) {
    template.beginText();
    template.setFontAndSize(bf, 8);
    template.showText(String.valueOf(writer.getPageNumber() - 1));
    template.endText();
}

From source file:dinamica.PDFPageEvents.java

License:LGPL

/**
 * Print value on empty template (total number of pages), this
 * will update footer on every page where the template was inserted
 *///from   w w  w.  j  ava 2  s . c  o m
public void onCloseDocument(PdfWriter writer, Document document) {
    // print total number of pages into template
    // this will update footer on every page
    int pageNum = writer.getPageNumber() - 1;
    tpl.beginText();
    tpl.setFontAndSize(bf, 10);
    tpl.showText(String.valueOf(pageNum));
    tpl.endText();
}

From source file:dinamica.PDFPageEvents.java

License:LGPL

/**
 * Create custom-made footer on every page, this
 * will place the empty template on every page
 *//*from  ww  w .j  av  a2 s .  c  om*/
public void onEndPage(PdfWriter writer, Document document) {

    // print (x of ...) on every page (bottom right)
    // append template at the end of the footer, like:
    // 1 of [template] - template is an empty box
    String footer = writer.getPageNumber() + this.pageXofY;
    float tWidth = bf.getWidthPoint(footer, 10);
    float extraSpace = bf.getWidthPoint("00", 10);
    float ty = document.bottom() - 12; //below bottom margin
    float tx = document.right() - tWidth - extraSpace; //x coordinate for the footer

    // print "X of " on right-side and footer + date on left side
    cb.beginText();
    cb.setFontAndSize(bf, 10);
    cb.showTextAligned(PdfContentByte.ALIGN_LEFT, footer, tx, ty, 0);
    cb.showTextAligned(PdfContentByte.ALIGN_LEFT, footerLeft, document.left(), ty, 0);
    cb.endText();

    // now append empty template after "X of " 
    cb.addTemplate(tpl, document.right() - extraSpace, ty);

}

From source file:docet.engine.PDFDocumentHandler.java

License:Apache License

private void writePDF(DocumentPart part, int totalPages, com.lowagie.text.Document pdf, PdfWriter writer)
        throws DocumentException {

    int initialPageNo = writer.getPageNumber();

    LOGGER.log(Level.FINE, "Writing {0} - {1}: {3} pages from {2}",
            new Object[] { title, part.name, initialPageNo, part.pages.size(), initialPageNo });

    part.startPageNo = initialPageNo;//from  w  w w. j  a va 2s . com

    RenderingContext renderingContext = newRenderingContext(part.root);
    renderingContext.setInitialPageNo(initialPageNo);

    if (initialPageNo > 0) {
        renderingContext.setPageCount(totalPages - initialPageNo + 1);
    } else {
        renderingContext.setPageCount(totalPages);
    }

    outputDevice.setRoot(part.root);

    outputDevice.start(part.doc);
    outputDevice.setWriter(writer);
    outputDevice.setStartPageNo(initialPageNo - 1);

    part.root.getLayer().assignPagePaintingPositions(renderingContext, Layer.PAGED_MODE_PRINT);

    int pageNo = 0;
    for (PageBox page : part.pages) {

        if (Thread.currentThread().isInterrupted())
            throw new RuntimeException("Timeout occured");

        com.lowagie.text.Rectangle pageSize = new com.lowagie.text.Rectangle(0, 0,
                page.getWidth(renderingContext) / dotsPerPoint,
                page.getHeight(renderingContext) / dotsPerPoint);

        pdf.setPageSize(pageSize);

        outputDevice.initializePage(writer.getDirectContent(), pageSize.getHeight());

        renderingContext.setPage(pageNo++, page);

        paintPage(renderingContext, writer, page, part.root);
        outputDevice.finishPage();

        /* Advance page */
        pdf.newPage();
    }

    outputDevice.finish(renderingContext, part.root);
    cleanOutputDevice();
}

From source file:fr.aliasource.webmail.server.export.ConversationPdfEventHandler.java

License:GNU General Public License

/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter,
 *      com.lowagie.text.Document)//from   w w  w .  jav a 2 s. co m
 */
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    // write the headertable
    table.setTotalWidth(document.right() - document.left());
    table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 20, cb);
    // compose the footer
    String text = writer.getPageNumber() + " / ";
    float textSize = helv.getWidthPoint(text, 12);
    float textBase = document.bottom() - 49;
    cb.beginText();
    cb.setFontAndSize(helv, 12);
    float adjust = helv.getWidthPoint("0", 12);
    cb.setTextMatrix(document.right() - textSize - adjust - 5, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(tpl, document.right() - adjust, textBase);
    cb.saveState();
    // draw a Rectangle around the page
    cb.setLineWidth(1);
    cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40);
    cb.stroke();
    cb.restoreState();
}

From source file:fr.aliasource.webmail.server.export.ConversationPdfEventHandler.java

License:GNU General Public License

/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onCloseDocument(com.lowagie.text.pdf.PdfWriter,
 *      com.lowagie.text.Document)//  w ww .  j a v  a 2s . c  om
 */
public void onCloseDocument(PdfWriter writer, Document document) {
    tpl.beginText();
    tpl.setFontAndSize(helv, 12);
    tpl.setTextMatrix(-5, 0);
    tpl.showText("" + (writer.getPageNumber() - 1));
    tpl.endText();
}