Java - Imprimir con Nombre de la Impresora

   
Vista:

Imprimir con Nombre de la Impresora

Publicado por EDUAR naibaf05@hotmail.com (1 intervención) el 13/10/2017 15:52:29
Tengo este codigo mediante el cual puedo imprimir un ticket pero me muestra el dialogo para escoger la impresora en la que quiero imprimir y no es lo que necesito, quiero saber si hay una manera de enviarle el nombre de la impresora que deseo imprimir. Gracias

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package Utils.print;
 
import Utils.EndPoints;
import Utils.mysqlTable.MySQLQuery;
import forms.billing.BillBank;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.prefs.Preferences;
import javafx.print.Printer;
import javax.imageio.ImageIO;
import javax.print.PrintService;
import service.BillForPrint;
 
public final class print {
 
    private static final String KEY_PRINTER = "printer";
    private DecimalFormat moneyFormat = new DecimalFormat("$ ###,###,###,##0.000");
    private SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    private EndPoints ep;
    public Font font = new Font("Courier New", Font.BOLD, 9);
    public PrintJob printJob;
    public Graphics page;
    public int x;
    public int y;
    public int weightImg;
    public int widthImg;
    public int increaseX;
    public int increaseY;
 
    public print(EndPoints ep, int x, int y, int weightImg, int widthImg, int increaseX, int increaseY) {
        printJob = Toolkit.getDefaultToolkit().getPrintJob(new Frame(), "SCAT", null);
        this.ep = ep;
        this.x = x;
        this.y = y;
        this.weightImg = weightImg;
        this.widthImg = widthImg;
        this.increaseX = increaseX;
        this.increaseY = increaseY;
    }
 
    public print(EndPoints ep) {//para Tickets con logo
        try {
            printJob = Toolkit.getDefaultToolkit().getPrintJob(new Frame(), "SCAT", prp); // aqui sale el dialogo para escoger impresora
 
 
            this.ep = ep;
            this.x = 10;
            this.y = 0;
            this.weightImg = 80;
            this.widthImg = 160;
            this.increaseX = 0;
            this.increaseY = 10;
 
            page = printJob.getGraphics();
            page.setFont(font);
            page.setColor(Color.black);
 
            BufferedImage img;
            img = ImageIO.read(new File("E:\\mg_logo_nit.png"));
            page.drawImage(img, (x + 20), y, widthImg, weightImg, null);
            y = y + (increaseY * 2) + weightImg;
 
            page.drawString("RECIBO DE CAJA", alignCenter("RECIBO DE CAJA".length()), (y));
            y = y + (increaseY * 2);
        } catch (HeadlessException | IOException e) {
            System.out.println("LA IMPRESION HA SIDO CANCELADA...");
        }
    }
 
    public void printer(Object[][] rows) {
        try {
            page = printJob.getGraphics();
            page.setFont(font);
            page.setColor(Color.black);
 
            BufferedImage img;
            img = ImageIO.read(new File("E:\\mg_logo_nit.png"));
            page.drawImage(img, (x + 20), y, widthImg, weightImg, null);
            y = y + (increaseY * 2) + weightImg;
            int posX = 0;
            int posY = 0;
            for (Object[] row : rows) {
                page.drawString(MySQLQuery.getAsString(row[0]), (x + posX), (y + posY));
                page.drawString(MySQLQuery.getAsString(row[1]), (x + posX + 150), (y + posY));
                posX = posX + increaseX;
                posY = posY + increaseY;
            }
 
            page.dispose();
            printJob.end();
        } catch (Exception e) {
            System.out.println("LA IMPRESION HA SIDO CANCELADA...");
        }
    }
 
