You are on page 1of 3

Experiment no: 33

Program to Print any message or Output to the Default Printer

Printing to a default printer in Java involves several steps, and it depends on the platform you
are using (Windows, Linux, macOS). Here's a general approach using Java's javax.print
package:

1. Get the Default Printer: First, you need to obtain the default printer connected to the
system.
2. Create a Printable Object: Implement the Printable interface to define what should be
printed.
3. Create a PrinterJob: This class controls the printing process.
4. Set the Printable Object to PrinterJob: Associate the Printable object with the
PrinterJob.
5. Print the Job: Use the print() method of PrinterJob to start the printing process.

1. To Print any message or Output to the Default Printer in java.

G PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();

2.Create a Printable Object


public class MyPrintable implements Printable
{
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws
PrinterException {
if (pageIndex > 0) {
return Printable.NO_SUCH_PAGE;
}

// Your printing logic here


graphics.drawString("Hello, World!", 100, 100);

return Printable.PAGE_EXISTS;
}
}

3. Create a PrinterJob:

printerJob.setPrintable(new MyPrintable());

4. Print the Job:

try {
printerJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
Sample Code:
package test;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.PageRanges;

public class DirectPrint implements Printable {

private PrintService[] printService;


private String text;

public DirectPrint() {
this.printService = PrinterJob.lookupPrintServices();
}

public static void main(String[] args) {


DirectPrint lt = new DirectPrint();
lt.printString("If this text gets printed, it will have worked! ;D");
}

public void printString(String input) {

this.text = input;

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();


aset.add(new PageRanges(1, 1));
aset.add(new Copies(1));

PrinterJob printJob = PrinterJob.getPrinterJob();


printJob.setPrintable(this);

try {
printJob.setPrintService(printService[7]);
//index of installed printers on you system
//not sure if default-printer is always '0'
printJob.print(aset);
} catch (PrinterException err) {
System.err.println(err);
}
}

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {


Graphics2D g2 = (Graphics2D) g;
g2.translate(pf.getImageableX(), pf.getImageableY());
g.drawString(String.valueOf(this.text), 14, 14);
return PAGE_EXISTS;
}
}

Printing a message or output to the default printer in Java can be useful in various scenarios,
such as generating reports, invoices, or any document that needs to be physically printed.
Here are some common use cases:

 Point of Sale (POS) Systems: In retail environments, when a transaction is


completed, a receipt containing details of the purchase can be printed automatically.
 Billing and Invoicing Systems: In businesses, invoices generated from accounting
software can be sent directly to the default printer for printing.
 Report Generation: When generating reports from an application, such as sales
reports, financial statements, or analytics summaries, you might want to print them for
further analysis or sharing.
 Ticketing Systems: In transportation or entertainment industries, tickets for flights,
trains, movies, concerts, etc., can be printed for customers upon booking.
 Label Printing: In warehouses or manufacturing facilities, labels for products,
packages, or shipments can be printed automatically as part of the workflow.
 Document Management Systems: Printing documents for archival purposes or for
physical distribution.
 Order Fulfillment: Printing packing slips or shipping labels for orders that need to
be shipped out.
 Healthcare Systems: Printing patient records, prescriptions, or medical reports in
hospitals and clinics.
 Educational Systems: Printing worksheets, exam papers, or other educational
materials in schools or colleges.
 Administrative Tasks: Printing forms, contracts, or other administrative documents
in various organizations.

In all these scenarios, the ability to print messages or outputs directly to the default printer in
Java allows for seamless integration of printing functionality into the application workflow,
improving efficiency and reducing manual intervention.

You might also like