Printing file from applet in JDK 1.1


We need to print data consisting of text, graphics (such as horizontal and vertical lines etc.), headers and footers, different fonts (including fonts which are not supported by Java directly) using signed applets in JDK 1.1. How to do it?
We know that PrintJob class etc. support printing. But they require drawing the object using Graphics class. In our case we want to print both text and graphics with complex formatting. You can assume that the data which we want to print is similar to a WINWORD file (with changes in font, lines, shaded text etc.)

Can somebody provide inputs. (Heyhey, eagerly awaiting your response!)
Who is Participating?
vladi21Connect With a Mentor Commented:
good article with example

1) Your must sign applet in browser specific way

Code signing resources:
Creating Signed, Persistent Java Applets


3.4: Can I print from my Applet?
If you have the JDK 1.1 patch installed, you can print if you have enabled the UniversalPrintJobAccess privilege. 

2) Print
Java 1.1 
Using Design Patterns to Simplify Printing in Java 1.1 *
How do I print out a component in Java 1.1? 

Java 1.2
How can I speed up printing in my JDK 1.2 Swing application?

bugs in Java2 printing API 

  2. (Sect. 8) How do I print from a Java program?

     [*] Use the Toolkit.getPrintJob() method

     Component c = this.getParent();
     while (c!=null && !(c instanceof Frame))

     PrintJob pj = getToolkit().getPrintJob((Frame) c, "test", null);
     Graphics pg = pj.getGraphics();

     This feature was introduced with JDK 1.1. A common place to put this is
     in the code that handles a button press. Printing from an untrusted
     applet is subject to a check from the SecurityManager.

     The JDK 1.1 printing API is more a screen hardcopy facility than a full
     blown publishing and illustration hardcopy API. JDK 1.2 offers a more
     full-featured printing API.

     If you simply want to print text, then write it to a file and print the
     file. Or open a filename that corresponds to the printer. On Windows,
     that is "LPT1" and the code looks like:

     try {
         FileOutputStream fos = new FileOutputStream("LPT1");
         PrintStream ps = new PrintStream(fos);
                 ps.print("Your string goes here");
     } catch (Exception e) {
         System.out.println("Exception occurred: " + e);

     The final formfeed is needed by windows to start the printjob.

  3. (Sect. 8) What are the properties that can be used in a PrintJob? The
     properties are
        o awt.print.destination - can be "printer" or "file"
        o awt.print.printer - printer name
        o awt.print.fileName - name of the file to print
        o awt.print.numCopies - obvious
        o awt.print.options - options to pass to the print command
        o awt.print.orientation - can be "portrait" or "landscape"
        o awt.print.paperSize - can be "letter","legal","executive" or "a4"
     The defaults are destination=printer, orientation=portrait,
     paperSize=letter, and numCopies=1.

     You can search for info like this by joining the Java Developer
     Connection (it's free) at

     and doing a search for "PrintJob".

Without going into too much detail, Netscape Communicator enables signed applets to request permission to access various system resources. It is left up to the user to grant or deny access.

An applet requests access to a system's printing resources by enabling the UniversalPrintJobAccess privilege. Once the privilege has been granted, the applet can proceed exactly as outlined above. To enable the print job privilege, you use the following code:

try {

PrivilegeManager.enablePrivilege ("UniversalPrintJobAccess");
// proceed with printing as normal
PrivilegeManager.disablePrivilege ("UniversalPrintJobAccess");

} catch (ForbiddenTargetException e) {

// user denied applet permission to access resource


The JavaT Printing Project
a simple, easy to use, Pure JavaT, Open-Source printing package built on the current Java2 Platform printing API's.
Ability to print anything from plain text to complex documents.
PDF and PostScript output to files.
Print Preview with zoom in/zoom out ability.
Continuous updates through future Java PlatformT releases (i.e. Java3).

One known solution is to create HTML page, open it and let Internet Explorer print it.
It is acceptable in 70% cases because it is very simple and inacceptable in 30% cases because of nature of application (perhaps, end-users will not like pressing 'Print' button again, if that cannot be achieved from Java).

Hope this helps.
TikshyaAuthor Commented:
Hi vladi21,

Thanks for prompt and exhaustive response. One question about following code which you have provided:
     FileOutputStream fos = new FileOutputStream("LPT1");
                           PrintStream ps = new PrintStream(fos);
                                   ps.print("Your string goes here");
This LPT1 is applicable to local printer. Am I right? If I have a network printer, how to do the above mentioned thing?

Hi sereda,
Thnks for your response. In the case of HTML page, is it possible to automate the printing by using some javascript code?

Thanks in advance to both of you,
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

its for Network topic area :)
in windows95 you can map the port LPT1 to a network printer
in NT just share the printer on your windows NT machine. If you computer name is for example NT01 and you share your printer as PRINTER1. Now you can use the net use command for assigning the printer to the lpt1 port. for example : NET USE lpt1: \\NT01\PRINTER1 now every program that sends to the lpt1 port, will send it to \\NT01\PRINTER1

I don't know how to automate printing with JavaScript. Perhaps there is an Explorer extension to javascript standard (which is Netscape's dev) but i'm not aware about it. I positively sure that with VBScript or ActiveX you will be able to automate printing...

I undertand that this is ugly solution but it works and it's simple enough...

You're welcome,
All Courses

From novice to tech pro — start learning today.