hi,
This is my code snippet.could u find the mistake in this code.if i run
this applet i am getting exception and if i run this code as normal
java application printer is getting activated but i am unable to get
printout.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import java.io.FileInputStream;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.swing.*;
import org.pdfbox.*;
import java.util.Properties;
//<applet code=PrinterApplet width=500 height=500></applet>
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
{
FileInputStream fin =new FileInputStream("PrinterApplet.html");
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintService pservices =
PrintServiceLookup.lookupDefaultPrintService();
System.out.println(pservices.getName());
doc = new SimpleDoc(fin,
javax.print.DocFlavor.INPUT_STREAM.TEXT_HTML_UTF_8, 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();
//printerJob = ps.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.");
}
}
> > Hi All,
> > 1) I want to print my html page from applet.If i run my applet
[quoted text clipped - 18 lines]
>
> Andrew T.
Andrew Thompson - 04 Jan 2007 10:45 GMT
> This is my code snippet.
..
Its crap, please don't waste our bandwidth with 138
lines of uncompilable rubbish. Post an SSCCE for
further help (from me).
http://www.physci.org/codes/sscce
(I had a longer response, but 'Google ate it', so
you got the short, sharp version)
Andrew T.
Andrew Thompson - 04 Jan 2007 10:48 GMT
> hi,
Please refrain from top-posting. It is most confusing.
> This is my code snippet.
Please don't waste our bandwidth with snippets, since..
>...could u find the mistake in this code.if i run
> this applet i am getting exception and if i run this code as normal
> java application ...
...
The code as posted is not compilable in any place
but your environtment due to the ..
a) import of import org.pdfbox.*;
..and other reasons it cannot be comipled are..
b) line wraps intorduced in posting long code lines to usenet
c) 2+ missing code parts (I.E. starting with the failure
to declare 'SimpleDoc', nor. declare and instantiate 'fin'
..and after having kludged the 'snippet' to fix those errors,
I then find it ..
d) it has no main()!
I suggest you stop wasting our bandwidth, and your time,
by posting an SSCCE of code that fails. For description
and hints, see..
http://www.physci.org/codes/sscce
So.. coming back to..
>...could u find the mistake in this code.
Do you mean.
"Could you find the mistake in this code?"
(note the Upper Case first letter, correct spelling
of 'you', and closing '?' - which denotes a question)
No. I will look no further at it.
Andrew T.