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.posterita.core.CrossTabReportGenerator.java

License:Open Source License

protected void writeDocument(Document document) throws OperationException {
    try {// ww w  .  j  a v  a2  s.  c o m
        int noOfRows = dataSource.size();
        String longestText = "";
        int columnCount = 0;

        Object[] obj = null;

        Object[] header = (Object[]) dataSource.get(0);
        columnCount = header.length;

        PdfPTable table = new PdfPTable(columnCount);
        table.setWidthPercentage(100);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.getDefaultCell().setPaddingBottom(5);
        table.getDefaultCell().setPaddingTop(5);

        //adding the headers
        for (int i = 0; i < columnCount; i++) {
            if (i == 0) {
                longestText = header[i].toString();
                table.addCell(new Paragraph(header[i].toString()));
            } else {
                Image img = getTextAsImage(header[i].toString());
                img.setRotationDegrees(90);
                img.setAlignment(Image.ALIGN_BOTTOM);

                PdfPCell cell = new PdfPCell(img);
                cell.setPadding(4);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
                //cell.setBackgroundColor(new Color(0, 0, 255));

                table.addCell(cell);
            }
        }

        //adding the data 
        for (int j = 1; j < noOfRows; j++) {
            obj = (Object[]) dataSource.get(j);

            for (int k = 0; k < columnCount; k++) {
                if (k == 0) {
                    String text = obj[0].toString();

                    if (new Chunk(text, HEADER_FONT).getWidthPoint() > new Chunk(longestText, HEADER_FONT)
                            .getWidthPoint()) {
                        longestText = text;
                    }

                    Chunk txtck = new Chunk(obj[0].toString(), HEADER_FONT);
                    PdfPCell cell = new PdfPCell(new Paragraph(txtck));

                    if (j == noOfRows - 1) {
                        cell.setBackgroundColor(new Color(170, 170, 170));
                    }

                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell.setPaddingBottom(5);
                    cell.setPaddingTop(5);
                    cell.setPaddingLeft(5);

                    table.addCell(cell);
                } else {
                    Chunk txtck = new Chunk(obj[k].toString(), DATA_FONT);

                    if (k == columnCount - 1) {
                        PdfPCell cell = new PdfPCell(new Paragraph(txtck));
                        cell.setBackgroundColor(new Color(170, 170, 170));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        cell.setPaddingBottom(5);
                        cell.setPaddingTop(5);

                        table.addCell(cell);
                    } else {
                        PdfPCell cell = new PdfPCell(new Paragraph(txtck));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        cell.setPaddingBottom(5);
                        cell.setPaddingTop(5);

                        if (j == noOfRows - 1) {
                            cell.setBackgroundColor(new Color(170, 170, 170));
                        }

                        table.addCell(cell);
                    }
                }

            }

        }

        //setting table width
        Chunk dataChk = new Chunk("9999", DATA_FONT);
        Chunk headerChk = new Chunk(longestText, HEADER_FONT);

        float dataChkLength = dataChk.getWidthPoint() + 2 * CELLPADDING;
        float headerChkLength = headerChk.getWidthPoint() + 2 * CELLPADDING;

        float tableWidth = headerChkLength + dataChkLength * columnCount;
        float actualTableWidth = document.getPageSize().getWidth() - 2 * MARGIN;

        float columnWidth = dataChkLength;

        if (tableWidth < actualTableWidth) {
            columnWidth = (actualTableWidth - headerChkLength) / (columnCount - 1);
        }

        float[] widths = new float[columnCount];
        widths[0] = headerChkLength + 2 * CELLPADDING;

        for (int i = 1; i < columnCount; i++) {
            widths[i] = columnWidth;
        }

        table.setWidths(widths);

        //writing the table      
        document.add(table);
    } catch (DocumentException e) {
        throw new OperationException(e);
    }

}

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

License:Open Source License

