Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
How to count String length in Pixels or inches or cm etc...
arunsrkr
<p>Hi All,</p>
<p> </p>
<p>I have below requirement.</p>
<p> </p>
<p>I would like to get String length in pixels.</p>
<p> </p>
<p>Please let me know how to calculate String length in pixels.</p>
<p> </p>
<p>Regards,</p>
<p>Arun</p>
Find more posts tagged with
Comments
Matthew L.
<p>Here is a method I use for BIRT engine side (example attached).</p>
<p>*Please note that this is not 100% accurate as other factors can effect the output.</p>
<pre class="_prettyXprint _lang-js">
importPackage(Packages.java.awt);
importPackage(Packages.java.awt.image);
var str = ""+row["CUSTOMERNAME"];
//http://stackoverflow.com/questions/13345712/string-length-in-pixels-in-java
var font = new Font("Sans Serif", Font.PLAIN, 10);
var img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
var fm = img.getGraphics().getFontMetrics(font);
var width = fm.stringWidth(str);
this.text = width +"px";
</pre>
<p>If you are needing to measure text via the rendering side (HTML, PDF, etc), you will have to use other methods for calculating text length.</p>