Solved

Applet printing to local printer

Posted on 2000-02-16
3
1,429 Views
Last Modified: 2013-12-29
Does anyone have sample codes for an applet that reads a file from
a server and print it out over at the user side (local printer).

The applet should be able to specify whether to print in portrait or
landscape format.

All I need is  a simple applet with one print button.
File to be printed can be hardcoded.

Thanks.
0
Comment
Question by:slok
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
vladi21 earned 100 total points
ID: 2526404
look:

Reads From A URL : example
http://www.mindspring.com/~david.dagon/applets/ReadsFromURL.html

java how-to IO part 1
http://tactika.com/realhome/javaht/java-f1.html


good PRINTING article with example
PRINTING FROM AN APPLET
http://www.adelaide.net.au/~bmodra/PrintJob.html

1) Your must sign applet in browser specific way

Code signing resources:
Creating Signed, Persistent Java Applets http://www.ddj.com/articles/1999/9902/9902h/9902h.htm
http://www.javasoft.com/products/jdk/1.1/docs/guide/security/index.html 
http://java.sun.com/security/signExample/index.html 
http://www.verisign.com/library/guide/developer/signing/index.html 
http://www.suitable.com/Doc_CodeSigning.shtml 
http://www.securingjava.com/appdx-c/ 
http://www.fastlane.net/~tlandry/javafaq.txt

MS:
http://www.thawte.com/support/developer/ms.html 
http://www.developer.com/journal/techworkshop/curr.html 
http://www.verisign.com/library/guide/developer/authenticode/index.html 

NN:
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.
http://developer.netscape.com/docs/manuals/signedobj/targets/contents.htm 
http://developer.netscape.com/support/faqs/objfaq.html 
http://developer.netscape.com/docs/manuals/signedobj/javadoc/Package-netscape_security.html 


2) Print
Java 1.1
http://www.developer.com/news/techworkshop/012298_javaprint.html 
Using Design Patterns to Simplify Printing in Java 1.1 *
http://www.ibm.com/java/education/printing/index.html
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-Printing.html
http://java.sun.com/products/jdk/1.1/docs/guide/awt/designspec/printing.html
How do I print out a component in Java 1.1?
http://www.inquiry.com/techtips/java_pro/answer.asp?pro=java_pro&docID=1869 


Java 1.2
http://manning.spindoczine.com/sbe/files/uts2/Chapter22html/Chapter22.htm
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/render.html
http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/advprint.html
http://java.sun.com/printing/jdk1.2/printing-chapter-v2.txt
How can I speed up printing in my JDK 1.2 Swing application?
http://developer.java.sun.com/developer/qow/archive/20/index.html
Java 2D: New Printing Package
http://developer.java.sun.com/developer/technicalArticles/Printing/Java2DPrinting/index.html
Java 2 Platform Introduces Print Capability to the Swing Forum
http://developer.java.sun.com/developer/technicalArticles/Printing/SwingPrinting/index.html

bugs in Java2 printing API
http://developer.java.sun.com/developer/bugParade/bugs/4184099.html 
http://developer.java.sun.com/developer/bugParade/bugs/4201135.html 
http://developer.java.sun.com/developer/bugParade/bugs/4038693.html 
http://developer.java.sun.com/developer/bugParade/bugs/4197648.html 
http://developer.java.sun.com/developer/bugParade/bugs/4143967.html 
....




  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))
         c=c.getParent();

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


     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.
     http://java.sun.com/printing/

     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");
                 ps.print("\f");
                 ps.close();
     } 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 http://java.sun.com/jdc.

     and doing a search for "PrintJob".

---
http://www.developer.com/news/techworkshop/012298_javaprint.html
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:

import netscape.security.PrivilegeManager;
import netscape.security.ForbiddenTargetException;
..
..
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
http://www.wam.umd.edu/~aweiland/jpp/2
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).
---
Network printer
in windows95 you can map the port LPT1 to a network printer (-add new printer -local printer -select driver -select port (LPT1) -Install drivers ect.
-Right click...Properties ...capture printer port... -Select LPT1 then use the pass to my 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
(/Persistent:yes)


0
 
LVL 8

Expert Comment

by:diakov
ID: 2527781
Yeah, 'copy and paste' him well!
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2527802
:)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example for saving data 19 52
login form jsp example 2 37
going to wrong jsp page 2 33
Need Help! Getting a syntax error and don't understand why 3 31
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question