Text to GIF

Is there an easy (or any :) way to save text (such as system time and date) to a GIF file.  I need a GIF for watermarking a PDF report.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Probably too slow :-(


import java.awt.* ;
import java.awt.image.* ;
import java.io.* ;
import com.sun.image.codec.jpeg.* ;

public class ImageMaker
  public ImageMaker()
  public static void saveBufferedImage( String filename, BufferedImage image )
      File f = new File( filename ) ;
      FileOutputStream out = new FileOutputStream( f ) ;
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( out ) ;
      JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam( image ) ;
      param.setQuality( 1.0f, false ) ;
      encoder.setJPEGEncodeParam( param ) ;
      encoder.encode( image ) ;
      out.close() ;
    catch( IOException e )
      e.printStackTrace() ;
  public static BufferedImage makeImage( Color bgColor,
                                         Color txtColor,
                                         String text,
                                         Font font )
    BufferedImage bi = new BufferedImage( 1, 1, BufferedImage.TYPE_INT_RGB ) ;
    Graphics g = bi.getGraphics() ;
    g.setFont( font ) ;
    FontMetrics fm = g.getFontMetrics() ;
    int width = fm.stringWidth( text ) + 10 ;
    int height = fm.getAscent() + 10 ;
    bi = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB ) ;
    g = bi.getGraphics() ;
    fm = g.getFontMetrics() ;
    g.setFont( font );
    g.setColor( bgColor );
    g.fillRect( 0, 0, width, height ) ;
    g.setColor( txtColor ) ;
    g.drawString( text, 5, fm.getAscent() + 5 ) ;
    return bi ;
  public static void main(String[] args)
    BufferedImage bi = ImageMaker.makeImage( Color.red, Color.white, "TimYates", new Font( "Courier", Font.BOLD, 20 ) ) ;
    ImageMaker.saveBufferedImage( "/users/tyates/woo.jpg", bi );

To make a GIF, you will need a GIF encoder, and use that (I use the JPEG encode which comes with java in the obscure com.sun.image.codec.jpeg package)

 public static void main(String[] args)
    BufferedImage bi = ImageMaker.makeImage( Color.red,
                                                                  "Made at " + new Date().toString(),
                                                                  new Font( "Courier", Font.BOLD, 20 ) ) ;
    ImageMaker.saveBufferedImage( "/users/tyates/woo.jpg", bi );

will obviously do the time and date ;-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ssherlockAuthor Commented:
Thanks Tim, That was exactly what I was after (and the speed is acceptable as it's being written to a PDF anyway)
Cheers, Simon
:-)  Glad I could help :-)
ssherlockAuthor Commented:
Is it possible to use said image in memory rather than save it to disk?  The reason for this is that it will be created everytime a report is requested and then is no longer required.  Because it is an enterprise system I would rather not be creating and deleting small files on the server.

it depends on whether the PDF generator you use allows you to embed images...

Which one are you using?
ssherlockAuthor Commented:
iText (http://www.lowagie.com/iText/) for creating reports in PDF format on a J2EE system
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Graphics Software

From novice to tech pro — start learning today.