    public void printerTicket(BillForPrint bill, BillBank billBank) throws Exception {
 
        Object[] obj = new MySQLQuery("SELECT "
                + "b.bill_num, "
                + "b.creation_date, "
                + "t.doc, "
                + "t.first_name, "
                + "t.last_name, "
                + "CONCAT(IFNULL(t.phones, ''), ' - ', IFNULL(bu.phones, '')), "
                + "CONCAT(bu.address, IF(t.apartment IS NULL, ' ', CONCAT(' Apto ', t.apartment))), "
                + "bu.name, "
                + "t.num_install, "
                + "IF(b.total > 0, 'Total', 'Parcial') "
                + "FROM bill_bill b "
                + "LEFT JOIN bill_client_tank t ON b.client_tank_id = t.id "
                + "LEFT JOIN bill_building bu ON bu.id = t.building_id "
                + "WHERE b.id = " + bill.getBillId()).getRecord(ep, ep.getPoolNameByCity());
 
        String str = new MySQLQuery("SELECT name FROM city WHERE id = " + (Integer) ep.getRestriction()).getAsString(ep);
        obj = (obj != null ? obj : new Object[9]);
 
        page.drawString("No. Factura:", (x), (y));
        page.drawString(checkNull(obj[0]), alignRight(checkNullPadding(obj[0])), getY(0));
 
        page.drawString("Ciudad:", (x), getY(1));
        page.drawString(checkNull(str), alignRight(checkNullPadding(str)), getY(1));
 
        page.drawString("Empleado:", (x), getY(3));
        page.drawString(getNameEmp(), alignRight(getNameEmp().length()), getY(3));
 
        page.drawString("Fecha:", (x), getY(5));
        page.drawString(sf.format(new Date()), alignRight(sf.format(new Date()).length()), getY(5));
 
        page.drawString("Identificación:", (x), getY(7));
        page.drawString(checkNull(obj[2]), alignRight(checkNullPadding(obj[2])), getY(7));
 
        page.drawString("Nombre:", (x), getY(9));
        page.drawString(checkNull(obj[3]).toUpperCase(), alignRight(checkNullPadding(obj[3])), getY(9));
 
        page.drawString("Apellido:", (x), getY(10));
        page.drawString(checkNull(obj[4]).toUpperCase(), alignRight(checkNullPadding(obj[4])), getY(10));
 
        page.drawString("Instalación:", (x), getY(11));
        page.drawString(checkNull(obj[8]), alignRight(checkNullPadding(obj[8])), getY(11));
 
        page.drawString("Tipo de Pago:", (x), getY(13));
        page.drawString(checkNull(obj[9]), alignRight(checkNullPadding(obj[9])), getY(13));
 
        page.drawString("Pagado en:", (x), getY(14));
        page.drawString(checkNull(billBank.name), alignRight(checkNullPadding(billBank.name)), getY(14));
 
        page.drawString("Valor Pagado:", (x), getY(15));
        page.drawString(moneyFormat.format(bill.getTotal()), alignRight(moneyFormat.format(bill.getTotal()).length()), getY(15));
 
        extensiveText(getFooter(), 17);
        page.drawString("---------------------------------------", x, getY(19));
        page.dispose();
        printJob.end();
 
    }
 
    private int alignRight(int length) {
        double max = 210;
        max = (max - (length * 5.3));
        return MySQLQuery.getAsInteger(max);
    }
 
    private int alignCenter(int length) {
        double max = 210 / 2;
        max = (max - ((length / 2) * 5.3));
        return MySQLQuery.getAsInteger(max);
    }
 
    private String checkNull(Object obj) {
        if (obj != null) {
            return MySQLQuery.getAsString(obj);
        }
        return "No Registra";
    }
 
    private int checkNullPadding(Object obj) {
        if (obj != null) {
            return MySQLQuery.getAsString(obj).length();
        }
        return 11;
    }
 
    private String getNameEmp() {
        return ep.getEmployee().firstName + " " + ep.getEmployee().lastName;
    }
 
    private String getFooter() {
        return "VIGILADO POR LA SUPERINTENDENCIA DE SERVICIOS PUBLICOS DOMICILIARIOS";
    }
 
    private void extensiveText(String text, int line) {
        if (text.length() <= 37) {
            page.drawString(text, alignCenter(text.length()), (y + increaseY * line));
        } else {
            String[] objs = text.split(" ");
            String txt = objs[0];
            for (int i = 1; i < objs.length; i++) {
                if ((txt + " " + objs[i]).length() > 37 || i == (objs.length - 1)) {
                    if (i == (objs.length - 1)) {
                        txt = txt + " " + objs[i];
                        page.drawString(txt, alignCenter(txt.length()), (y + increaseY * line));
                    } else {
                        page.drawString(txt, alignCenter(txt.length()), (y + increaseY * line));
                        y = y + 10;
                        txt = objs[i];
                    }
                } else {
                    txt = txt + " " + objs[i];
                }
            }
        }
    }
 
    private int getY(int line) {
        return (y + increaseY * line);
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Revisar política de publicidad