JAVA : Take screenshot of text file


import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class MultiLineTextToImage{
	static String INPATH="";
	static String OUTPATH="";
	File f = null;
	PrintWriter writer = null;
	PrintWriter writer2 = null;
	PrintWriter writer3 = null;
	PrintWriter writer4 = null;
	PrintWriter writer5 = null;
	String tempfile="";
	public void convert(String inputpath, String outputpath)
		//String text = "Hello";
		INPATH = inputpath;
		OUTPATH = outputpath;
		String temptext="";
		String temptext2="";
		int flag1=0;int flag2=0;
		int setht=0;
  try {
								tempfile = OUTPATH+"Sample.txt";
								writer = new PrintWriter(tempfile, "UTF-8");
								writer.println("Hello Tobo, How are you?");
							} catch (Exception e) {
								// TODO Auto-generated catch block
           Because font metrics is based on a graphics context, we need to create
           a small, temporary image so we can ascertain the width and height
           of the final image
        int width = 1250;
        int height = 500;

        BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        Font font = new Font("Consolas", Font.PLAIN, 14);
        FontMetrics fm = g2d.getFontMetrics();


        img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        g2d = img.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        //fm = g2d.getFontMetrics();

        File file = null;
		try {
			file = new File(tempfile);
		} catch (Exception e) {
			// TODO Auto-generated catch block

        BufferedReader br=null;
        int nextLinePosition=15;
        int fontSize = 12;
        try {
            br = new BufferedReader(new FileReader(file));

            String line;
            while ((line = br.readLine()) != null) {
               g2d.drawString(line, 0, nextLinePosition);
               nextLinePosition = nextLinePosition + fontSize;
        } catch (FileNotFoundException ex) {
            //Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            //Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex);

        try {
            ImageIO.write(img, "png", new File(tempfile.substring(0, tempfile.indexOf(".txt"))+".png"));
        } catch (IOException ex) {
		catch(Exception ytr) {ytr.printStackTrace();}

In above code there is a text file named “sample.txt”, it gets converted to “sample.png”. Just call the convert method and pass input path and output path as arguements and execute the code.

Happy coding


5 thoughts on “JAVA : Take screenshot of text file

  1. Does your blog have a contact page? I’m having trouble locating it but, I’d
    like to send you an email. I’ve got some recommendations for your blog
    you might be interested in hearing. Either way, great website and I look forward to seeing it
    grow over time.

  2. Link exchange is nothing else but it is simply placing the other person’s weblog link
    on your page at proper place and other person will also do same in support
    of you.

  3. This design is steller! You certainly know how to keep
    a reader amused. Between your wit and your videos,
    I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job.
    I really loved what you had to say, and more than that, how you presented it.
    Too cool!

Leave a Reply

Your email address will not be published.