Example usage for com.lowagie.text.pdf PdfPTable writeSelectedRows

List of usage examples for com.lowagie.text.pdf PdfPTable writeSelectedRows

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfPTable writeSelectedRows.

Prototype

public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas) 

Source Link

Document

Writes the selected rows to the document.

Usage

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 {//from www.  j a  v a  2 s  .  com
        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   w w w . j a  v a2s  .  c  o 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);
        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  .  ja va2  s .c  o  m
        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();
    }
}

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

License:Open Source License

public void onEndPage(PdfWriter writer, Document document) {
    String pageTemplate = "/templates/page.pdf";
    try {/*from  www  . j ava2 s  .com*/
        PdfContentByte cb = writer.getDirectContentUnder();
        PdfReader reader = new PdfReader(this.getClass().getResourceAsStream(pageTemplate));
        PdfImportedPage page = writer.getImportedPage(reader, 1);
        cb.addTemplate(page, 0, 0);

        Font font = FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL, Color.GRAY);
        Rectangle pageSize = document.getPageSize();
        PdfPTable head = new PdfPTable(1);
        head.getDefaultCell().setVerticalAlignment(PdfCell.ALIGN_MIDDLE);
        head.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER);
        head.getDefaultCell().setBorder(0);
        Phrase projectName = new Phrase(project.getName(), font);
        head.addCell(projectName);
        head.setTotalWidth(pageSize.getWidth() - document.leftMargin() - document.rightMargin());
        head.writeSelectedRows(0, -1, document.leftMargin(), pageSize.getHeight() - 15,
                writer.getDirectContent());
        head.setSpacingAfter(10);

        PdfPTable foot = new PdfPTable(1);
        foot.getDefaultCell().setVerticalAlignment(PdfCell.ALIGN_MIDDLE);
        foot.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
        foot.getDefaultCell().setBorder(0);
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm");
        Phrase projectAnalysisDate = new Phrase(df.format(project.getMeasures().getDate()), font);
        foot.addCell(projectAnalysisDate);
        foot.setTotalWidth(pageSize.getWidth() - document.leftMargin() - document.rightMargin());
        foot.writeSelectedRows(0, -1, document.leftMargin(), 20, writer.getDirectContent());
        foot.setSpacingBefore(10);
    } catch (IOException e) {
        Logger.error("Cannot find the required template: " + pageTemplate);
        e.printStackTrace();
    }
}

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

License:Open Source License

protected void printFrontPage(Document frontPageDocument, PdfWriter frontPageWriter)
        throws org.dom4j.DocumentException, ReportException {
    String frontPageTemplate = "/templates/frontpage.pdf";
    try {/*w  w w .  j  a  v  a2s  .co m*/
        PdfContentByte cb = frontPageWriter.getDirectContent();
        PdfReader reader = new PdfReader(this.getClass().getResourceAsStream(frontPageTemplate));
        PdfImportedPage page = frontPageWriter.getImportedPage(reader, 1);
        frontPageDocument.newPage();
        cb.addTemplate(page, 0, 0);

        Project project = getProject();

        Rectangle pageSize = frontPageDocument.getPageSize();
        PdfPTable projectInfo = new PdfPTable(1);
        projectInfo.getDefaultCell().setVerticalAlignment(PdfCell.ALIGN_MIDDLE);
        projectInfo.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT);
        projectInfo.getDefaultCell().setBorder(Rectangle.BOTTOM);
        projectInfo.getDefaultCell().setPaddingBottom(10);
        projectInfo.getDefaultCell().setBorderColor(Color.GRAY);
        Font font = FontFactory.getFont(FontFactory.COURIER, 18, Font.NORMAL, Color.LIGHT_GRAY);

        Phrase projectName = new Phrase("Project: " + project.getName(), font);
        projectInfo.addCell(projectName);

        Phrase projectVersion = new Phrase("Version: " + project.getMeasures().getVersion(), font);
        projectInfo.addCell(projectVersion);

        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm");
        Phrase projectAnalysisDate = new Phrase("Analysis Date: " + df.format(project.getMeasures().getDate()),
                font);
        projectInfo.addCell(projectAnalysisDate);

        projectInfo.setTotalWidth(
                pageSize.getWidth() - frontPageDocument.leftMargin() * 2 - frontPageDocument.rightMargin() * 2);
        projectInfo.writeSelectedRows(0, -1, frontPageDocument.leftMargin(), pageSize.getHeight() - 575,
                frontPageWriter.getDirectContent());
        projectInfo.setSpacingAfter(10);
    } catch (IOException e) {
        Logger.error("Cannot find the required template: " + frontPageTemplate);
        e.printStackTrace();
    }
}

From source file:org.webguitoolkit.ui.util.export.PDFEvent.java

License:Apache License

