Is the applet signed? If it's not, you won't be able to print due to
Yes the applet is not a signed Applet. I think with the Java printng
API we can print from an unsigned applet also. It just that before
printing it asks for a confirmation something like "Do you want to
allow the applet wants to print?".
I am able to instantiate the print icon on the system tray but not
able to spool to the printer.
If I run the code as a applet application i.e. using appletviewer then
things are fine but as I run on an HTML page the print icon loads on
the system tray but the print outs are not there on the printer.
Please excuse the coding standards.
Thanks,
Java Hello World
Here is the snapshot of the code.
package Applet.unsigned;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.swing.*;
public class PrinterApplet extends JApplet {
//~ Instance fields
========================================================
private PrintRequestAttributeSet aset;
DocFlavor DocFlavor;
public Doc doc;
DocPrintJob printerJob;
JTextPane pane;
//~ Methods ================================================================
/**
* DOCUMENT ME!
*/
public void init() {
getContentPane().setLayout(new BorderLayout());
pane = new JTextPane();
pane.setSize(150, 100);
pane.setContentType("text/html");
pane.setText(
"<html><center><b><big>Applet Test</big></b><br>"
+ "</center></html>");
getContentPane().add(pane, "Center");
JPanel buttons = new JPanel();
buttons.setBackground(Color.white);
JButton print = new JButton("Print");
buttons.add(print);
getContentPane().add(buttons, "South");
print.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
print();
}
});
}
/**
* DOCUMENT ME!
*/
void prep() {
URL url = null;
aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(1));
try {
url =
new URL("
http://sohowww.nascom.nasa.gov//data//realtime//eit_171//512//latest.gif");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintService pservices =
PrintServiceLookup.lookupDefaultPrintService();
System.out.println(pservices.getName());
//DocFlavor flavor = javax.print.DocFlavor.INPUT_STREAM.GIF;
DocFlavor flavor = javax.print.DocFlavor.URL.GIF;
//doc = new InputStreamDoc("a.gif", flavor);
//doc = new InputStreamDoc(pane.getText(), flavor);
doc = new SimpleDoc(url, javax.print.DocFlavor.URL.GIF, null);
try {
System.out.println("DOC : \n " + doc.getPrintData());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Create a Print Job */
printerJob = pservices.createPrintJob();
}
/**
* DOCUMENT ME!
*/
void print() {
prep();
System.out.println("Printer Name : " +
printerJob.getPrintService());
try {
printerJob.print(doc, aset);
} catch (PrintException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Done Printing.");
}
}