Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.*;
public class printtest {
public static void main(String[] args) {
try {
// Open the image file
InputStream is = new BufferedInputStream(
new FileInputStream("filename.gif"));
String testdata = "hello world \n have a nice day.";
// Find the default service
// DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
// DocFlavor flavor = DocFlavor."text/plain"; <<-this will not compile
// DocFlavor flavor = DocFlavor.STRING; // <<-this will not compile
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
// Create the print job
DocPrintJob job = service.createPrintJob();
Doc doc= new SimpleDoc(testdata, flavor, null);
// Monitor print job events; for the implementation of PrintJobWatcher,
// see e702 Determining When a Print Job Has Finished
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) {
}
}
static 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) {
}
}
}
}
public class printtest {
public static void main(String[] args) {
try {
// Open the image file
char[] testdata = "hello world \n have a nice day.".toCharArray();
//DocFlavor flavor = DocFlavor.STRING.TEXT_PLAIN;
DocFlavor flavor = DocFlavor.CHAR_ARRAY.TEXT_PLAIN;
// Find the default service
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
// Create the print job
DocPrintJob job = service.createPrintJob();
Doc doc= new SimpleDoc(testdata, flavor, null);
// Print it
job.print(doc, null);
} catch (PrintException e) {
System.out.println("print exception");
System.out.println(e.getMessage());
}
}
}
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.*;
public class PrintTest {
public static void main(String[] args) {
try {
// Open the image file
String testData = "Hello World !";
InputStream is = new ByteArrayInputStream(testData.getBytes());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE ;
// Find the default service
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(service);
// Create the print job
DocPrintJob job = service.createPrintJob();
Doc doc= new SimpleDoc(is, flavor, null);
// Monitor print job events; for the implementation of PrintJobWatcher,
// see e702 Determining When a Print Job Has Finished
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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
static 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) {
}
}
}
}
public class printtest {
public static void main(String[] args) {
try {
// Open the image file
String testData = "hello world \r\n have a nice day.";
InputStream is = new ByteArrayInputStream(testData.getBytes());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE ;
//DocFlavor flavor = DocFlavor.STRING.TEXT_PLAIN;
//DocFlavor flavor = DocFlavor.CHAR_ARRAY.TEXT_PLAIN;
// Find the default service
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(service);
// Create the print job
DocPrintJob job = service.createPrintJob();
Doc doc= new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
// Print it
job.print(doc, null);
pjDone.waitForDone();
// It is now safe to close the input stream
is.close();
} catch (PrintException e) {
System.out.println("print exception");
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println("IO exception");
e.printStackTrace();
}
}
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
System.out.println(service +" supports :");
DocFlavor[] flavors = service.getSupportedDocFlavors() ;
for (int i = 0; i < flavors.length; i++) {
System.out.println("\t" +flavors[i]);
}