Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
How to print using printer font and not graphics

804881
Member Posts: 12
Dear experts,
I have created the following code to send string to the default printer.
I have modified the code as suggested by Oracle tutorial:
1. Is there a way that print(...) method of Printable interface can send ASCII character and not graphics rendering?
2. How can I prevent the program from halting whenever the printer is off-line, if I use
3. Is there another way to send String to printer with printer management like Printable interface, but without graphics rendering?
Thank you for your guidance,
Patrick
I have created the following code to send string to the default printer.
import java.io.FileWriter; import java.io.IOException; public class PrinterTest1 { public static void main(String[] args) { try{ FileWriter out = new FileWriter("/dev/lp0"); out.write(0x1B); // ESC out.write(0x40); // @ - initialize printer out.write(0x1B); // ESC out.write(0x4D); // M - Select 10.5-point, 12-cpi out.write("Hello World!"); out.write(0x0A); // Line feed out.write("Second line"); out.write(0x0A); // Line feed out.write(0x0D); // Carriage return out.write(0x0C); // Form feed out.close(); }catch(IOException e){ e.printStackTrace(); } System.out.println("Printing is done!"); } }The print out is fine, but whenever the printer is off-line, the program will halt.
I have modified the code as suggested by Oracle tutorial:
/* * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle or the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.print.*; 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); // boolean ok = job.printDialog(); boolean ok = true; // go ahead without print dialog if (ok) { try { job.print(); } catch (PrinterException ex) { /* The job did not successfully complete */ } System.out.println("End of test print!"); } } 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); } }The problem is that the printed-out font is a java font and not a built-in printer font, which is my preference. Also, I would like to control the ESC/P printer by sending ESC command. My questions are:
1. Is there a way that print(...) method of Printable interface can send ASCII character and not graphics rendering?
2. How can I prevent the program from halting whenever the printer is off-line, if I use
FileWriter out = new FileWriter("/dev/lp0");to send String to printer?
3. Is there another way to send String to printer with printer management like Printable interface, but without graphics rendering?
Thank you for your guidance,
Patrick
Answers
-
801878 wrote:Not that I know of, but note that you can use the Graphics.setFont(Font) method on your Graphics/Graphics2D to cause the printing to use whatever font (or combination of fonts) you want.
1. Is there a way that print(...) method of Printable interface can send ASCII character and not graphics rendering? -
I have solved the problem. Instead of using FileWriter, I use FileChannel that can be interrupted. Please refer to the following link for further details.
http://forward.com.au/javaProgramming/HowToStopAThread.html
This discussion has been closed.