Solved

print in Java

Posted on 2003-11-26
8
771 Views
Last Modified: 2008-02-20
I Want to do a Mass Print operation for some 20 Documents.. Documents can be .doc/.rtf/.pdf

Without Opening those files all the 20 documents should be printed.

Can any body help in this problem.. Any ideas..

Using Java i have to do this
Thanks
0
Comment
Question by:chgk
  • 4
8 Comments
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9829811
0
 

Author Comment

by:chgk
ID: 9831509
I saw the above link.. But still if u can ellobarate..or any sample code

It will be very helpful

0
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9833967
I've just had a little play with print api... here's a html printer class.  I still need to put a catch printer controller in because i almost wasted half a ream of paper! (doop)


import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.*;

public class print  {
   
    public static void main(String args[]) {
        print ps = new print();
    }
    public print() {
                /* Construct the print request specification.
                 * The print data is Postscript which will be
                 * supplied as a stream.  The media size
                 * required is A4, and 2 copies are to be printed
                 */
        //DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_HTML_UTF_8;
       
        //DocFlavor flavor = DocFlavor.STRING.TEXT_HTML_UTF_8;
        DocFlavor flavor = DocFlavor.STRING.TEXT_HTML;
        //DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        PrintRequestAttributeSet aset =
        new HashPrintRequestAttributeSet();
        aset.add(MediaSizeName.ISO_A4);
        aset.add(new Copies(2));
        aset.add(Sides.TWO_SIDED_LONG_EDGE);
        aset.add(Finishings.STAPLE);
       
        /* locate a print service that can handle it *//*
        PrintService[] pservices =
        PrintServiceLookup.lookupPrintServices(flavor, aset);*/
       
        System.out.println(PrintServiceLookup.lookupDefaultPrintService());
       

            DocPrintJob pj = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();
            try {
 
                FileInputStream fis = new FileInputStream("c:\\test.html");
                Doc doc = new SimpleDoc(fis, flavor, null);                
               
                /* print the doc as specified */
                pj.print(doc, aset);
               

               
            } catch (IOException ie) {
                System.err.println(ie);
            } catch (PrintException e) {
                System.err.println(e);
            }

    }
}


// you'll have to change the flavor to pdf, rtf and play with their settings (to avoid invalid flavor
// errors)
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
LVL 4

Accepted Solution

by:
Howie_Ly earned 40 total points
ID: 9835289
JPS is however a little buggy, or more correctly not mature yet (as you require a third party print service to use the pdf printing functions).

There is of course a much cleaner solution if you're deploying this application on windows.

This comes courtesy of the [chicks] from the p-pdf developers forum (i lost the original link so i'll post it here).

<cut>
An example of viewing/printing PDF or other file types from Java on Windows.

Uses Windows ShellExecute function, with "print", "printto" or "open" verbs. ShellExecute will associate file with its viewer using the file's extension.

Viewers (Adobe Reader, Word, Excel, etc.) must be installed for this to work.

Extract ShellExec.dll from the attached jar to the current directory, then test with:

java -cp ShellExec.jar Test mypdf.pdf

Which will open "mypdf.pdf" in Adobe Reader. Change Test.java to "print" or "printto" to test printing.
</cut>

You can download shellexec.jar from here
http://www.dmac.adelaide.edu.au/shellexec.jar

It's only temporary storage on our webserver, so i suggest you dl it soon.

Just a comment I'd like to add to this, it uses a dll written in c that accesses windows directly.  It is a prerequisite that adobe acrobat reader is installed to print pdf's , or word (or word viewer) is installed to print doc's.  But it's the simplest solution i've found so far that works.


0
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9847008
did you have any luck chgk?
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 11101977
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

828 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