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; }
protected JRDesignTextField createJRTextField( String key, String expression, int x, int y, int w, int h, String fontName, int fontSize, int fontStyle) { JRDesignTextField txf = new JRDesignTextField(); // if (!print && drawBounds) { // txf.setBorder((byte) 1); // } txf.setStretchWithOverflow(false); txf.setBlankWhenNull(true); txf.setEvaluationTime(JRExpression.EVALUATION_TIME_NOW); txf.setHyperlinkType(JRDesignTextField.HYPERLINK_TYPE_NONE); txf.setForecolor(Color.BLACK); txf.setBackcolor(Color.WHITE); txf.setKey(key); txf.setStretchType(JRElement.STRETCH_TYPE_NO_STRETCH); txf.setPositionType(JRElement.POSITION_TYPE_FIX_RELATIVE_TO_TOP); txf.setPrintRepeatedValues(true); txf.setRemoveLineWhenBlank(false); txf.setPrintInFirstWholeBand(false); txf.setPrintWhenDetailOverflows(false); txf.setHorizontalAlignment(JRTextElement.HORIZONTAL_ALIGN_CENTER); // textAlignment txf.setVerticalAlignment((JRTextElement.VERTICAL_ALIGN_TOP)); txf.setRotation(JRTextElement.ROTATION_NONE); txf.setLineSpacing(JRTextElement.LINE_SPACING_SINGLE); txf.setMode(JRTextElement.MODE_OPAQUE); JRDesignExpression txfValue = new JRDesignExpression(); txfValue.setValueClass(String.class); txfValue.setText(expression); txf.setExpression(txfValue); txf.setX(x); txf.setY(y); txf.setWidth(w); txf.setHeight(h); // JRReportFont font = new JRDesignReportFont(); txf.setFontName(fontName); txf.setFontSize(fontSize); txf.setBold((fontStyle & Font.BOLD) != 0); txf.setItalic((fontStyle & Font.ITALIC) != 0); txf.setUnderline(false); txf.setPdfEmbedded(false); txf.setPdfEncoding("Cp1252"); txf.setStrikeThrough(false); txf.setPdfFontName("Helvetica"); JRReportFont rfont = new JRDesignReportFont(); rfont.setFontName(fontName); rfont.setFontSize(fontSize); rfont.setBold((fontStyle & Font.BOLD) != 0); rfont.setItalic((fontStyle & Font.ITALIC) != 0); rfont.setUnderline(false); rfont.setPdfEmbedded(false); rfont.setPdfEncoding("Cp1252"); rfont.setStrikeThrough(false); txf.setReportFont(rfont); rfont.setPdfFontName("Helvetica"); JRDesignStyle style = new JRDesignStyle(); style.setFontName(fontName); txf.setStyle(style); return txf; }