打印不会填充A4纸

时间:2017-04-24 09:18:18

标签: java printing

我的代码存在一些问题: 1)打印件没有填充A4Paper(见下图) 2)我打印的区域的背景,在纸上是灰色的,我该如何解决这个问题?

基本上我的代码会打印JFrame及其组件。

我的源代码:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;


import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import javax.swing.SwingConstants;

public class Stampa {

    private JFrame frame;
    double qt, num, sconto, importo;
    /**
     * Launch the application.
     */
    public static void stampa() {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Stampa window = new Stampa();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Stampa() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 12));
        frame.setBounds(100, 100, 794, 1123);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate localDate = LocalDate.now();
        System.out.println(dtf.format(localDate)); 

        JLabel lblSpettle = new JLabel("SPETT.LE");
        lblSpettle.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblSpettle.setBounds(442, 116, 68, 14);
        frame.getContentPane().add(lblSpettle);

        JLabel lblNewLabel = new JLabel(Main.textField_14.getText()+"  "+Main.textField_15.getText());

        lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblNewLabel.setBounds(442, 141, 250, 14);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel(Main.textField_18.getText());
        lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblNewLabel_1.setBounds(442, 184, 250, 14);
        frame.getContentPane().add(lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel(Main.textField_19.getText()+"  "+Main.textField_17.getText());
        lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblNewLabel_2.setBounds(442, 223, 206, 14);
        frame.getContentPane().add(lblNewLabel_2);

        JLabel lblNewLabel_3 = new JLabel(Main.textField_20.getText());
        lblNewLabel_3.setBounds(714, 224, 43, 14);
        frame.getContentPane().add(lblNewLabel_3);

        JLabel lblNewLabel_4 = new JLabel("Codice Cliente:        "+Main.codw);
        lblNewLabel_4.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblNewLabel_4.setBounds(29, 165, 250, 14);
        frame.getContentPane().add(lblNewLabel_4);

        JLabel label = new JLabel("Milano,                        "+dtf.format(localDate));
        label.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label.setBounds(29, 224, 250, 14);
        frame.getContentPane().add(label);

        JLabel label_1 = new JLabel("Partita Iva:      "+Main.textField_16.getText());
        label_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_1.setBounds(29, 202, 250, 14);
        frame.getContentPane().add(label_1);

        JLabel lblNewLabel_5 = new JLabel("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        lblNewLabel_5.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblNewLabel_5.setBounds(29, 336, 728, 14);
        frame.getContentPane().add(lblNewLabel_5);

        JLabel label_2 = new JLabel("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        label_2.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_2.setBounds(29, 381, 728, 14);
        frame.getContentPane().add(label_2);

        JLabel lblNewLabel_6 = new JLabel("D E S C R I Z I O N E");
        lblNewLabel_6.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblNewLabel_6.setBounds(55, 357, 122, 24);
        frame.getContentPane().add(lblNewLabel_6);

        JLabel lblQta = new JLabel("Q.TA");
        lblQta.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblQta.setBounds(244, 357, 43, 24);
        frame.getContentPane().add(lblQta);

        JLabel lblPrezzo = new JLabel("PREZZO");
        lblPrezzo.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblPrezzo.setBounds(345, 357, 49, 24);
        frame.getContentPane().add(lblPrezzo);

        JLabel lblSconto = new JLabel("SCONTO");
        lblSconto.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblSconto.setBounds(457, 357, 53, 24);
        frame.getContentPane().add(lblSconto);

        JLabel lblImporto = new JLabel("IMPORTO");
        lblImporto.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblImporto.setBounds(589, 357, 59, 24);
        frame.getContentPane().add(lblImporto);

        JLabel lblIva = new JLabel("IVA");
        lblIva.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblIva.setBounds(714, 357, 27, 24);
        frame.getContentPane().add(lblIva);

        JLabel label_4 = new JLabel("Testata:            "+Main.textField_21.getText());
        label_4.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_4.setBounds(29, 473, 250, 14);
        frame.getContentPane().add(label_4);

        JLabel label_5 = new JLabel(Main.textField_22.getText()+"               "+Main.textField_28.getText());
        label_5.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_5.setBounds(29, 498, 250, 14);
        frame.getContentPane().add(label_5);

        JLabel label_6 = new JLabel("Soggetto:           "+Main.textField_26.getText());
        label_6.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_6.setBounds(29, 523, 250, 14);
        frame.getContentPane().add(label_6);

        JLabel label_7 = new JLabel("Cliente:");
        label_7.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_7.setBounds(29, 548, 250, 14);
        frame.getContentPane().add(label_7);

        JLabel label_8 = new JLabel("Uscita:             "+"N.:        "+Main.textField_24.getText()+"   del   "+Main.textField_25.getText());
        label_8.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_8.setBounds(29, 573, 333, 14);
        frame.getContentPane().add(label_8);

        JLabel lblD = new JLabel(Main.textField_23.getText());
        lblD.setBounds(252, 423, 27, 14);
        frame.getContentPane().add(lblD);
        qt = Double.parseDouble(lblD.getText());

        JLabel lblD_1 = new JLabel(Main.textField_30.getText());
        lblD_1.setBounds(341, 423, 74, 14);
        frame.getContentPane().add(lblD_1);

        num = Double.parseDouble(lblD_1.getText());

        JLabel lblD_2 = new JLabel(Main.textField_31.getText());
        lblD_2.setBounds(450, 423, 80, 14);
        frame.getContentPane().add(lblD_2);

        sconto = Double.parseDouble(lblD_2.getText());

        JLabel label_12 = new JLabel(Main.textField_32.getText());
        label_12.setBounds(698, 423, 43, 14);
        frame.getContentPane().add(label_12);

        importo = (qt*num)-((num*sconto)/100);
        JLabel label_3 = new JLabel(String.valueOf(importo));
        label_3.setBounds(589, 423, 74, 14);
        frame.getContentPane().add(label_3);

        JLabel label_9 = new JLabel("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        label_9.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_9.setBounds(29, 615, 728, 14);
        frame.getContentPane().add(label_9);

        JLabel lblImponibile = new JLabel("IMPONIBILE");
        lblImponibile.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblImponibile.setBounds(86, 636, 122, 24);
        frame.getContentPane().add(lblImponibile);

        JLabel label_11 = new JLabel("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        label_11.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_11.setBounds(29, 660, 728, 14);
        frame.getContentPane().add(label_11);

        JLabel lblAlIva = new JLabel("Al. Iva");
        lblAlIva.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblAlIva.setBounds(244, 636, 43, 24);
        frame.getContentPane().add(lblAlIva);

        JLabel lblIva_1 = new JLabel("I.V.A.");
        lblIva_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblIva_1.setBounds(345, 636, 49, 24);
        frame.getContentPane().add(lblIva_1);

        JLabel lblTotaleFattura = new JLabel("Totale Fattura");
        lblTotaleFattura.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblTotaleFattura.setBounds(589, 636, 152, 24);
        frame.getContentPane().add(lblTotaleFattura);

        JLabel label_10 = new JLabel(Main.textField_30.getText());
        label_10.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_10.setBounds(87, 685, 74, 14);
        frame.getContentPane().add(label_10);

        JLabel label_13 = new JLabel(Main.textField_32.getText());
        label_13.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_13.setHorizontalAlignment(SwingConstants.CENTER);
        label_13.setBounds(228, 685, 74, 14);
        frame.getContentPane().add(label_13);

        double iva = (num*Double.parseDouble(Main.textField_32.getText()))/100;
        JLabel label_14 = new JLabel(String.valueOf(iva));
        label_14.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_14.setBounds(345, 685, 59, 14);
        frame.getContentPane().add(label_14);

        JLabel label_15 = new JLabel("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        label_15.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_15.setBounds(29, 737, 728, 14);
        frame.getContentPane().add(label_15);

        JLabel label_16 = new JLabel(Main.textField_30.getText());
        label_16.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_16.setBounds(86, 754, 74, 14);
        frame.getContentPane().add(label_16);


        JLabel label_17 = new JLabel(String.valueOf(iva));
        label_17.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_17.setBounds(345, 754, 59, 14);
        frame.getContentPane().add(label_17);

        double ivaa = importo+iva;
        JLabel label_18 = new JLabel(String.valueOf(ivaa));
        label_18.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_18.setBounds(633, 754, 59, 14);
        frame.getContentPane().add(label_18);

        JLabel label_19 = new JLabel(Main.textField_29.getText());
        label_19.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_19.setBounds(469, 754, 87, 14);
        frame.getContentPane().add(label_19);

        JLabel label_20 = new JLabel("Appoggio:          BANCO BPM");
        label_20.setFont(new Font("Tahoma", Font.PLAIN, 12));
        label_20.setBounds(29, 779, 333, 14);
        frame.getContentPane().add(label_20);

        JLabel lblAbicab = new JLabel("Abi/Cab:   0503434071");
        lblAbicab.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblAbicab.setBounds(507, 779, 239, 14);
        frame.getContentPane().add(lblAbicab);

        JLabel lblBancarioCc = new JLabel("bancario                  c/c:     000000002741      CIN:   X");
        lblBancarioCc.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblBancarioCc.setBounds(29, 799, 365, 14);
        frame.getContentPane().add(lblBancarioCc);

        JLabel lblScadenze = new JLabel("Scadenze:                                                                                               "+dtf.format(localDate)+"                       "+ivaa);
        lblScadenze.setFont(new Font("Tahoma", Font.PLAIN, 12));
        lblScadenze.setBounds(29, 820, 712, 14);
        frame.getContentPane().add(lblScadenze);

        JLabel lblNewLabel_7 = new JLabel("Articolo");
        lblNewLabel_7.setBounds(404, 640, 46, 14);
        frame.getContentPane().add(lblNewLabel_7);

        JLabel lblNewLabel_8 = new JLabel(Main.tipoiva);
        lblNewLabel_8.setBounds(404, 685, 182, 14);
        frame.getContentPane().add(lblNewLabel_8);

        frame.setVisible(true);

        PrinterJob pjob = PrinterJob.getPrinterJob();
        PageFormat preformat = pjob.defaultPage();
        preformat.setOrientation(PageFormat.PORTRAIT);
        PageFormat postformat = pjob.pageDialog(preformat);

        //If user does not hit cancel then print.
        if (preformat != postformat) {
            //Set print component
            pjob.setPrintable(new Printer(frame), postformat);
            if (pjob.printDialog()) {
                try {
                    pjob.print();

                } catch (PrinterException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }


    }
    public static class Printer implements Printable {
        final Component comp;

        public Printer(Component comp){
            this.comp = comp;
        }
        @Override
        public int print(java.awt.Graphics g, PageFormat format, int page_index) throws PrinterException {
            if (page_index > 0) {
                return Printable.NO_SUCH_PAGE;
            }

            // get the bounds of the component
            Dimension dim = comp.getSize();
            double cHeight = dim.getHeight();
            double cWidth = dim.getWidth();

            // get the bounds of the printable area
            double pHeight = format.getImageableHeight();
            double pWidth = format.getImageableWidth();

            double pXStart = format.getImageableX();
            double pYStart = format.getImageableY();

            double xRatio = pWidth / cWidth;
            double yRatio = pHeight / cHeight;


            Graphics2D g2 = (Graphics2D) g;
            g2.translate(pXStart, pYStart);
            g2.scale(xRatio, yRatio);
            comp.paint(g2);

            return Printable.PAGE_EXISTS;
        }


    }
}

此代码的打印输出是: image

正如您所看到的那样,它并没有填满页面,它给边界带来了巨大的差距。

提前谢谢!

0 个答案:

没有答案