Permalink
Browse files

Watermark with background image

  • Loading branch information...
1 parent 7107c93 commit d3b83120e7a0638eefc103acd7f29cb96021657e @juanalvarezg juanalvarezg committed Dec 4, 2015
View
@@ -0,0 +1,13 @@
+# Created by .ignore support plugin (hsz.mobi)
+### Maven template
+target/
+pom.xml.tag
+pom.xml.releaseBackup
+pom.xml.versionsBackup
+pom.xml.next
+release.properties
+dependency-reduced-pom.xml
+buildNumber.properties
+.mvn/timing.properties
+
+
@@ -1,18 +1,15 @@
package ar.com.fdvs.dj.core;
-import java.util.Map;
-
-import com.opensymphony.webwork.views.jasperreports.JasperReportConstants;
import net.sf.jasperreports.engine.JRDefaultScriptlet;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JRScriptletException;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.fill.JRFillGroup;
-
-import net.sf.jasperreports.engine.fill.JRFillParameter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import java.util.Map;
+
/**
* This class handles parameter passing to custom expressions in runtime (during report fill)
* @author mamana
@@ -29,20 +29,27 @@
package ar.com.fdvs.dj.core;
+import java.awt.*;
+import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.*;
+import java.util.List;
-import ar.com.fdvs.dj.util.Utils;
-import ar.com.fdvs.dj.util.WaterMarkRenderer;
+import ar.com.fdvs.dj.domain.constants.*;
+import ar.com.fdvs.dj.domain.constants.Font;
+import ar.com.fdvs.dj.util.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.*;
+import net.sf.jasperreports.engine.type.EvaluationTimeEnum;
+import net.sf.jasperreports.engine.type.OnErrorTypeEnum;
import net.sf.jasperreports.engine.type.PositionTypeEnum;
+import net.sf.jasperreports.engine.type.ScaleImageEnum;
import net.sf.jasperreports.engine.util.JRProperties;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.xml.JRXmlWriter;
@@ -65,8 +72,8 @@
import ar.com.fdvs.dj.domain.entities.Subreport;
import ar.com.fdvs.dj.domain.entities.columns.AbstractColumn;
import ar.com.fdvs.dj.domain.entities.columns.PercentageColumn;
-import ar.com.fdvs.dj.util.DJCompilerFactory;
-import ar.com.fdvs.dj.util.LayoutUtils;
+
+import javax.imageio.ImageIO;
/**
* Helper class for running a report and some other DJ related stuff
@@ -541,27 +548,50 @@ public final static JasperReport generateJasperReport(DynamicReport dr, LayoutMa
private static void registerWatermark(DynamicJasperDesign jd, Map generatedParams) {
JRDesignBand backgroundBand = (JRDesignBand) jd.getBackground();
+
if (backgroundBand == null){
backgroundBand = new JRDesignBand();
jd.setBackground(backgroundBand);
}
- backgroundBand.setHeight(jd.getPageHeight());
- generatedParams.put("watermark", new WaterMarkRenderer(true));
- JRDesignParameter jrparam = new JRDesignParameter();
- jrparam.setName("watermark");
- jrparam.setValueClass(Renderable.class);
- jd.getParametersMap().put("watermark",jrparam);
+ int printableHeight = jd.getPageHeight() - jd.getTopMargin() - jd.getBottomMargin();
+ int printableWidth = jd.getPageWidth() - jd.getLeftMargin() - jd.getRightMargin();
+ backgroundBand.setHeight(printableHeight);
- JRDesignExpression imageExp = new JRDesignExpression();
- imageExp.setText("$P{watermark}");
- imageExp.setValueClass(Renderable.class);
+ generatedParams.put("watermarkText", "Esto es \nTOP SECRET");
+ JRDesignParameter jrparam = new JRDesignParameter();
+ jrparam.setName("watermarkText");
+ jrparam.setValueClass(String.class);
+ jd.getParametersMap().put("watermarkText",jrparam);
JRDesignImage image = new JRDesignImage(new JRDesignStyle().getDefaultStyleProvider());
+ JRDesignExpression imageExp = null;
+
+ Font arialBig = Font.ARIAL_BIG;
+ BufferedImage watermark = WaterMarkRenderer.rotateText("Este es mi texto\nTOP SECRET!!!",
+ new java.awt.Font(arialBig.getFontName(), 1, arialBig.getFontSize()*20),
+ printableWidth*4,
+ printableHeight*4,
+ 320, Color.PINK);
+ try {
+ File outputfile = File.createTempFile("dynamicJasper","watermark.png");
+ outputfile.deleteOnExit();
+ ImageIO.write(watermark, "png", outputfile);
+ String absolutePath = outputfile.getAbsolutePath();
+ log.debug("Watermark Image: " + absolutePath);
+ String escapeTextForExpression = Utils.escapeTextForExpression(absolutePath);
+ imageExp = ExpressionUtils.createStringExpression("\"" + escapeTextForExpression + "\"");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+
image.setExpression(imageExp);
- image.setPositionType(PositionTypeEnum.FLOAT);
- image.setHeight(jd.getPageHeight()-jd.getTopMargin()-jd.getBottomMargin());
- image.setWidth(jd.getPageWidth()-jd.getLeftMargin() - jd.getRightMargin());
+ image.setHeight(printableHeight);
+
+ image.setWidth(printableWidth);
+ image.setScaleImage(ScaleImageEnum.RETAIN_SHAPE);
+ image.setOnErrorType(OnErrorTypeEnum.ICON );
backgroundBand.addElement(image);
}
@@ -72,7 +72,7 @@
private boolean printBackgroundOnOddRows = false;
private Style oddRowBackgroundStyle = new StyleBuilder(false,"defaultOddRowStyle")
- .setBackgroundColor(new Color(200,200,200))
+ .setBackgroundColor(new Color(200,200,200,128))
.build();
private Integer titleHeight = DEFAULT_HEADER_HEIGHT;
private boolean titleNewPage = false;
@@ -103,13 +103,16 @@ public static void main(String[] args) {
* For example: if the title text is: <b>November "2009" sales</b><br>
* The double quotes (") must be escaped to <b>\"</b>, and in java that would be <b>\\\\\"</b><br>
*
+ * single bars "\" are escaped to "\\"
+ *
* To understand better, the expression can be tested in iReport expression editor.
* @param text
* @return
*/
public static String escapeTextForExpression(String text) {
if (text == null)
return null;
+ text = text.replaceAll("\\\\", "\\\\\\\\");
text = text.replaceAll("\"", "\\\\\"");
return text;
}
@@ -9,6 +9,8 @@
import java.awt.geom.AffineTransform;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
+import java.util.ArrayList;
/**
* Created by dj on 01/12/15.
@@ -75,4 +77,90 @@ public void render(Graphics2D g2, Rectangle2D rectangle) throws JRException
g2.setTransform(originalTransform);
}
}
+
+ public static BufferedImage rotateText(String text, Font font, int width, int height, int textAngle, Color textColor) {
+ if(text==null)
+ throw new IllegalArgumentException("text must be not-null ");
+ if(text.length()==0)
+ throw new IllegalArgumentException("text is empty string");
+ if(text.trim().length()==0)
+ throw new IllegalArgumentException("text must contain at least one character that is not space");
+
+ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+ // make whole image transparent
+ for (int i = image.getWidth() - 1; i > -1; i--) {
+ for (int j = image.getHeight() - 1; j > -1; j--) {
+ if (image.getRGB(i, j) == new Color(255, 255, 255).getRGB()) {
+ image.setRGB(i, j, new Color(0, 0, 0, 0).getRGB());
+ }
+ }
+ }
+
+ Graphics2D g = (Graphics2D) image.getGraphics();
+ g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
+ RenderingHints.VALUE_ANTIALIAS_ON));
+ g.setFont(font);
+ FontMetrics fm = g.getFontMetrics();
+ double maxLineWidth = width/* * 2.0/3.0*/;
+ double completeWidth = fm.stringWidth(text);
+
+ double caLinesCountD = completeWidth / maxLineWidth;
+ int caLinesCount = -1;
+ if(caLinesCountD%1.0>0){
+ caLinesCount = (int)caLinesCountD+1;
+ }else
+ caLinesCount = (int)caLinesCountD;
+
+
+ java.util.List<String> lines = new ArrayList<String>(caLinesCount);
+
+ if(caLinesCount==1)
+ lines.add(text);
+ else{
+ int caSepIdx = text.length()/caLinesCount;
+ for(int i=0;i<text.length();){
+ int nextCaSep = i+caSepIdx;
+ if(text.length()>nextCaSep){
+
+ int sepIdx = text.substring(i,nextCaSep+1).lastIndexOf(' ');
+ if(sepIdx==0){
+ i++;
+ continue;
+
+ }else if(sepIdx==-1){
+ lines.add(text.substring(i,nextCaSep));
+ i+=nextCaSep;
+ }else{
+ lines.add(text.substring(i,i+sepIdx));
+ i+=sepIdx+1;
+ }
+ }else{
+ lines.add(text.substring(i));
+ break;
+ }
+ }
+ }
+
+ g.rotate(Math.PI / 180 * textAngle, width / 2, height / 2);
+ int textHeight = fm.getMaxAscent();
+
+ int lineY = height/2;
+ // shift up for vertical centration
+ int halfLines = lines.size()/2;
+ double shiftLines = lines.size()%2==0?halfLines-1:halfLines-0.5;
+ shiftLines+=0.2;
+ lineY-=textHeight*shiftLines;
+
+
+ for(int lineIdx=0;lineIdx<lines.size();lineIdx++){
+ String line = lines.get(lineIdx);
+ int lineWidth = fm.stringWidth(line);
+ int lineX = (width / 2) - (lineWidth / 2);
+ g.setColor(textColor);
+ g.drawChars(line.toCharArray(), 0, line.length(), lineX, lineY);
+ lineY+=textHeight;
+ }
+
+ return image;
+ }
}
@@ -29,22 +29,17 @@
package ar.com.fdvs.dj.test;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
-import net.sf.jasperreports.engine.export.JExcelApiExporter;
-import net.sf.jasperreports.engine.export.JRHtmlExporter;
-import net.sf.jasperreports.engine.export.JRPdfExporter;
-import net.sf.jasperreports.engine.export.JRXlsExporter;
-import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
-
+import net.sf.jasperreports.engine.export.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+
public class ReportExporter {
/**
* Logger for this class
@@ -61,6 +61,14 @@
<category name="com.opensymphony.xwork2" additivity="false">
<priority value="DEBUG" />
</category>
+ <!--<category name="net.sf.jasperreports.extensions" additivity="false">-->
+ <!--<priority value="ERROR" />-->
+ <!--<appender-ref ref="console" />-->
+ <!--</category>-->
+ <!--<category name="net.sf.jasperreports" additivity="false">-->
+ <!--<priority value="DEBUG" />-->
+ <!--<appender-ref ref="console" />-->
+ <!--</category>-->
<category name="com.opensymphony">
<priority value="error" />

0 comments on commit d3b8312

Please sign in to comment.