Example usage for com.lowagie.text Document getPageSize

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

Introduction

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

Prototype


public Rectangle getPageSize() 

Source Link

Document

Gets the pagesize.

Usage

From source file:org.kuali.ole.module.purap.pdf.PurapPdf.java

License:Educational Community License

/**
 * Overrides the method in PdfPageEventHelper from itext to write the headerTable, compose the footer and show the
 * footer./*from   w ww.  j a v a  2s .c  o  m*/
 *
 * @param writer   The PdfWriter for this document.
 * @param document The document.
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
 */
public void onEndPage(PdfWriter writer, Document document) {
    LOG.debug("onEndPage() started.");
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    // write the headerTable
    headerTable.setTotalWidth(document.right() - document.left());
    headerTable.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 10, cb);
    // compose the footer
    String text = "Page " + writer.getPageNumber() + " of ";
    float textSize = helv.getWidthPoint(text, 12);
    float textBase = document.bottom() - 20;
    cb.beginText();
    cb.setFontAndSize(helv, 12);
    // show the footer
    float adjust = helv.getWidthPoint("0", 12);
    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(tpl, document.right() - adjust, textBase);
    cb.saveState();
}

From source file:org.mapfish.print.PDFCustomBlocks.java

License:Open Source License

private void addBackground(PdfWriter writer, Document document, PdfContentByte dc) {
    if (backgroundPdf != null) {
        try {/*ww  w. j  a  v  a 2s. c  o  m*/
            PdfImportedPage page = backgroundPdfs.get(backgroundPdf);
            if (page == null) {
                PdfReader reader = new PdfReader(backgroundPdf);
                page = writer.getImportedPage(reader, 1);
                backgroundPdfs.put(backgroundPdf, page);
            }
            final Rectangle pageSize = document.getPageSize();
            final boolean rotate = (page.getWidth() < page.getHeight())
                    ^ (pageSize.getWidth() < pageSize.getHeight());
            if (rotate) {
                dc.addTemplate(page, 0, -1, 1, 0, 0, pageSize.getHeight());
            } else {
                dc.addTemplate(page, 0, 0);
            }
        } catch (IOException e) {
            addError(e);
        }
    }
}

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 w  w .  j  a  v a2s .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.nuxeo.dam.pdf.export.PDFCreator.java

License:Open Source License

public boolean createPDF(String title, OutputStream out) throws ClientException {
    try {//from   w  ww  .  j  a  v  a  2s .  c  om
        Document document = new Document();
        PdfWriter.getInstance(document, out);

        document.addTitle(title);
        document.addAuthor(Functions.principalFullName(currentUser));
        document.addCreator(Functions.principalFullName(currentUser));

        document.open();

        document.add(new Paragraph("\n\n\n\n\n\n\n\n\n\n"));
        Font titleFont = new Font(Font.HELVETICA, 36, Font.BOLD);
        Paragraph titleParagraph = new Paragraph(title, titleFont);
        titleParagraph.setAlignment(Element.ALIGN_CENTER);
        document.add(titleParagraph);
        Font authorFont = new Font(Font.HELVETICA, 20);
        Paragraph authorParagraph = new Paragraph("By " + Functions.principalFullName(currentUser), authorFont);
        authorParagraph.setAlignment(Element.ALIGN_CENTER);
        document.add(authorParagraph);

        document.newPage();

        boolean foundOnePicture = false;
        for (DocumentModel doc : docs) {
            if (!doc.hasSchema(PICTURE_SCHEMA)) {
                continue;
            }
            foundOnePicture = true;

            PictureResourceAdapter picture = doc.getAdapter(PictureResourceAdapter.class);
            Blob blob = picture.getPictureFromTitle(ORIGINAL_JPEG_VIEW);
            Rectangle pageSize = document.getPageSize();
            if (blob != null) {
                Paragraph imageTitle = new Paragraph(doc.getTitle(), font);
                imageTitle.setAlignment(Paragraph.ALIGN_CENTER);
                document.add(imageTitle);

                Image image = Image.getInstance(blob.getByteArray());
                image.scaleToFit(pageSize.getWidth() - 20, pageSize.getHeight() - 100);
                image.setAlignment(Image.MIDDLE);
                Paragraph imageParagraph = new Paragraph();
                imageParagraph.add(image);
                imageParagraph.setAlignment(Paragraph.ALIGN_MIDDLE);
                document.add(imageParagraph);

                document.newPage();
            }
        }
        if (foundOnePicture) {
            document.close();
            return true;
        }
    } catch (Exception e) {
        throw ClientException.wrap(e);
    }
    return false;
}

