Solved

print in Java

Posted on 2003-11-26
8
768 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now