Esempio n. 1
0
  private static JRDesignStaticText createStaticText(String txt, int x, int y) {
    JRDesignStaticText staticText = new JRDesignStaticText();
    staticText.setX(x);
    staticText.setY(y);
    staticText.setWidth(300);
    staticText.setHeight(15);
    staticText.setForecolor(Color.white);
    staticText.setBackcolor(new Color(0, 153, 255));
    staticText.setMode(ModeEnum.OPAQUE);
    staticText.setHorizontalAlignment(HorizontalAlignEnum.CENTER);
    staticText.setVerticalAlignment(VerticalAlignEnum.MIDDLE);
    staticText.setText(txt);

    JRLineBox lineBox = staticText.getLineBox();
    JRBoxPen pen = lineBox.getBottomPen();
    pen.setLineColor(new Color(204, 204, 204));
    pen.setLineWidth(1);

    return staticText;
  }
  private static JasperDesign getJasperDesign(List<PropertyDescriptor> props) throws JRException {
    // JasperDesign
    JasperDesign jasperDesign = new JasperDesign();
    jasperDesign.setName("NoXmlDesignReport");
    jasperDesign.setPageWidth(595);
    jasperDesign.setPageHeight(842);
    jasperDesign.setColumnWidth(515);
    jasperDesign.setColumnSpacing(0);
    jasperDesign.setColumnCount(1);
    jasperDesign.setLeftMargin(40);
    jasperDesign.setRightMargin(40);
    jasperDesign.setTopMargin(50);
    jasperDesign.setBottomMargin(50);

    // Fonts
    JRDesignStyle normalStyle = new JRDesignStyle();
    normalStyle.setName("Sans_Normal");
    normalStyle.setDefault(true);
    normalStyle.setFontName("DejaVu Sans");
    normalStyle.setFontSize(12);
    normalStyle.setPdfFontName("Helvetica");
    normalStyle.setPdfEncoding("Cp1252");
    normalStyle.setPdfEmbedded(false);
    jasperDesign.addStyle(normalStyle);

    JRDesignStyle boldStyle = new JRDesignStyle();
    boldStyle.setName("Sans_Bold");
    boldStyle.setFontName("DejaVu Sans");
    boldStyle.setFontSize(12);
    boldStyle.setBold(true);
    boldStyle.setPdfFontName("Helvetica-Bold");
    boldStyle.setPdfEncoding("Cp1252");
    boldStyle.setPdfEmbedded(false);
    jasperDesign.addStyle(boldStyle);

    JRDesignStyle italicStyle = new JRDesignStyle();
    italicStyle.setName("Sans_Italic");
    italicStyle.setFontName("DejaVu Sans");
    italicStyle.setFontSize(12);
    italicStyle.setItalic(true);
    italicStyle.setPdfFontName("Helvetica-Oblique");
    italicStyle.setPdfEncoding("Cp1252");
    italicStyle.setPdfEmbedded(false);
    jasperDesign.addStyle(italicStyle);

    // Parameters
    JRDesignParameter parameter = new JRDesignParameter();
    parameter.setName("ReportTitle");
    parameter.setValueClass(java.lang.String.class);
    jasperDesign.addParameter(parameter);

    // Fields
    int i = 1;
    for (PropertyDescriptor p : props) {
      JRDesignField field = new JRDesignField();
      field.setName(p.getName());
      field.setValueClass(resolveType(p));
      jasperDesign.addField(field);
      i++;
    }

    // Title
    JRDesignBand band = new JRDesignBand();
    band.setHeight(50);

    JRDesignTextField textField = new JRDesignTextField();
    textField.setBlankWhenNull(true);
    textField.setX(0);
    textField.setY(20);
    textField.setWidth(510);
    textField.setHeight(30);
    textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
    textField.setStyle(normalStyle);
    textField.setFontSize(22);
    JRDesignExpression expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("$P{ReportTitle}");
    textField.setExpression(expression);
    band.addElement(textField);
    jasperDesign.setTitle(band);

    // Page header
    band = new JRDesignBand();
    band.setHeight(20);
    int x = 0;
    for (PropertyDescriptor p : props) {
      JRDesignStaticText staticText = new JRDesignStaticText();
      staticText.setX(x);
      staticText.setY(0);

      if (p.getName().equals("message")) {
        staticText.setWidth(400);
        x += 400;
      } else {
        staticText.setWidth(100);
        x += 100;
      }
      staticText.setHeight(20);
      staticText.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
      staticText.setVerticalAlignment(JRAlignment.VERTICAL_ALIGN_MIDDLE);
      staticText.setStyle(boldStyle);
      staticText.setStretchType(JRDesignStaticText.STRETCH_TYPE_RELATIVE_TO_TALLEST_OBJECT);

      staticText.setText(p.getDisplayName());
      band.addElement(staticText);
    }

    jasperDesign.setPageHeader(band);

    // Column header
    band = new JRDesignBand();
    jasperDesign.setColumnHeader(band);

    band = new JRDesignBand();
    band.setHeight(25);
    x = 0;
    for (PropertyDescriptor p : props) {
      textField = new JRDesignTextField();
      textField.setX(x);
      textField.setY(0);
      if (p.getName().equals("message")) {
        textField.setWidth(400);
        x += 400;
      } else {
        textField.setWidth(100);
        x += 100;
      }
      textField.setHeight(20);
      textField.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_CENTER);
      textField.setVerticalAlignment(JRAlignment.VERTICAL_ALIGN_MIDDLE);
      textField.setStyle(normalStyle);
      expression = new JRDesignExpression();
      Class type = resolveType(p);
      expression.setValueClass(type);
      expression.setText("$F{" + p.getName() + "}");
      textField.setExpression(expression);
      textField.setStretchWithOverflow(true);
      band.addElement(textField);
    }

    jasperDesign.setDetail(band);

    // Column footer
    band = new JRDesignBand();
    jasperDesign.setColumnFooter(band);

    // Page footer
    band = new JRDesignBand();
    jasperDesign.setPageFooter(band);

    // Summary
    band = new JRDesignBand();
    jasperDesign.setSummary(band);

    return jasperDesign;
  }