You can install PDF printing, which can act as a virtual printer for your Java application. Basically what you need to do is install a freely available PDF printer and make your Java application discover this print service and print any document for this service. I remember, I had the same situation when I did not have a printer, I used the code below to interface my application with a virtual printer.
public class HelloWorldPrinter implements Printable, ActionListener { public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if (page > 0) { return NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D)g; g2d.translate(pf.getImageableX(), pf.getImageableY()); g.drawString("Hello world!", 100, 100); return PAGE_EXISTS; } public void actionPerformed(ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(this); PrintService[] printServices = PrinterJob.lookupPrintServices(); try { job.setPrintService(printServices[0]); job.print(); } catch (PrinterException ex) { Logger.getLogger(HelloWorldPrinter.class.getName()).log(Level.SEVERE, null, ex); } } public static void main(String args[]) { UIManager.put("swing.boldMetal", Boolean.FALSE); JFrame f = new JFrame("Hello World Printer"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); JButton printButton = new JButton("Print Hello World"); printButton.addActionListener(new HelloWorldPrinter()); f.add("Center", printButton); f.pack(); f.setVisible(true); } }
Abdul fatah
source share