How to rotate a text field by 45 degrees?

1

Hi All,

 

I need to rotate a text field by 45 degrees. Using iReport, I could rotate the text to left, right or upside down i.e., i could rotate the text by 90 degrees or by 360 degrees but could not rotate the text by 45 degrees.

 

Is it possible to have a text at an angle of 45 degrees in Jasper Reports? If so, please let me know how to do it.

 

Thanks in advance,

Bhavya

 

 

bhavya_s's picture
Joined: Nov 14 2008 - 2:57am
Last seen: 10 years 8 months ago

7 Answers:

0
Hi,

I have the same requirement as it is mentioned above.
Please help if you have got the solution.

Thanks in advance..
Sunny


imsunny's picture
Joined: Jul 21 2010 - 5:29am
Last seen: 8 months 3 days ago
0

if you read here

http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/type/RotationEnum.html

and here

http://jasperreports.sourceforge.net/sample.reference/rotation/index.html

seems that it's impossible.

 

if you want... give me karma points for the bad news :)

slow's picture
891
Joined: Feb 17 2010 - 6:57am
Last seen: 9 years 5 months ago
0
Not an ideal solution, but you could write a helper class in java that creates a image with the text rotated. You could do it as a scriptlet I guess, I prefer a standalone java utility as I could get more mileage from it.
vickirk's picture
34
Joined: Dec 16 2008 - 3:28am
Last seen: 10 years 7 months ago
0

I have a requirement for rotating text and images by 15 degrees, so that certain elements are slanted.

I've done a search on the forums and seen plenty of discussions on the ability to rotate text by 90, 180 and 270, but I've not seen any example or solution to being able to print text/images at an arbitary angle.  Any ideas would be welcome.

As I'm not an expert in Java, I've not seen any example of how to pass a field into a java image rendering function to create a java.awt.Image object, and I'm not sure how to fit this into iReport.

Thanks

 

davepower's picture
Joined: Feb 5 2010 - 1:32am
Last seen: 5 years 3 weeks ago
4

Hi guys. After reading that thread I wrote a scriptlet for rotation of text. It works quite nice, even if the scriptlet seems to find no Open Type Fonts and some character parts appear a little pixilated (although I used antialiasing). But its good enough that our customer will use it for 10.000 prints per month. The code is long only because of a text wrapping algorithm. Tell me if you need a version without wrapping.

After registering the class in classpath (e.g. via the jar archive I have attached: In iReport Extras/Options/iReport/Classpath/Add JAR) all you have to do in layout is the following:

1. Add attribute:

<jasperReport ...  scriptletClass="RotateTextScriptlet" ... >

2. use it in image element. Example:

            <image scaleImage="RealSize" hAlign="Center" vAlign="Middle">
                <reportElement x="26" y="16" width="162" height="162"/>
                <imageExpression class="java.awt.Image"><![CDATA[$P{REPORT_SCRIPTLET}.rotateText(
    $V{SpecialDescription},new java.awt.Font("Sans Serif",1,100), 365, 365, 350, java.awt.Color.white)]]></imageExpression>
            </image>

Note: The image must lead to the java interface type "java.awt.Image" and not "java.lang.String", which is used when referring to an image file. The scriptlet returns an image implementing that interface.

I hope this will help you out!

PS: For any RGB Colors of the text use "new java.awt.Color(R,G,B)" instead of "java.awt.Color.white" which is a constant. Any value can be 0 to 255, so "java.awt.Color.white" is the same like "new java.awt.Color(255,255,255)"

RotateTextScriptlet.java:
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
 
import net.sf.jasperreports.engine.JRDefaultScriptlet;
 
public class RotateTextScriptlet extends JRDefaultScriptlet {
 
  /**
   * Creates an image with rotated text
   *
   * @param text
   *          the text to be displayed
   * @param font
   * @param width
   *          The width of the returned image, must fit the size of the text
   * @param height
   *          the height of the returned image, must fit the size of the text
   * @param textAngle
   *          the angle of the text
   * @param textColor
   *          the color of the text
   * @return an image with the rotated text
   */
  public Image 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;
 
 
    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;
  }
 
}



Post Edited by artduc at 07/18/2012 10:03



Post Edited by artduc at 07/18/2012 10:04



Post Edited by artduc at 07/18/2012 10:14



Post Edited by artduc at 07/18/2012 10:18
artduc's picture
42
Joined: Jul 13 2012 - 9:08am
Last seen: 7 years 1 week ago

I don't know if this post is still active but if it is, Could you help me with this .jar file? I have problems understanding completely the way this is implemented in iReport. I'm using iReport 5.5.0, I tried to attach it and then try it out on an image element but I get this error:
"com.jaspersoft.ireport.designer.errorhandler.ProblemItem@12ff416 The method rotateText(String, Font, int, int, int, Color) is undefined for the type String"

Thanks.

ck_cl - 4 years 6 months ago

Awesome it really worked! Thanks a lot.

bikashduttarox - 2 years 4 months ago
3

Another way to do this is with an Image and an ImageExpression with SVG.

net.sf.jasperreports.renderers.BatikRenderer.getInstanceFromText("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"765\" height=\"990\"><text x=\"60\" y=\"40\" transform=\"matrix(.6,.8,-.8,.6,0,0)\" style=\"font-size:140px;font-weight:bold;fill:#d8d8d8\">$F{my_text_field}</text></svg>")

Circuitsoft's picture
Joined: Jul 29 2016 - 11:37am
Last seen: 1 year 11 months ago
0

Thanks to Circuitsoft, I add this to my jrxml:

<background>

<band height="842" splitType="Stretch">

<image>

<reportElement x="20" y="0" width="511" height="830" uuid="d5d7a19d-060a-4875-954b-e176ab96898a"/>

<imageExpression><![CDATA[net.sf.jasperreports.renderers.BatikRenderer.getInstanceFromText("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"765\" height=\"990\"><text x=\"60\" y=\"40\" transform=\"matrix(.6,.8,-.8,.6,0,0)\" style=\"font-size:140px;font-weight:bold;fill:#d8d8d8\">DRAFT</text></svg>")]]></imageExpression>

</image>

</band>

</background>

 

 

fdelcamino's picture
Joined: Oct 2 2017 - 11:47pm
Last seen: 1 year 9 months ago
Feedback