Thanks a lot..
i have a small question...how can i have a jpg/ jpeg file? as my requirement is to have a jpeg file.. pls let me know
Main Topics
Browse All TopicsHello All, Can anybody provide me with an example, how to convert html file to an image file using Java?
Thanks in advance
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: Bart_CrPosted on 2007-10-05 at 12:20:54ID: 20024799
The code below will work for simple HTML pages, as JEditorPane can only render not to complex pages. The componentToImage method can be used for any Component subclass, so if you find a better HTML renderer somewhere you can use that to get the image result.
ge; vent; istener;
"); geListener (new PropertyChangeListener() { ngeEvent evt) { als("page" )) {
e, file);
comp, File file) throws IOException { ze = " + prefSize); th, comp.getPreferredSize().he ight, B);
Most components will work with just the method, but JEditorPane needs some extra magic to make sure the page is loaded before the converter method is called. That's where the PropertyChangeListener and the Thread.yield() come in.
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedIma
import java.beans.PropertyChangeE
import java.beans.PropertyChangeL
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class Test {
private static volatile boolean loaded;
public static void main(String[] args) throws IOException {
loaded = false;
URL url = new URL("http://www.google.com
JEditorPane editorPane = new JEditorPane();
editorPane.addPropertyChan
public void propertyChange(PropertyCha
if (evt.getPropertyName().equ
loaded = true;
}
}
});
editorPane.setPage(url);
while (!loaded) {
Thread.yield();
}
File file = new File("out.png");
componentToImage(editorPan
}
public static void componentToImage(Component
Dimension prefSize = comp.getPreferredSize();
System.out.println("prefSi
BufferedImage img = new BufferedImage(prefSize.wid
BufferedImage.TYPE_INT_ARG
Graphics graphics = img.getGraphics();
comp.setSize(prefSize);
comp.paint(graphics);
ImageIO.write(img, "png", file);
}
}