<p>!_NEWLINE_!I need to print a swing form with a JTextArea on to <br> a printer. I want to print the <br> entire contents of the textarea </p>
1 posts in topic
Flat View  Flat View

Posted By:   shashikanth_sastry
Posted On:   Friday, August 17, 2001 12:50 AM

I need to print a swing form on to
a printer. The swing form contains
a JTextArea inside a JScrollPane
(ie, it has scrollbars) and
the textarea may contain a large text.
I want the entire text in the textarea to be printed. but it is printing only the contents visible to the user.

I have made the panel containing the rest of the GUI as Pageable and the
textarea also a Pageable and appending them to a Book object and printing
them.But it is not printing the full contents of the textarea.

You need to retrive the contents of the text area?

Posted By:   Shaji_Kalidasan  
Posted On:   Friday, August 17, 2001 03:37 AM

You may require to retrieve the contents of the text area into a string and with the help of the Java Print Service API provided with JDK1.4 beta you can do it easily.

just make use of the following packages





A sample program from the demo is provided here for your convenience. Please also check the JPS API Doc for more information here.



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

public class PrintPS {

public static void main(String args[]) {
PrintPS ps = new PrintPS();
public PrintPS() {
/* 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.POSTSCRIPT;
PrintRequestAttributeSet aset =
new HashPrintRequestAttributeSet();
aset.add(new Copies(2));

/* locate a print service that can handle it */
PrintService[] pservices =
PrintServiceLookup.lookupPrintServices(flavor, aset);
if (pservices.length > 0) {
System.out.println("selected printer " + pservices[0].getName());

/* create a print job for the chosen service */
DocPrintJob pj = pservices[0].createPrintJob();
try {
* Create a Doc object to hold the print data.
* Since the data is postscript located in a disk file,
* an input stream needs to be obtained
* BasicDoc is a useful implementation that will if requested
* close the stream when printing is completed.
FileInputStream fis = new FileInputStream("example.ps");
Doc doc = new SimpleDoc(fis, flavor, null);

/* print the doc as specified */
pj.print(doc, aset);

* Do not explicitly call System.exit() when print returns.
* Printing can be asynchronous so may be executing in a
* separate thread.
* If you want to explicitly exit the VM, use a print job
* listener to be notified when it is safe to do so.

} catch (IOException ie) {
} catch (PrintException e) {

With love,

About | Sitemap | Contact