protected Rectangle getDocumentDimension() {
    Document document = new Document(PAGE_SIZE, MARGIN, MARGIN, MARGIN, MARGIN);

    String longestText = "";
    int columnCount = 0;
    Object[] obj = null;/*from w  w w . j  av  a 2s. c o  m*/

    //Getting column count
    obj = (Object[]) dataSource.get(0);
    columnCount = obj.length;

    //Getting longest text
    Iterator iter = dataSource.iterator();

    while (iter.hasNext()) {
        obj = (Object[]) iter.next();

        String header = obj[0].toString();

        if (header.length() > longestText.length()) {
            longestText = header;
        }
    }

    //setting the table width
    Chunk dataChk = new Chunk("9999", DATA_FONT);
    Chunk headerChk = new Chunk(longestText, HEADER_FONT);

    float dataChkLength = dataChk.getWidthPoint() + 2 * CELLPADDING;
    float headerChkLength = headerChk.getWidthPoint() + 2 * CELLPADDING;

    float tableWidth = headerChkLength + dataChkLength * columnCount;

    float actualTableWidth = document.getPageSize().getWidth() - 2 * MARGIN;
    //float actualTableHeight = document.getPageSize().height() - 2*MARGIN;

    //if the table size is greater than that of the page we should 
    //scale the page

    if (tableWidth > actualTableWidth) {
        float documentWidth = document.getPageSize().getWidth();
        float documentHeight = document.getPageSize().getHeight();

        float newDocumentWidth = tableWidth + 2 * MARGIN;
        float newDocumentHeight = (documentHeight * newDocumentWidth) / documentWidth;

        return new Rectangle(newDocumentWidth, newDocumentHeight);
    }

    return document.getPageSize();
}

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();//from  w w  w.j  ava  2  s . co  m
    // 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

/**
 * Sets the marks to the PDF document.//from   w ww .  j  ava 2s.c  o  m
 * 
 * @param writer
 *            the PDF writer
 * @param document
 *            the PDF document
 */
private void setMarks(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    float height = PDFTools.ptToCm(document.getPageSize().getHeight());

    cb.setLineWidth(0.0f);

    cb.moveTo(0.0f, PDFTools.cmToPt(height / 2.0f));
    cb.lineTo(PDFTools.cmToPt(0.3f), PDFTools.cmToPt(height / 2.0f));

    cb.moveTo(0.0f, PDFTools.cmToPt(height * 0.33f));
    cb.lineTo(PDFTools.cmToPt(0.3f), PDFTools.cmToPt(height * 0.33f));

    cb.moveTo(0.0f, PDFTools.cmToPt(height * 0.66f));
    cb.lineTo(PDFTools.cmToPt(0.3f), PDFTools.cmToPt(height * 0.66f));

    cb.stroke();
}

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 ww .  ja va  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.sigmah.server.report.renderer.itext.ItextChartRenderer.java

License:Open Source License