public void onEndPage(PdfWriter writer, Document document) {
    TableExportOptions exportOptions = wgtTable.getExportOptions();
    try {//from w  ww. jav a  2 s  . co  m
        Rectangle page = document.getPageSize();
        if (exportOptions.isShowDefaultHeader() || StringUtils.isNotEmpty(exportOptions.getHeaderImage())) {
            PdfPTable head = new PdfPTable(3);
            head.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            Paragraph title = new Paragraph(wgtTable.getTitle());
            title.setAlignment(Element.ALIGN_LEFT);
            head.addCell(title);

            Paragraph empty = new Paragraph("");
            head.addCell(empty);
            if (StringUtils.isNotEmpty(exportOptions.getHeaderImage())) {
                try {
                    URL absoluteFileUrl = wgtTable.getPage().getClass()
                            .getResource("/" + exportOptions.getHeaderImage());
                    if (absoluteFileUrl != null) {
                        String path = absoluteFileUrl.getPath();
                        Image jpg = Image.getInstance(path);
                        jpg.scaleAbsoluteHeight(40);
                        jpg.scaleAbsoluteWidth(200);
                        head.addCell(jpg);
                    }
                } catch (Exception e) {
                    logger.error(e.getMessage());
                    Paragraph noImage = new Paragraph("Image not found!");
                    head.addCell(noImage);
                }
            } else {
                head.addCell(empty);
            }
            head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
            head.writeSelectedRows(0, -1, document.leftMargin(),
                    page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent());
        }

        if (exportOptions.isShowDefaultFooter() || StringUtils.isNotEmpty(exportOptions.getFooterText())
                || exportOptions.isShowPageNumber()) {
            PdfPTable foot = new PdfPTable(3);
            String footerText = exportOptions.getFooterText() != null ? exportOptions.getFooterText() : "";

            if (!exportOptions.isShowDefaultFooter()) {
                foot.addCell(new Paragraph(footerText));
                foot.addCell(new Paragraph(""));
            } else {
                foot.getDefaultCell().setBorder(Rectangle.NO_BORDER);
                String leftText = "";
                if (StringUtils.isNotEmpty(exportOptions.getFooterText())) {
                    leftText = exportOptions.getFooterText();
                }
                Paragraph left = new Paragraph(leftText);
                left.setAlignment(Element.ALIGN_LEFT);
                foot.addCell(left);

                DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM,
                        TextService.getLocale());
                Date today = new Date();
                String date = df.format(today);
                Paragraph center = new Paragraph(date);
                center.setAlignment(Element.ALIGN_CENTER);
                foot.addCell(center);
            }

            if (exportOptions.isShowPageNumber()) {
                Paragraph right = new Paragraph(
                        TextService.getString("pdf.page@Page:") + " " + writer.getPageNumber());
                right.setAlignment(Element.ALIGN_LEFT);
                foot.addCell(right);

                foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
                foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                        writer.getDirectContent());
            } else {
                foot.addCell(new Paragraph(""));
            }
        }
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:questions.tables.AddTableAsHeaderFooter.java

public void onEndPage(PdfWriter writer, Document document) {
    try {/*from www. j ava2 s .  c o  m*/
        // Header
        headerTable.writeSelectedRows(0, -1, document.leftMargin(),
                document.top() + headerTable.getTotalHeight(), writer.getDirectContent());
        // Footer
        PdfPTable footerTable = new PdfPTable(2);
        PdfPCell cell1 = new PdfPCell(new Phrase("page " + writer.getPageNumber()));
        footerTable.addCell(cell1);
        PdfPCell cell2 = new PdfPCell(Image.getInstance(tpl));
        footerTable.addCell(cell2);
        footerTable.setTotalWidth(document.right() - document.left());
        footerTable.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                writer.getDirectContent());
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:questions.tables.TablesWriteSelected.java

public static void main(String[] args) {
    Document document = new Document(PageSize.A4.rotate());
    try {//from w w  w  .j a va 2s  .com
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));

        document.open();

        // the content of the columns
        PdfPTable items = new PdfPTable(2);
        items.setTotalWidth(TOTAL_WIDTH);
        for (int i = 0; i < 100; ++i) {
            items.addCell("item " + i);
            items.addCell("some item");
        }
        int rows = items.size();

        // adding the stuff to the document
        int column = 0;
        int start;
        int end = 0;
        int row = 0;
        float available = 0;
        float[][] x = { { document.left(), document.left() + TOTAL_WIDTH },
                { document.right() - TOTAL_WIDTH, document.right() } };
        while (row < rows) {
            start = row;
            if (column == 0) {
                PdfPTable table = new PdfPTable(1);
                table.setWidthPercentage(100);
                table.addCell("EmployeeSheets");
                table.addCell("Page " + writer.getPageNumber());
                document.add(table);
                available = document.top() - table.getTotalHeight() - 10 - document.bottom();
            }
            float needed = items.getRowHeight(start);
            while (needed < available && row < rows) {
                needed += items.getRowHeight(++row);
                end = row;
            }
            items.writeSelectedRows(start, end, x[column][0], document.bottom() + available,
                    writer.getDirectContent());
            if (++column >= x.length) {
                column = 0;
                document.newPage();
            }
        }
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}