From source file:org.openconcerto.erp.generationDoc.SheetUtils.java

License:Open Source License

public static void convert2PDF(final OpenDocument doc, final File pdfFileToCreate) throws Exception {
    assert (!SwingUtilities.isEventDispatchThread());
    // Open the PDF document
    Document document = new Document(PageSize.A4, 50, 50, 50, 50);

    try {//from  w  w  w. j  av a 2s  .co m

        FileOutputStream fileOutputStream = new FileOutputStream(pdfFileToCreate);

        // Create the writer
        PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream);
        writer.setPdfVersion(PdfWriter.VERSION_1_6);
        writer.setFullCompression();

        document.open();

        PdfContentByte cb = writer.getDirectContent();

        // Configure the renderer
        ODTRenderer renderer = new ODTRenderer(doc);
        renderer.setIgnoreMargins(false);
        renderer.setPaintMaxResolution(true);

        // Scale the renderer to fit width
        renderer.setResizeFactor(renderer.getPrintWidth() / document.getPageSize().getWidth());

        // Print pages
        for (int i = 0; i < renderer.getPrintedPagesNumber(); i++) {

            Graphics2D g2 = cb.createGraphics(PageSize.A4.getWidth(), PageSize.A4.getHeight());

            // If you want to prevent copy/paste, you can use
            // g2 = tp.createGraphicsShapes(w, h, true, 0.9f);

            // Render
            renderer.setCurrentPage(i);
            renderer.paintComponent(g2);
            g2.dispose();

            // Add our spreadsheet in the middle of the page
            if (i < renderer.getPrintedPagesNumber() - 1)
                document.newPage();

        }
        // Close the PDF document
        document.close();
        // writer.close();
        fileOutputStream.close();

    } catch (Exception originalExn) {
        ExceptionHandler.handle("Impossible de crer le PDF " + pdfFileToCreate.getAbsolutePath(),
                originalExn);
    }

}

From source file:org.openconcerto.erp.graph.GraphSamplePDF.java

License:Open Source License

/**
 * @param args//from   w w w  . ja v a2s.c om
 * @throws DocumentException
 * @throws IOException
 */
public static void main(String[] args) throws DocumentException, IOException {

    // ChartPanel panel = new ChartPanel(c);
    FileOutputStream baos = new FileOutputStream(new File("OpenConcerto_Chart.pdf"));
    Document document = new Document(PageSize.A4);
    // PdfWriter writer = new PdfWriter(document, baos);

    PdfWriter writer = PdfWriter.getInstance(document, baos);

    document.open();
    PdfContentByte cb = writer.getDirectContent();
    System.out.println(document.getPageSize());
    createPage1(document, cb);

    document.newPage();
    createPage2(document, cb);
    document.close();
    baos.close();
}

From source file:org.openconcerto.erp.graph.GraphSamplePDF.java

License:Open Source License