public void render(DocWriter writer, Document doc, PivotChartElement element) {

    try {//w w  w  . j  a  va  2 s. c  o m
        doc.add(ThemeHelper.elementTitle(element.getTitle()));
        ItextRendererHelper.addFilterDescription(doc, element.getContent().getFilterDescriptions());

        float width = doc.getPageSize().getWidth() - doc.rightMargin() - doc.leftMargin();
        float height = (doc.getPageSize().getHeight() - doc.topMargin() - doc.bottomMargin()) / 3f;

        if (writer instanceof PdfWriter) {

            // We can render the chart directly as vector graphics
            // in the PDF file

            PdfWriter pdfWriter = (PdfWriter) writer;
            PdfContentByte cb = pdfWriter.getDirectContent();
            Graphics2D g2d = cb.createGraphics(width, height);
            chartRenderer.render(element, false, g2d, (int) width, (int) height, 72);
            g2d.dispose();

        } else {

            // For RTF/Html we embed as a GIF

            width = width / 72f * RESOLUTION;
            height = height / 72f * RESOLUTION;

            BufferedImage chartImage = chartRenderer.renderImage(element, false, (int) width, (int) height,
                    RESOLUTION);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(chartImage, "GIF", baos);

            Image image = Image.getInstance(baos.toByteArray());
            image.scalePercent(72f / RESOLUTION * 100f);

            doc.add(image);

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.sigmah.server.report.renderer.itext.ItextPivotTableRenderer.java

License:Open Source License

protected float[] calcColumnWidths(Document doc, PivotTableData data, List<PivotTableData.Axis> leafColumns) {
    // assume fixed column size
    float[] widths = new float[leafColumns.size() + 1];
    widths[0] = doc.getPageSize().getWidth() - doc.leftMargin() - doc.rightMargin()
            - (leafColumns.size() * 47f);
    for (int i = 1; i != widths.length; ++i) {
        widths[i] = 47f;/*  ww w.  j a  va  2 s.  com*/
    }
    return widths;
}

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

License:Open Source License

@Override
protected void printFrontPage(Document frontPageDocument, PdfWriter frontPageWriter)
        throws org.dom4j.DocumentException, ReportException {
    try {/* ww w. j a v  a2s . co  m*/
        URL largeLogo;
        if (super.getConfigProperty("front.page.logo").startsWith("http://")) {
            largeLogo = new URL(super.getConfigProperty("front.page.logo"));
        } else {
            largeLogo = this.getClass().getClassLoader()
                    .getResource(super.getConfigProperty("front.page.logo"));
        }
        Image logoImage = Image.getInstance(largeLogo);
        Rectangle pageSize = frontPageDocument.getPageSize();
        float positionX = pageSize.getWidth() / 2f - logoImage.getWidth() / 2f;
        logoImage.setAbsolutePosition(positionX, pageSize.getHeight() - logoImage.getHeight() - 100);
        frontPageDocument.add(logoImage);

        PdfPTable title = new PdfPTable(1);
        title.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        title.getDefaultCell().setBorder(Rectangle.NO_BORDER);

        String projectRow = super.getTextProperty("general.project") + ": " + super.getProject().getName();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateRow = df.format(super.getProject().getMeasures().getDate());
        String descriptionRow = super.getProject().getDescription();

        title.addCell(new Phrase(projectRow, Style.FRONTPAGE_FONT_1));
        title.addCell(new Phrase(descriptionRow, Style.FRONTPAGE_FONT_2));
        title.addCell(new Phrase(dateRow, Style.FRONTPAGE_FONT_3));
        title.setTotalWidth(
                pageSize.getWidth() - frontPageDocument.leftMargin() - frontPageDocument.rightMargin());
        title.writeSelectedRows(0, -1, frontPageDocument.leftMargin(),
                pageSize.getHeight() - logoImage.getHeight() - 150, frontPageWriter.getDirectContent());

    } catch (IOException e) {
        e.printStackTrace();
    } catch (BadElementException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

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

License:Open Source License

@Override
protected void printFrontPage(Document frontPageDocument, PdfWriter frontPageWriter)
        throws org.dom4j.DocumentException, ReportException {
    try {/*from   ww w  .  jav  a2  s . c om*/
        URL largeLogo;
        if (super.getConfigProperty("front.page.logo").startsWith("http://")) {
            largeLogo = new URL(super.getConfigProperty("front.page.logo"));
        } else {
            largeLogo = this.getClass().getClassLoader()
                    .getResource(super.getConfigProperty("front.page.logo"));
        }
        Image logoImage = Image.getInstance(largeLogo);
        logoImage.scaleAbsolute(360, 200);
        Rectangle pageSize = frontPageDocument.getPageSize();
        logoImage.setAbsolutePosition(Style.FRONTPAGE_LOGO_POSITION_X, Style.FRONTPAGE_LOGO_POSITION_Y);
        frontPageDocument.add(logoImage);

        PdfPTable title = new PdfPTable(1);
        title.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        title.getDefaultCell().setBorder(Rectangle.NO_BORDER);

        String projectRow = super.getProject().getName();
        String versionRow = super.getProject().getMeasures().getVersion();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String dateRow = df.format(super.getProject().getMeasures().getDate());
        String descriptionRow = super.getProject().getDescription();

        title.addCell(new Phrase(projectRow, Style.FRONTPAGE_FONT_1));
        title.addCell(new Phrase(versionRow, Style.FRONTPAGE_FONT_1));
        title.addCell(new Phrase(descriptionRow, Style.FRONTPAGE_FONT_2));
        title.addCell(new Phrase(super.getProject().getMeasure(MetricKeys.PROFILE).getDataValue(),
                Style.FRONTPAGE_FONT_3));
        title.addCell(new Phrase(dateRow, Style.FRONTPAGE_FONT_3));
        title.setTotalWidth(
                pageSize.getWidth() - frontPageDocument.leftMargin() - frontPageDocument.rightMargin());
        title.writeSelectedRows(0, -1, frontPageDocument.leftMargin(), Style.FRONTPAGE_LOGO_POSITION_Y - 150,
                frontPageWriter.getDirectContent());

    } catch (IOException e) {
        e.printStackTrace();
    } catch (BadElementException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

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

License:Open Source License

public void onEndPage(PdfWriter writer, Document document) {
    try {/*w  w w  .j ava 2 s  .  c  om*/
        Image logoImage = Image.getInstance(logo);
        Rectangle page = document.getPageSize();
        PdfPTable head = new PdfPTable(4);
        head.getDefaultCell().setVerticalAlignment(PdfCell.ALIGN_MIDDLE);
        head.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
        head.addCell(logoImage);
        Phrase projectName = new Phrase(project.getName(),
                FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL, Color.GRAY));
        Phrase phrase = new Phrase("Sonar PDF Report",
                FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL, Color.GRAY));
        head.getDefaultCell().setColspan(2);
        head.addCell(phrase);
        head.getDefaultCell().setColspan(1);
        head.addCell(projectName);
        head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
        head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - 20, writer.getDirectContent());
        head.setSpacingAfter(10);
    } catch (BadElementException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}