Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to print using printer font and not graphics

804881Oct 12 2010 — edited Oct 18 2010
Dear experts,

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

Comments

L. Fernigrini

Hi, the forum is for all XE versions (actually, 11.2 and 18c). You can post any question here.

Murray Sobol

I recently installed Oracle 18c Express Edition on my Windows 10x laptop.
When I tried to run DBCA i got this message:
image.pngWhen I researched this error one suggestion was to use a TRY/Catch block to trap this error; I shouldn't be trying to change DBCA.
My background is with Oracle 11g on windows servers; I am researching a project to migrate databases to Oracle 19.
I would have preferred to install an Oracle 19 Express edition but apparently that does not exist.
My account with oracle is a "free" one so I don't have access to knowledge base articles.
I am curious - did something change between Oracle 11 and Oracle 18/19 with respect to language support.
Amy assistance would be appreciated.

Murray Sobol

Still looking for any assistance with this problem.

L. Fernigrini

You should consider opening a question with the details, since the current question "Forum for Oracle 18c Express Edition" has no relation to the problem you described later and most people may ignore it

Murray Sobol

L. Fernigrini:
See my second post; it has the details. If you need further information let me know.

Murray Sobol

I recently installed Oracle 18c Express Edition on my Windows 10x laptop.
When I tried to run DBCA i got this message:
image.png
When I researched this error one suggestion was to use a TRY/Catch block to trap this error; I shouldn't be trying to change DBCA.
My background is with Oracle 11g on windows servers; I am researching a project to migrate databases to Oracle 19.
I would have preferred to install an Oracle 19 Express edition but apparently that does not exist.
My account with oracle is a "free" one so I don't have access to knowledge base articles.
I am curious - did something change between Oracle 11 and Oracle 18/19 with respect to language support.
Amy assistance would be appreciated.
Did this answer the question? Yes No

L. Fernigrini

I know the details and cannot help since I did not have such a problem.
My suggestion, again, is that you open a NEW question with a title associated to the real problem. This question / thread was started about forum space, has been answered (although you did not select the right answer) and does not really match the problem you have (that has no relation to "Forum for Oracle 18c Express Edition").

User_KC6Y3
User_KC6Y3
"

Murray Sobol

Which forum would you suggest I post this question in??

L. Fernigrini
Answer

This is the correct one (Oracle Database Express Edition (XE)) but the title is completely unrelated to your technical problem, and there are a lot of messages already on the thread so it would make things confusing if you just change the title.

Marked as Answer by Murray Sobol · Jun 2 2021
1 - 9
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 15 2010
Added on Oct 12 2010
2 comments
2,375 views