Solved

print in Java

Posted on 2003-11-26
8
775 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

635 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