Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Help ME



I have written a code wherein i wish to print an html page browser look to printer . . . .

But the problem is . . My page also contains images which do not get printed . .

I have used JEditoPane component in JFrame . . to display my page . . . .
But in the place of image all i can see is a broken icon image . . .

Please help me out friends . .

I have done enough reading . . dun suggest any reading stuff

if anyone can help wid the code . . it will be a great favour . .

Here's The Code :

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
import java.io.*;
import java.net.*;
import javax.swing.text.EditorKit;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.Document;
public class PrintUtilities implements Printable {

JEditorPane componentToBePrinted;

public PrintUtilities(JEditorPane componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;
}

public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try {
System.out.println("Calling PrintJob.print()");
//Thread.sleep(5000);
printJob.print();
System.out.println("End PrintJob.print()");
//System.exit(1);
}
catch (PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}

public static void main(String args[])
{
JFrame jf=new JFrame();
JEditorPane p=new JEditorPane();
HTMLEditorKit kit = new HTMLEditorKit()
{
public ViewFactory getViewFactory()
{
return new HTMLFactory()
{
public View create(Element elem)
{
View view = super.create(elem);

if (view instanceof ImageView)
{
((ImageView) view).setLoadsSynchronously(true);
}
return view;
}
};
}
};
p.setEditorKit(kit);
jf.getContentPane().add(p);
p.setContentType("text/html");
try
{
String s = "file:"
+ "c:\\print.html";
URL helpURL = new URL(s);
p.setPage(helpURL);

}catch(Exception e)
{
e.printStackTrace();
}
jf.setVisible(true);
PrintUtilities pu=new PrintUtilities(p);
pu.print();

}

public int print(Graphics g, PageFormat pf, int pageIndex) {
int response = NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D) g;
// for faster printing, turn off double buffering
disableDoubleBuffering(componentToBePrinted);
Dimension d = componentToBePrinted.getSize(); //get size of document
double panelWidth = d.width; //width in pixels
double panelHeight = d.height; //height in pixels
double pageHeight = pf.getImageableHeight(); //height of printer page
double pageWidth = pf.getImageableWidth(); //width of printer page
double scale = pageWidth / panelWidth;
int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight);
// make sure not print empty pages
if (pageIndex >= totalNumPages) {
response = NO_SUCH_PAGE;
}
else {
// shift Graphic to line up with beginning of print-imageable region
g2.translate(pf.getImageableX(), pf.getImageableY());
// shift Graphic to line up with beginning of next page to print
g2.translate(0f, -pageIndex * pageHeight);
// scale the page so the width fits...
g2.scale(scale, scale);
componentToBePrinted.paint(g2); //repaint the page for printing
enableDoubleBuffering(componentToBePrinted);
response = Printable.PAGE_EXISTS;
}
return response;
}

public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}

public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}

NIKHIL.
nikhil_brahmbhatt@hotmail.com