Aung Thet
Aung Thet

Reputation: 3640

How to remove margin in printed page

I am facing a problem in printing panel . In my printed page, there is unwanted margin at both side of content and the content area is too small to contain all text in that panel. so... my result page is.......My Picture

My code is here ......

package UI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.SimpleDateFormat;
import java.util.Date;
 import java.util.Dictionary;
 import javax.print.PrintService;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

public class printService  extends JFrame implements Printable {
JFrame f = new JFrame("Printing");
JPanel p3 = new JPanel(new GridBagLayout());
JPanel main = new JPanel();

JLabel title = new JLabel("Ni Lar Aung");
JLabel subtitle = new JLabel("Mobile Sale & Service");
JLabel lblNo = new JLabel("No");
JLabel lblType = new JLabel("Type");
JLabel lblAmt = new JLabel("Service Amt");
JLabel lblDes = new JLabel("Description");
JLabel lblTot = new JLabel("Total");

public printService(DefaultTableModel tm, int rowcount,int dis ) {
    Container c=getContentPane();
    c.setLayout(new BorderLayout());
    p3.setLayout(new GridBagLayout());

    p3.setBackground(Color.white);

    GridBagConstraints con = new GridBagConstraints();
    con.anchor = GridBagConstraints.NORTH;
    con.insets = new Insets(5, 5, 5, 5);
    con.fill = GridBagConstraints.NONE;



    con.gridx = 1;
    con.gridy = 0;
    con.gridwidth = 2;
    title.setFont(new Font("Times New Roman", Font.BOLD,13));
    p3.add(title, con);

    con.gridx = 1;
    con.gridy = 1;
    con.gridwidth = 2;
    subtitle.setFont(new Font("Times New Roman", Font.BOLD,11));
    p3.add(subtitle, con);

    con.gridx = 3;
    con.gridy = 2;
    con.gridwidth = 2;
    Date d = new Date();
    SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
    JLabel lblDate = new JLabel(simple.format(d));
    lblDate.setFont(new Font("Times New Roman", Font.PLAIN,9));
    p3.add(lblDate, con);

    con.gridx = 0;
    con.gridy = 3;
    con.weightx = 0.5;
    con.gridwidth =1;
    lblNo.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(lblNo, con);

    con.gridx = 1;
    con.gridy = 3;
    con.weightx = 0.5;
    con.gridwidth =1;
    lblType.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(lblType, con);

    con.gridx = 2;
    con.gridy = 3;
    con.weightx = 0.5;
    con.gridwidth =1;
    lblAmt.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(lblAmt, con);

    /*con.gridx = 3;
    con.gridy = 3;
    con.weightx = 0.5;
    con.gridwidth =1;
    lblDes.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(lblDes, con);*/

    con.gridx = 3;
    con.gridy = 3;
    con.weightx = 0.5;
    con.gridwidth =1;
    p3.add(lblTot, con);
    lblTot.setFont(new Font("Times New Roman", Font.PLAIN,8));
    int totalcost = 0;
    int row = 4;
    int quantity = 1;
    int price = 1000;
    int discout = dis;
    String stock = "stockName";
    System.out.println(rowcount+"rowcount");
    System.out.println(tm.getRowCount());
    System.out.println(tm.getColumnCount());
    //System.out.println(Service.dt.getValueAt(0, 0)+"hhhh");
    for (int i = 0; i < rowcount; i++) {
        con.gridx = 0;
        con.gridy = row;
        con.weightx = 0.5;
        con.gridwidth =1;
        JLabel lblNo = new JLabel(i + "");
        lblNo.setFont(new Font("Times New Roman", Font.PLAIN,8));
        p3.add(lblNo, con);

        con.gridx = 1;
        con.gridy = row;
        con.weightx = 0.5;
        con.gridwidth =1;
        JLabel val1 = new JLabel(tm.getValueAt(i, 1).toString());
        val1.setFont(new Font("Times New Roman", Font.PLAIN,8));
        p3.add(val1, con);
        System.out.println(tm.getValueAt(i, 1)+"llll");


        con.gridx = 2;
        con.gridy = row;
        con.weightx = 0.5;
        con.gridwidth =1;
        JLabel val2 = new JLabel(tm.getValueAt(i, 2).toString());
        val2.setFont(new Font("Times New Roman", Font.PLAIN,8));
        p3.add(val2, con);
        System.out.println(tm.getValueAt(i, 2));

        /*con.gridx = 3;
        con.gridy = row;
        con.weightx = 0.5;
        con.gridwidth =1;
        JLabel val3 = new JLabel(tm.getValueAt(i, 3).toString());
        val3.setFont(new Font("Times New Roman", Font.PLAIN,8));
        p3.add(val3, con);*/

        con.gridx = 3;
        con.gridy = row;
        con.weightx = 0.5;
        con.gridwidth =1;
        JLabel val4 = new JLabel(tm.getValueAt(i, 4).toString());
        val4.setFont(new Font("Times New Roman", Font.PLAIN,8));
        p3.add(val4, con);

        totalcost += Integer.parseInt(tm.getValueAt(i, 4).toString());
        row++;
    }

    con.gridy = row;
    con.gridx = 3;
    p3.add(new JLabel("-------"), con);
    row++;

    con.gridy = row;
    con.gridx = 1;
    JLabel totAmt = new JLabel("Total Amount");
    totAmt.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(totAmt, con);

    con.gridy = row;
    con.gridx = 3;
    JLabel total = new JLabel(totalcost + "");
    total.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(total, con);
    row++;

    con.gridy = row;
    con.gridx = 1;
    JLabel lblDisAmt = new JLabel("Discount Allow");
    lblDisAmt.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(lblDisAmt, con);

    con.gridy = row;
    con.gridx = 3;
    JLabel Dis = new JLabel(discout + ""); 
    Dis.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(Dis, con);
    row++;

    con.gridy = row;
    con.gridx = 3;
    p3.add(new JLabel("-------"), con);
    row++;

    con.gridy = row;
    con.gridx = 1;
    JLabel netAmt = new JLabel("Net Amount");
    netAmt.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(netAmt, con);

    con.gridy = row;
    con.gridx = 3;
    JLabel result = new JLabel((totalcost - discout) + ""); 
    result.setFont(new Font("Times New Roman", Font.PLAIN,8));
    p3.add(result, con);
    row++;

    con.gridy = row;
    con.gridx = 1;
    con.gridwidth = 3;
    JLabel thanks = new JLabel("Thank You");
    thanks.setFont(new Font("Times New Roman", Font.BOLD, 9));
    p3.add(thanks,con);


    final JButton but=new JButton("print");
    con.gridy = row;
    con.gridx = 4;
    p3.add(but, con);

    row++;
    c.setSize(300, 400);
    c.setVisible(true);
    setVisible(true);
    setSize(300,200);
    c.add(main);
    main.setVisible(true);



    GridBagConstraints m = new GridBagConstraints();
    m.anchor = GridBagConstraints.NORTH;
    m.insets = new Insets(5,5,5,5);
    m.fill = GridBagConstraints.BOTH;

    m.gridy = 0;
    m.gridx = 0;
    main.add(p3,m);
    main.setBackground(Color.yellow);
    p3.setVisible(true);
but.setVisible(false);
/*  but.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    but.setVisible(false);
    printForm();
  }

      });*/
    printForm();
}

   public void printForm() {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
PrintUtility.getPrinterServiceNameList();
PrintService service = PrintUtility
        .findPrintService("XP-80C");
System.out.println("default printer selection");

try {
    job.printDialog();
    System.out.println("within try block");
    job.setPrintService(service);
    job.print();
} catch (PrinterException ex) {
    System.out.println("within catch");
  }
   }


@Override
public int print(Graphics g, PageFormat pf, int page)
        throws PrinterException {
    // TODO Auto-generated method stub
    System.out.println("ddddddddd");
    if (page > 0)
        return NO_SUCH_PAGE;
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    p3.printAll(g);
    return PAGE_EXISTS;

}

}

Upvotes: 0

Views: 826

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

Try setting the size of p3 to meet the page size of the PageFormat when the print method is called.

 Rectangle bounds = p3.getBounds();
 p3.setBounds(0, 0, (int)pf.getWidth(), (int)pf.getHeight());
 p3.printAll(g);
 p3.setBounds(bounds);

Upvotes: 2

Related Questions