Solved

print in Java

Posted on 2003-11-26
8
774 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 78
jsp login check 12 67
glassfish admin console 1 52
Java List 4 76
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

751 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