Solved

print in Java

Posted on 2003-11-26
8
766 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 72
powerN  challenge 3 47
Passing list of object to Oracle Database Procedure 3 42
topping1 challenge 7 53
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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

762 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

20 Experts available now in Live!

Get 1:1 Help Now