Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4544064
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:37:08+00:00 2026-05-21T15:37:08+00:00

I have an application which talks to USB printer and prints out the receipt

  • 0

I have an application which talks to USB printer and prints out the receipt once a sale is completed. I have no problem with connecting to the printer and printing something. THe problem i have now is when i do some printing i could see only a part of my message gets printed on the receipt. I have attached the code i am using. Please help me to see the full printed receipt 🙂

–CODE–

package utilities;
import javax.swing.JFrame;   
import javax.swing.JOptionPane;    
import java.awt.print.PrinterJob;    
import java.awt.print.PageFormat;  
import java.awt.print.Printable;    
import java.awt.print.PrinterException;  
import java.awt.Color;  
import java.awt.Graphics;  
import java.awt.Graphics2D;      
public class ReceiptPrinter implements Printable {  
    private JFrame printFrame;  
    private String waitMsg;  
    private javax.swing.JTextArea jTextArea1;`  

    /** Inner class for the actual printed object */
    class PrintFrame extends JFrame {

        PrintFrame(String msg) {
            setBackground(new Color(255, 255, 255, 0));

            jTextArea1 = new javax.swing.JTextArea();

            jTextArea1.setColumns(80);

            jTextArea1.setLineWrap(true);

            jTextArea1.setRows(5);

            jTextArea1.setWrapStyleWord(true);

            jTextArea1.setText(msg);

            add(jTextArea1);
            pack();
            setVisible(true);
        }
    }

    /** Creates a new instance of ReceiptPrinter with a default wait message */
    public ReceiptPrinter() {
        waitMsg = "Wait for the printer to finish\nClick the OK button when done";
    }

    /**
     * Creates a new instance of ReceiptPrinter with a wait message.
     *
     * @param   msg     the wait message
     */
    public ReceiptPrinter(String msg) {
        waitMsg = msg;
    }

    /**
     * Sends the actual message to the receipt printer - does not wait.
     *
     * @param   msg     the actual message to be printed
     */
    public void printIt(String msg) {
        printIt(msg, false);
    }

    /**
     * Sends the actual message to the receipt printer.
     *
     * @param   msg     the actual message to be printed
     * @param   wait    show JOptionPane to wait for print to finish
     */
    public void printIt(String msg, boolean wait) {
        printFrame = new PrintFrame(msg);

        PrinterJob job = PrinterJob.getPrinterJob();
        PageFormat format = job.defaultPage();
        format.setOrientation(PageFormat.PORTRAIT);
        //double width = format.getWidth();
        System.out.println(format.getImageableX()+","+format.getImageableY());

        job.setPrintable(this, format);

        try {
            job.print();
            if (wait) {
                JOptionPane.showMessageDialog(null, waitMsg, "Information",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (PrinterException e) {
            e.printStackTrace();
        }
        //printFrame.dispose();
    }

    /**
     * Print method required by Printable interface.
     *
     * @param   g       the graphics context
     * @param   format  the page format
     * @param   pagenum the page number requested to print
     * @return  int     flag indicating page existance
     */

    public int print(Graphics g, PageFormat pf, int pagenum) {
        if (pagenum > 0) {
            return Printable.NO_SUCH_PAGE;
        }
        //g.translate(0, 150);
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

        /* Print the entire visible contents of a java.awt.Frame */
        printFrame.printAll(g2d);
        //g.translate((int) format.getImageableX(),
          //      (int) format.getImageableY());
        //printFrame.paint(g);
        return Printable.PAGE_EXISTS;
    }

}

And the main function where i call this class is

package utilities;
import forms_helper.global_variables;  
import java.awt.Dimension;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;
import javax.swing.JOptionPane;
public class PrintTest extends javax.swing.JFrame {  
    private ReceiptPrinter receiptPrinter = new ReceiptPrinter();  
    private FileOutputStream fos;  
    public PrintTest() {  
        initComponents();  
        setPreferredSize(new Dimension(300, 200));  
        pack();  
        try {  
            fos = new FileOutputStream("USB002");    
        }  
        catch (FileNotFoundException e) {  
           JOptionPane.showMessageDialog(this, "Cannot open file\n" + e.getMessage(),  
                   "Warning", JOptionPane.WARNING_MESSAGE);  
        }  
    }`  

    private void initComponents() {
        openButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        display = new javax.swing.JTextArea();
        exitButton = new javax.swing.JButton();

        getContentPane().setLayout(null);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        openButton.setFont(new java.awt.Font("Tahoma", 1, 11));
        openButton.setText("Open");
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });

        getContentPane().add(openButton);
        openButton.setBounds(151, 120, 80, 23);

        display.setColumns(20);
        display.setRows(5);
        jScrollPane1.setViewportView(display);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(10, 10, 220, 92);

        exitButton.setFont(new java.awt.Font("Tahoma", 1, 11));
        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        getContentPane().add(exitButton);
        exitButton.setBounds(10, 120, 70, 23);

        pack();
    }

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            fos.close();
        }
        catch (IOException e) {
            JOptionPane.showMessageDialog(this, "Unable to close printer port",
                    "Warning", JOptionPane.WARNING_MESSAGE);
        }
        dispose();
    }

    private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // this section attempts to send the BEL character to the printer port
        byte bel = 0x07;
        try {
            fos.write(bel);
            fos.flush();
        }
        catch (IOException e) {
            JOptionPane.showMessageDialog(this, "Error trying to write\n" + e.getMessage(),
                    "Warning", JOptionPane.WARNING_MESSAGE);
        }
    // this section appends the BEL to  the printed message and sends it to the Windows printer
        //String msg = "test \n test";
        String msg=global_variables.msg;
        msg += ((char) 0x07);

        receiptPrinter.printIt(msg);
    // this section displays a hex dump of the printed message
    // note that the BEL is being converted to a box and that
    // is what actually prints on the printer instead of the beep
        for (int i = 0; i < msg.length(); i += 5) {
            for (int j = 0; j < 5; j++) {
                if ((i + j) < msg.length()) {
                    int x = msg.charAt(i + j);
                    display.append(String.format("%02x ", x));
                }
            }
            display.append("   ");
            for (int j = 0; j < 5; j++) {
                if (i + j < msg.length()) {
                    char c = msg.charAt(i + j);
                    display.append(String.format(" %c", c));
                }
            }
            display.append("\n");
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PrintTest().setVisible(true);
            }
        });
    }

    private javax.swing.JTextArea display;
    private javax.swing.JButton exitButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton openButton;

}

When i print it i am getting the image like this
enter image description here

Actual content was something like this
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:37:09+00:00Added an answer on May 21, 2026 at 3:37 pm

    Can you tell me more?

    Compare the rectangle returned by g.getClipBounds() to the getImageableWidth() and getImageableHeight() found in the PageFormat. It looks like your image it getting clipped to the printers default Paper size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET application which talks to a third-party SOAP web service. My
I have an application which really should be installed, but does work fine when
I have an application which extracts data from an XML file using XPath. If
We have an application which needs to use Direct3D. Specifically, it needs at least
I have an application which behaves as a slideshow for all pictures in a
I have an application which is a portal application and I want to allow
I have an application which takes a string value of the form %programfiles%\directory\tool.exe from
I have an application which get copied and run on client machines. The program
I have an application which has to live as a service, I create an
I have one application which uses the standard .NET forms authentication cookie, now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.