From source file:s2s.report.MyPageEvents.java

License:GNU General Public License

@Override
public void onStartPage(PdfWriter writer, Document m_document) {
    if (!bShowHeader) {
        return;/* www .  ja v a  2 s .co m*/
    }
    int pageN = writer.getPageNumber();
    if (!m_writeIndex) {
        if (!IndexAlreadyWrited) {
            pageN += m_iCount;
        }
    } else {
        pageN = ++m_iCurrentIndexPage;
    }

    PdfPTable tbl = new PdfPTable(3);

    String text = "                 " + ApplicationConfigurator.LanguageManager.getString("Pag") + " " + pageN
            + " " + ApplicationConfigurator.LanguageManager.getString("Di") + " ";

    tbl.setTotalWidth(m_rect.getWidth() - 60);
    tbl.addCell(new Phrase(strTopLeft, REPORT_SETTINGS.ftText10));

    PdfPCell pc = new PdfPCell(new Phrase(strTopCenter, REPORT_SETTINGS.ftText11));
    pc.setHorizontalAlignment(Element.ALIGN_CENTER);
    tbl.addCell(pc);

    Phrase ph = new Phrase(text, REPORT_SETTINGS.ftText10);
    tbl.addCell(ph);
    tbl.writeSelectedRows(0, -1, 30, m_rect.getHeight() - 30, cb);

    cb.addTemplate(template, bRotate ? 647 : 500, m_rect.getHeight() - 30 - 12);
}

From source file:s2s.report.MyPageEvents.java

License:GNU General Public License

@Override
public void onEndPage(PdfWriter writer, Document m_document) {
    if ((strBottomLeft.length() == 0) && (strBottomRight.length() == 0) && (bShowDate == false)
            && strFirstBottomLeft.length() == 0 && strFirstBottomRight.length() == 0) {
        return;/* w  w w . j  av a 2s.co m*/
    }
    int iColumns = 0;
    PdfPTable tbl1 = null;
    PdfPTable tbl2 = null;

    // Stampa gli eventuali valori delle celle (sinistra e destra) di un 
    // ulteriore pi di pagina, precedente a quello standard, senza bordi di 
    // tabella.
    if (strFirstBottomLeft.length() != 0 || strFirstBottomRight.length() != 0) {
        if (strFirstBottomLeft.length() != 0) {
            iColumns++;
        }
        if (strFirstBottomRight.length() != 0) {
            iColumns++;
        }
        tbl1 = new PdfPTable(iColumns);
        tbl1.setTotalWidth(m_rect.getWidth() - 60);
        if (strFirstBottomLeft.length() != 0) {
            PdfPCell cell = new PdfPCell(new Phrase(strFirstBottomLeft, REPORT_SETTINGS.ftText10));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            tbl1.addCell(cell);
        }
        if (strFirstBottomRight.length() != 0) {
            PdfPCell cell = new PdfPCell(new Phrase(strFirstBottomRight, REPORT_SETTINGS.ftText10));
            cell.setBorder(Rectangle.NO_BORDER);
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            tbl1.addCell(cell);
        }
    }

    // Stampa gli eventuali valori delle celle (sinistra e destra) 
    // del pi di pagina standard.
    if ((strBottomLeft.length() != 0) || (strBottomRight.length() != 0) || (bShowDate == true)) {
        iColumns = 0;
        if (strBottomLeft.length() != 0) {
            iColumns++;
        }
        if (strBottomRight.length() != 0) {
            iColumns++;
        }
        if (bShowDate) {
            iColumns++;
        }

        tbl2 = new PdfPTable(iColumns);
        tbl2.setTotalWidth(m_rect.getWidth() - 60);
        if (strBottomLeft.length() != 0) {
            tbl2.addCell(new Phrase(strBottomLeft, REPORT_SETTINGS.ftText10));
        }
        if (strBottomRight.length() != 0) {
            tbl2.addCell(new Phrase(strBottomRight, REPORT_SETTINGS.ftText10));
        }
        if (bShowDate) {
            PdfPCell pc = new PdfPCell(new Phrase(
                    Formatter.format(new java.sql.Date(System.currentTimeMillis())), REPORT_SETTINGS.ftText11));
            pc.setHorizontalAlignment(Element.ALIGN_CENTER);
            tbl2.addCell(pc);
        }
    }

    // Scrive il pi di pagina precedente a quello standard
    if (tbl1 != null) {
        tbl1.writeSelectedRows(0, -1, 30, (tbl2 != null ? 150 : 100), cb);
    }
    // Scrive il pi di pagina standard
    if (tbl2 != null) {
        tbl2.writeSelectedRows(0, -1, 30, 50, cb);
    }
}