Tip

Converting Web pages to images using Java

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

VIEW MEMBER FEEDACK TO THIS TIP
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:

  • FAQ: Java for Lotus Notes and Domino
  • Reference Center: Java tips and resources

    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

  • There are Comments. Add yours.

     
    TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

    REGISTER or login:

    Forgot Password?
    By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
    Sort by: OldestNewest

    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:

    Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.