Invoice/bill receipt create and print using graphics 2d and java array list

This post about invoice/bill receipt printing system. Following this tutorial, you can create customize invoice for your any kind of system. Here I use graphics 2d to create this invoice. You can follow my video tutorials to get a complete idea about this method and here include all source code regarding this tutorial. In this part show you how to create the sales invoice using graphics 2d and java array list.

Do you need help to remotely set up my any project on your machine or customize any project with your requirement please contact

This bill receipt can divide three parts. First one is heading part here can include shop name contact details, logo and more details. This part is a static part.

The second part is the body part. Here include sales items details. Those details are dynamic details. Because those data can different bill to bill. Here I used an array list to store sales item data in this system. We can attach this part with the database when developing an advance system.

The third part is the footer part. Here include some simple details as bill end and software developer and his contact details. also, this part is static.

The source codes Of this project

package posinvoice;

 * @author ccs

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import static java.awt.print.Printable.NO_SUCH_PAGE;
import static java.awt.print.Printable.PAGE_EXISTS;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Home extends javax.swing.JFrame {

     * Creates new form Home
  //  String billNo="";
    Double totalAmount=0.0;
    Double cash=0.0;
    Double balance=0.0;
    Double bHeight=0.0;
    ArrayList<String> itemName = new ArrayList<>();
    ArrayList<String> quantity = new ArrayList<>();
    ArrayList<String> itemPrice = new ArrayList<>();
    ArrayList<String> subtotal = new ArrayList<>();
    public Home() {


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        totalAmount = totalAmount+ Double.valueOf(txtsubtotal.getText());

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
         bHeight = Double.valueOf(itemName.size());
        //JOptionPane.showMessageDialog(rootPane, bHeight);
        PrinterJob pj = PrinterJob.getPrinterJob();        
        pj.setPrintable(new BillPrintable(),getPageFormat(pj));
        try {
         catch (PrinterException ex) {

    private void clear()
    public PageFormat getPageFormat(PrinterJob pj)
    PageFormat pf = pj.defaultPage();
    Paper paper = pf.getPaper();    

    double bodyHeight = bHeight;  
    double headerHeight = 5.0;                  
    double footerHeight = 5.0;        
    double width = cm_to_pp(8); 
    double height = cm_to_pp(headerHeight+bodyHeight+footerHeight); 
    paper.setSize(width, height);
    paper.setImageableArea(0,10,width,height - cm_to_pp(1));  

    return pf;
    protected static double cm_to_pp(double cm)
	        return toPPI(cm * 0.393600787);            
protected static double toPPI(double inch)
	        return inch * 72d;            

public class BillPrintable implements Printable {
  public int print(Graphics graphics, PageFormat pageFormat,int pageIndex) 
  throws PrinterException 
      int r= itemName.size();
      ImageIcon icon=new ImageIcon("C:UsersccsDocumentsNetBeansProjectsvideo TestPOSInvoicesrcposinvoicemylogo.jpg"); 
      int result = NO_SUCH_PAGE;    
        if (pageIndex == 0) {                    
            Graphics2D g2d = (Graphics2D) graphics;                    
            double width = pageFormat.getImageableWidth();                               
            g2d.translate((int) pageFormat.getImageableX(),(int) pageFormat.getImageableY()); 

          //  FontMetrics metrics=g2d.getFontMetrics(new Font("Arial",Font.BOLD,7));
            int y=20;
            int yShift = 10;
            int headerRectHeight=15;
           // int headerRectHeighta=40;
            g2d.setFont(new Font("Monospaced",Font.PLAIN,9));
            g2d.drawImage(icon.getImage(), 50, 20, 90, 30, rootPane);y+=yShift+30;
            g2d.drawString("        ",12,y);y+=yShift;
            g2d.drawString("   No 00000 Address Line One ",12,y);y+=yShift;
            g2d.drawString("   Address Line 02 SRI LANKA ",12,y);y+=yShift;
            g2d.drawString(" ",12,y);y+=yShift;
            g2d.drawString("        +94700000000      ",12,y);y+=yShift;

            g2d.drawString(" Item Name                  Price   ",10,y);y+=yShift;
            for(int s=0; s<r; s++)
            g2d.drawString(" "+itemName.get(s)+"                            ",10,y);y+=yShift;
            g2d.drawString("      "+quantity.get(s)+" * "+itemPrice.get(s),10,y); g2d.drawString(subtotal.get(s),160,y);y+=yShift;

            g2d.drawString(" Total amount:               "+txttotalAmount.getText()+"   ",10,y);y+=yShift;
            g2d.drawString(" Cash      :                 "+txtcash.getText()+"   ",10,y);y+=yShift;
            g2d.drawString(" Balance   :                 "+txtbalance.getText()+"   ",10,y);y+=yShift;
            g2d.drawString("       THANK YOU COME AGAIN            ",10,y);y+=yShift;
            g2d.drawString("       SOFTWARE BY:CODEGUID          ",10,y);y+=yShift;
            g2d.drawString("   CONTACT:       ",10,y);y+=yShift;       

    catch(Exception e){

              result = PAGE_EXISTS;    
          return result;    

Don’t Forget Subscribe Us

Be the first to comment

Leave a Reply

Your email address will not be published.