# 2 Re: Printing a html page through Swings
I am having the problem of printing an HTML page displayed on a JEditorPane which is more than a single page. Here is my code . Please correct it . Thank you inadvance.
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.print.*;
import java.awt.geom.*;
import javax.swing.text.html.*;
import java.util.*;
public class HTMLBrowser extends JFrame {
final JEditorPane jep = new JEditorPane();
final JScrollPane jsp = new JScrollPane(jep);
public HTMLBrowser(String startingUrl) {
super("Test Pane");
setSize(600,700);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
we.getWindow().setVisible(false);
System.exit(0);
}});
JPanel urlPanel = new JPanel();
urlPanel.setLayout(new BorderLayout());
JTextField urlField = new JTextField(startingUrl);
urlPanel.add(new JLabel("Site: "), BorderLayout.WEST);
urlPanel.add(urlField, BorderLayout.CENTER);
final JLabel statusBar = new JLabel(" ");
jep.setEditable(false);
// force the pane to use our new editor kit
jep.setEditorKitForContentType("text/html", new HTMLEditorKit());
try {
jep.setPage(startingUrl);
} catch(Exception e) {statusBar.setText("Could not open starting page. Using a blank.");}
JButton btnPrint = new JButton("Print");
btnPrint.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat aPageFormat = printJob.defaultPage();
printJob.setPageable(prepareBook(aPageFormat));
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception ex) {ex.printStackTrace();}
}
}
});
getContentPane().add(jsp, BorderLayout.CENTER);
getContentPane().add(urlPanel, BorderLayout.NORTH);
getContentPane().add(btnPrint, BorderLayout.SOUTH);
urlField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String urlText = null;
try {
if(ae.getSource() instanceof JTextField){
urlText = ((JTextField)ae.getSource()).getText();
}
jep.setPage(urlText);
}
catch(Exception e) {statusBar.setText("Could not open starting page. Using a blank.");}
}
});
}
public Book prepareBook(PageFormat thePageFormat){
Book aBook = new Book();
PrintReport aPrintReport = new PrintReport();
int aPageCount = aPrintReport.getPageCount(thePageFormat);
aBook.append(aPrintReport, thePageFormat, aPageCount);
return aBook;
}
class PrintReport implements Printable{
public int print(Graphics g, PageFormat thePageFormat, int theIndex){
Graphics2D g2d = (Graphics2D)g;
if(theIndex >= getPageCount(thePageFormat)) return Printable.NO_SUCH_PAGE;
drawPage(g2d, thePageFormat, theIndex);
return Printable.PAGE_EXISTS;
}
public void drawPage(Graphics2D g2d, PageFormat thePageFormat, int theIndex){
Shape aClipShape = g2d.getClip();
Rectangle2D.Double rectangle = new Rectangle2D.Double(0,theIndex*thePageFormat.getImageableHeight(), thePageFormat.getImageableWidth(), thePageFormat.getImageableHeight());
g2d.setClip(rectangle);
layoutPage(g2d, thePageFormat);
jep.paint(g2d);
g2d.setClip(aClipShape);
}
public void layoutPage(Graphics2D g2d, PageFormat thePageFormat){
Dimension dim = jep.getSize();
double scaleFactor = (double)thePageFormat.getImageableWidth()/dim.width;
g2d.scale(scaleFactor, 1);
g2d.translate(thePageFormat.getImageableX(), thePageFormat.getImageableY());
}
public int getPageCount(PageFormat thePageFormat){
Dimension dim = jep.getSize();
RepaintManager manager = RepaintManager.currentManager(jep);
final Image htmlImage = manager.getOffscreenBuffer(jep, dim.width, dim.height);
return (int)Math.ceil(htmlImage.getHeight(null)/thePageFormat.getImageableHeight());
}
}
public static void main(String args[]) {
(new HTMLBrowser(args.length > 0
? args[0]
:"file:///C:/WINNT/Profiles/nag/Desktop/Browser/HTMLReport.html")).setVisible(true);
}
}
nag143 at 2007-11-10 2:54:37 >