private static void createPage1(Document document, PdfContentByte cb) {
    Graphics2D graphics2D = cb.createGraphics(document.getPageSize().getWidth(),
            document.getPageSize().getHeight());
    int w = 180;//from w  w  w .j a v a  2 s .  c om
    int h = 200;
    int y = 140;
    Font font = new Font("Arial", Font.PLAIN, 12);
    graphics2D.setFont(font);
    int xLogo = 30;
    // AngularGauge c = new AngularGauge();
    // c.setMinValue(0);
    // c.setMaxValue(100);
    // c.setValue(30);
    // c.setChartRectangle(new Rectangle(0, 100, 150, 150));
    // c.render(graphics2D);

    //
    int cX = xLogo;
    int cY = y + 30;
    // Line 1
    table1(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "a) Classique");
    cX += w;

    table2(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "b) Design");
    cX += w;
    table3(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "c) Maximum par colonne");
    // Line 2
    cY += h;
    cX = xLogo;
    line1(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "a) Courbe simple");
    cX += w;

    line2(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "b) Lignes multiples");
    cX += w;
    line3(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "c) Quantitatif");

    // Line 3
    cY += h;
    cX = xLogo;
    piechart1(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "a) Simple");
    cX += w;

    piechart2(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "b) Anneau");
    cX += w;
    piechart3(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "c) Complet avec lgende");
    // Logo

    graphics2D.setFont(new Font("Arial", Font.PLAIN, 24));
    graphics2D.setColor(new Color(66, 119, 167));

    graphics2D.drawString("Open", xLogo, 48);
    graphics2D.setColor(new Color(178, 81, 81));
    graphics2D.drawString("Concerto", xLogo + 59, 48);
    graphics2D.setColor(Color.BLACK);

    // Main title and copyright
    drawCopyrightAndFooter(graphics2D, xLogo, 1);

    // Titles
    graphics2D.setFont(graphics2D.getFont().deriveFont(12f));
    graphics2D.setFont(graphics2D.getFont().deriveFont(Font.BOLD));
    graphics2D.drawString("Tableaux", xLogo, y);
    y += h;
    graphics2D.drawString("Graphiques", xLogo, y);
    y += h;
    graphics2D.drawString("Diagrammes circulaires", xLogo, y);
    graphics2D.dispose();
}

From source file:org.openconcerto.erp.graph.GraphSamplePDF.java

License:Open Source License

private static void createPage2(Document document, PdfContentByte cb) {
    Graphics2D graphics2D = cb.createGraphics(document.getPageSize().getWidth(),
            document.getPageSize().getHeight());
    int w = 180;// ww  w . j  a v a  2 s. c o  m
    int h = 200;
    int y = 140;
    Font font = new Font("Arial", Font.PLAIN, 12);
    graphics2D.setFont(font);
    int xLogo = 30;

    //
    int cX = xLogo;
    int cY = y + 30;
    // Line 1
    barchart1(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "a) Simple");
    cX += w;

    barchart2(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "b) Group");
    cX += w;
    barchart3(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "c) Empill");
    // Line 2
    cY += h;
    cX = xLogo;
    gauge1(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "a) Valeur");
    cX += w;

    gauge2(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "b) Pourcentage");
    cX += w;
    gauge3(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "c) Qualitatif");

    // Line 3
    cY += h;
    cX = xLogo;
    cloud1(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "a) Points");
    cX += w;

    cloud2(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "b) Bulles");
    cX += w;
    cloud3(graphics2D, cX, cY);
    legend(graphics2D, cX, cY, "c) Zones");
    // Logo

    graphics2D.setFont(new Font("Arial", Font.PLAIN, 24));
    graphics2D.setColor(new Color(66, 119, 167));

    graphics2D.drawString("Open", xLogo, 48);
    graphics2D.setColor(new Color(178, 81, 81));
    graphics2D.drawString("Concerto", xLogo + 59, 48);
    graphics2D.setColor(Color.BLACK);

    drawCopyrightAndFooter(graphics2D, xLogo, 2);
    // Titles
    graphics2D.setFont(graphics2D.getFont().deriveFont(12f));
    graphics2D.setFont(graphics2D.getFont().deriveFont(Font.BOLD));
    graphics2D.drawString("Diagrammes en btons", xLogo, y);
    y += h;
    graphics2D.drawString("Jauge", xLogo, y);
    y += h;
    graphics2D.drawString("Nuages", xLogo, y);
    graphics2D.dispose();
}

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 w  w.j a v a2s  .c o m*/

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