Change so that I can print .txt or .rtf  
Author Message
bH





PostPosted: 2006-11-14 23:29:00 Top

java-programmer, Change so that I can print .txt or .rtf Hi All,
I have tested this program below using a .gif and it works
successfully.
I want to change it so that I can print a notepad.txt or wordpad.rtf.

TIA,
bH

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

public class BasicPrint {
public static void main(String[] args) {
try {
// Open the image file
InputStream is = new BufferedInputStream(
new FileInputStream("BIGbggrn01.gif"));

// Find the default service
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
PrintService service =
PrintServiceLookup.lookupDefaultPrintService();

// Create the print job
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);

// Monitor print job events; for the implementation of
PrintJobWatcher,

PrintJobWatcher pjDone = new PrintJobWatcher(job);

// Print it
job.print(doc, null);

// Wait for the print job to be done
pjDone.waitForDone();

// It is now safe to close the input stream
is.close();
} catch (PrintException e) {
} catch (IOException e) {
}
}
}
class PrintJobWatcher {
// true iff it is safe to close the print job's input stream
boolean done = false;

PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}

 
Thomas Fritsch





PostPosted: 2006-11-15 1:50:00 Top

java-programmer >> Change so that I can print .txt or .rtf bH schrieb:
> Hi All,
> I have tested this program below using a .gif and it works
> successfully.
> I want to change it so that I can print a notepad.txt or wordpad.rtf.
>
> TIA,
> bH
>
[...]
> import javax.print.*;
[...]
> DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
[...]

You did already look into the API doc of javax.print.DocFlavor, didn't
you? If you'd do so, it should be pretty obvious how to modify your code
from printing GIF to printing plain text.

--
Thomas
 
bH





PostPosted: 2006-11-15 7:45:00 Top

java-programmer >> Change so that I can print .txt or .rtf

Thomas Fritsch wrote:
> bH schrieb:
> > Hi All,
> > I have tested this program below using a .gif and it works
> > successfully.
> > I want to change it so that I can print a notepad.txt or wordpad.rtf.
> >
> > TIA,
> > bH
> >
> [...]
> > import javax.print.*;
> [...]
> > DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
> [...]
>
> You did already look into the API doc of javax.print.DocFlavor, didn't
> you? If you'd do so, it should be pretty obvious how to modify your code
> from printing GIF to printing plain text.
>
> --
> Thomas


Hi Thomas,

I tried this replacement and there was no motion in the printer:

InputStream is = new BufferedInputStream(
new FileInputStream("googl.txt"));

// Find the default service
DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;

and I tried this also with no success:

// Find the default service
DocFlavor flavor =
DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII;


I also tried to study this Sun html page below. But again I did not get
a solution because I cannot see how to apply the information.

http://java.sun.com/j2se/1.4.2/docs/guide/jps/spec/docflavor.fm1.html

I need more help, please.

bH