आप एक पीडीएफ प्रिंट स्थापित कर सकते हैं जो आपके जावा एप्लिकेशन के लिए वर्चुअल प्रिंटर के रूप में काम कर सकता है। असल में, आप क्या करना चाहते हैं, एक स्वतंत्र रूप से उपलब्ध पीडीएफ प्रिंटर स्थापित करें और अपना जावा एप्लिकेशन उस प्रिंट सेवा को खोजें और उस सेवा को जो भी दस्तावेज़ प्रिंट करें। मुझे याद है, मेरे पास एक ही स्थिति थी जब मेरे पास प्रिंटर नहीं था, मैंने वर्चुअल प्रिंटर के साथ अपने एप्लिकेशन को इंटरफ़ेस करने के लिए नीचे दिए गए कोड का उपयोग किया था।
public class HelloWorldPrinter implements Printable, ActionListener {
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now we perform our rendering */
g.drawString("Hello world!", 100, 100);
/* tell the caller that this page is part of the printed document */
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);
}
}
स्रोत
2014-11-27 16:48:54
क्या "पीडीएफ प्रिंटर" काम करेगा? मुझे यकीन नहीं है कि प्रत्येक बार बनाए गए दस्तावेज़ स्वचालित परीक्षण सूट के हिस्से के रूप में उपयोग करने के लिए पर्याप्त अद्वितीय होंगे। मुझे भी यकीन है कि ट्रे जैसे प्रिंटर जैसी चीजों का पर्दाफाश नहीं करेगा। – Charles
क्या मॉक प्रिंटर को वास्तव में इनपुट के साथ कुछ भी करने की ज़रूरत है, या यह ठीक है अगर यह केवल सामान स्वीकार करता है और इसे एनयूएल में भेजता है? – eis
ने इसे इंटरनेट में खोजा है? – ggcodes