Converting Web pages to images using Java

Learn how you can use Java code to convert Web pages to images with the click of a button.

I was recently asked to come up with some Java code that would convert Web pages into images.

My objective was to have the Web page become an image upon the clicking of a button or when certain events happened. Below is the Java code I came up with, I hope it works as well for you as it did for me.

//Call the Web page and convert to Image
BufferedImage ire;
ire = WebImage.create("<web page URL>", 800, 600);
//You can convert the BufferedImage to 
any format that you wish, jpg I thought was the best format
ImageIO.write(ire, "jpg", new File
("c:\\Temp\\tt.jpg"));

//Class that Converts the web page to Image
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public abstract class WebImage 
{ 
static class Kit extends HTMLEditorKit 
{
public Document createDefaultDocument() {
HTMLDocument doc = 
(HTMLDocument) super.createDefaultDocument();
doc.setTokenThreshold(Integer.MAX_VALUE);
doc.setAsynchronousLoadPriority(-1);
return doc;
}
}

public static BufferedImage create
(String src, int width, int height) {
BufferedImage image = null;
JEditorPane pane = new JEditorPane();
Kit kit = new Kit();
pane.setEditorKit(kit);
pane.setEditable(false);
pane.setMargin(new Insets(0,0,0,0));
try {
pane.setPage(src);
image = new BufferedImage
(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
Container c = new Container();
SwingUtilities.paintComponent
(g, pane, c, 0, 0, width, height);
g.dispose();
} catch (Exception e) {
System.out.println(e);
}
return image;
}
}

MEMBER FEEDBACK TO THIS TIP

Wouldn't it just be easier to hit the PrintScreen button or Alt+PrintScreen to bring up only the active window?
—Leonard V.

Do you have comments on this tip? Let us know.

Related information from SearchDomino.com:

This tip was submitted to the SearchDomino.com tip library by member Michael Marcavage. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.

This was first published in March 2007

Dig deeper on Java for Lotus Notes Domino

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.UK

Close