Java - problema con itext

 
Vista:

problema con itext

Publicado por Maximiliano (2 intervenciones) el 11/05/2020 17:38:35
Hola, amigos programadores. Tengo un problema al momento de la creacion de un pdf. es muy extraño. No tengo ningun error, al ejecutar el programa desde netbeans no tengo ningun problema. pero cuando lo abro desde el archivo que creo el clean and build y quiero crear el pdf no cierra el documento y cuando intento abrir el pdf me dice que esta en uso. aqui mando el codigo empleado. Si alguien me puede ayudar se lo agradezco


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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
Document documento = new Document();
 
  try {
 
        int mes = Integer.parseInt(JOptionPane.showInputDialog("Seleccione el Mes\n\n"
                + "0 - Todos los meses\n"
                + "1 - Enero\n"
                + "2 - Febrero\n"
                + "3 - Marzo\n"
                + "4 - Abril\n"
                + "5 - Mayo\n"
                + "6 - Junio\n"
                + "7 - Julio\n"
                + "8 - Agosto\n"
                + "9 - Septiembre\n"
                + "10 - Octubre\n"
                + "11 - Noviembre\n"
                + "12 - Diciembre\n"));
        int anio = Integer.parseInt(JOptionPane.showInputDialog("Seleccione el Año\n\n"
                + "0 - Para todos los años\n"
                + "Año numero: \n"));
 
        String ruta = System.getProperty("user.home");
        PdfWriter.getInstance(documento, new FileOutputStream(ruta + "/Desktop/" + "Registro de Ingresos "
                + Fechas.Fecha() + " Mes " + mes + " Año " + anio + ".pdf"));
        com.itextpdf.text.Image header = com.itextpdf.text.Image.getInstance("src/imagenes/Cabecera.png");
        header.scaleToFit(400, 200);
        header.setAlignment(Chunk.ALIGN_CENTER);
 
        documento.open();
        documento.add(header);
 
        PdfPTable tabla__Ingresos = new PdfPTable(6);
        PdfPCell celda0 = new PdfPCell(new Paragraph("Informacion de los Ingresos",
                                                                FontFactory.getFont("Tahoma", 10, Font.BOLD, BaseColor.BLUE)));
        celda0.setHorizontalAlignment(Element.ALIGN_CENTER);
        celda0.setPadding(12.0f);
        celda0.setColspan(6);
        celda0.setBackgroundColor(BaseColor.CYAN);
        tabla__Ingresos.addCell(celda0);
 
        PdfPCell columna1 = new PdfPCell(new Paragraph("ID", FontFactory.getFont("Tahoma", 6, Font.BOLD,
                                                                  BaseColor.BLACK)));
        columna1.setHorizontalAlignment(Element.ALIGN_CENTER);
        columna1.setBackgroundColor(BaseColor.ORANGE);
        tabla__Ingresos.addCell(columna1);
 
        PdfPCell columna2 = new PdfPCell(new Paragraph("Dinero", FontFactory.getFont("Tahoma", 6,
                                                                    Font.BOLD, BaseColor.BLACK)));
        columna2.setHorizontalAlignment(Element.ALIGN_CENTER);
        columna2.setBackgroundColor(BaseColor.ORANGE);
        tabla__Ingresos.addCell(columna2);
 
        PdfPCell columna3 = new PdfPCell(new Paragraph("Fecha", FontFactory.getFont("Tahoma", 6,
                                                                    Font.BOLD, BaseColor.BLACK)));
        columna3.setHorizontalAlignment(Element.ALIGN_CENTER);
        columna3.setBackgroundColor(BaseColor.ORANGE);
        tabla__Ingresos.addCell(columna3);
 
        PdfPCell columna4 = new PdfPCell(new Paragraph("Concepto", FontFactory.getFont("Tahoma", 6,
                                                                     Font.BOLD, BaseColor.BLACK)));
        columna4.setHorizontalAlignment(Element.ALIGN_CENTER);
        columna4.setBackgroundColor(BaseColor.ORANGE);
        tabla__Ingresos.addCell(columna4);
 
        PdfPCell columna5 = new PdfPCell(new Paragraph("Ingresado por", FontFactory.getFont("Tahoma", 6,
                                                                         Font.BOLD, BaseColor.BLACK)));
        columna5.setHorizontalAlignment(Element.ALIGN_CENTER);
        columna5.setBackgroundColor(BaseColor.ORANGE);
        tabla__Ingresos.addCell(columna5);
 
        PdfPCell columna6 = new PdfPCell(new Paragraph("Modificado por", FontFactory.getFont("Tahoma", 6,
                                                                    Font.BOLD, BaseColor.BLACK)));
        columna6.setHorizontalAlignment(Element.ALIGN_CENTER);
        columna6.setBackgroundColor(BaseColor.ORANGE);
        tabla__Ingresos.addCell(columna6);
 
        PdfPTable tabla__Total = new PdfPTable(1);
        tabla__Total.setWidthPercentage(20f);
        tabla__Total.setHorizontalAlignment(Element.ALIGN_CENTER);
        PdfPCell celdaTotal = new PdfPCell(new Paragraph("Total", FontFactory.getFont("Tahoma", 6, Font.BOLD,
                                                                     BaseColor.BLUE)));
        celdaTotal.setHorizontalAlignment(Element.ALIGN_CENTER);
        celdaTotal.setBackgroundColor(BaseColor.CYAN);
        tabla__Total.addCell(celdaTotal);
 
        if (mes == 0 && anio == 0) {
            cn = Conexion.Conectar();
            pst = cn.prepareStatement("select id, dinero, fecha, concepto, ingresadopor, modificadopor from
                                                                         Ingresos order by id desc");
            rs = pst.executeQuery();
            if (rs.next()) {
                do {
                    PdfPCell celda1 = new PdfPCell(new Paragraph(rs.getString(1), FontFactory.getFont("Arial", 6,
                                                                                Font.PLAIN, BaseColor.BLACK)));
                    celda1.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    tabla__Ingresos.addCell(celda1);
 
                    PdfPCell celda2 = new PdfPCell(new Paragraph(rs.getString(2), FontFactory.getFont("Arial", 6,
                                                                            Font.PLAIN, BaseColor.BLACK)));
                    celda2.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda2.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    tabla__Ingresos.addCell(celda2);
 
                    PdfPCell celda3 = new PdfPCell(new Paragraph(rs.getString(3), FontFactory.getFont("Arial", 6,
                                                                             Font.PLAIN, BaseColor.BLACK)));
                    celda3.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda3.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    tabla__Ingresos.addCell(celda3);
 
                    PdfPCell celda4 = new PdfPCell(new Paragraph(rs.getString(4), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                    celda4.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda4.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    tabla__Ingresos.addCell(celda4);
 
                    PdfPCell celda5 = new PdfPCell(new Paragraph(rs.getString(5), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                    celda5.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda5.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    tabla__Ingresos.addCell(celda5);
 
                    PdfPCell celda6 = new PdfPCell(new Paragraph(rs.getString(6), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                    celda6.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celda6.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    tabla__Ingresos.addCell(celda6);
 
                } while (rs.next());
 
                documento.add(tabla__Ingresos);
            }
            pst = cn.prepareStatement("select sum(dinero) as total from Ingresos");
            rs = pst.executeQuery();
            if (rs.next()) {
                PdfPCell celdaTotalSuma = new PdfPCell(new Paragraph(rs.getString(1), FontFactory.getFont("Arial", 6, Font.BOLD, BaseColor.ORANGE)));
                celdaTotalSuma.setHorizontalAlignment(Element.ALIGN_CENTER);
                celdaTotalSuma.setBackgroundColor(BaseColor.BLACK);
                tabla__Total.addCell(celdaTotalSuma);
            }
 
            documento.add(tabla__Total);
 
            cn.close();
 
        } else if (mes != 0 && anio == 0) {
 
                cn = Conexion.Conectar();
                pst = cn.prepareStatement("select id, dinero, fecha, concepto, ingresadopor, modificadopor from Ingresos where month(fecha) = ? order by id desc");
                pst.setInt(1, mes);
                rs = pst.executeQuery();
                if (rs.next()) {
                    do {
                        PdfPCell celda1 = new PdfPCell(new Paragraph(rs.getString(1), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda1.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda1);
 
                        PdfPCell celda2 = new PdfPCell(new Paragraph(rs.getString(2), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda2.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda2.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda2);
 
                        PdfPCell celda3 = new PdfPCell(new Paragraph(rs.getString(3), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda3.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda3.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda3);
 
                        PdfPCell celda4 = new PdfPCell(new Paragraph(rs.getString(4), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda4.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda4.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda4);
 
                        PdfPCell celda5 = new PdfPCell(new Paragraph(rs.getString(5), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda5.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda5.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda5);
 
                        PdfPCell celda6 = new PdfPCell(new Paragraph(rs.getString(6), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda6.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda6.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda6);
 
                    } while (rs.next());
 
                    documento.add(tabla__Ingresos);
                }
                pst = cn.prepareStatement("select sum(dinero) as total from Ingresos where month(fecha) = ?");
                pst.setInt(1, mes);
                rs = pst.executeQuery();
                if (rs.next()) {
                    PdfPCell celdaTotalSuma = new PdfPCell(new Paragraph(rs.getString(1), FontFactory.getFont("Arial", 6, Font.BOLD, BaseColor.ORANGE)));
                    celdaTotalSuma.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celdaTotalSuma.setBackgroundColor(BaseColor.BLACK);
                    tabla__Total.addCell(celdaTotalSuma);
                }
 
                documento.add(tabla__Total);
 
                cn.close();
 
        } else if (mes != 0 && anio != 0) {
 
                cn = Conexion.Conectar();
                pst = cn.prepareStatement("select id, dinero, fecha, concepto, ingresadopor, modificadopor from Ingresos where month(fecha) = ? and year(fecha) = ? order by id desc");
                pst.setInt(1, mes);
                pst.setInt(2, anio);
                rs = pst.executeQuery();
                if (rs.next()) {
                    do {
                        PdfPCell celda1 = new PdfPCell(new Paragraph(rs.getString(1), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda1.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda1);
 
                        PdfPCell celda2 = new PdfPCell(new Paragraph(rs.getString(2), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda2.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda2.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda2);
 
                        PdfPCell celda3 = new PdfPCell(new Paragraph(rs.getString(3), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda3.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda3.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda3);
 
                        PdfPCell celda4 = new PdfPCell(new Paragraph(rs.getString(4), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda4.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda4.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda4);
 
                        PdfPCell celda5 = new PdfPCell(new Paragraph(rs.getString(5), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda5.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda5.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda5);
 
                        PdfPCell celda6 = new PdfPCell(new Paragraph(rs.getString(6), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda6.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda6.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda6);
 
                    } while (rs.next());
 
                    documento.add(tabla__Ingresos);
                }
                pst = cn.prepareStatement("select sum(dinero) as total from Ingresos where month(fecha) = ? and year(fecha) = ?");
                pst.setInt(1, mes);
                pst.setInt(2, anio);
                rs = pst.executeQuery();
                if (rs.next()) {
                    PdfPCell celdaTotalSuma = new PdfPCell(new Paragraph(rs.getString(1), FontFactory.getFont("Arial", 6, Font.BOLD, BaseColor.ORANGE)));
                    celdaTotalSuma.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celdaTotalSuma.setBackgroundColor(BaseColor.BLACK);
                    tabla__Total.addCell(celdaTotalSuma);
                }
 
                documento.add(tabla__Total);
 
                cn.close();
 
        } else if (mes == 0 && anio != 0) {
 
                cn = Conexion.Conectar();
                pst = cn.prepareStatement("select id, dinero, fecha, concepto, ingresadopor, modificadopor from Ingresos where year(fecha) = ? order by id desc");
                pst.setInt(1, anio);
                rs = pst.executeQuery();
                if (rs.next()) {
                    do {
                        PdfPCell celda1 = new PdfPCell(new Paragraph(rs.getString(1), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda1.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda1);
 
                        PdfPCell celda2 = new PdfPCell(new Paragraph(rs.getString(2), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda2.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda2.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda2);
 
                        PdfPCell celda3 = new PdfPCell(new Paragraph(rs.getString(3), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda3.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda3.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda3);
 
                        PdfPCell celda4 = new PdfPCell(new Paragraph(rs.getString(4), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda4.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda4.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda4);
 
                        PdfPCell celda5 = new PdfPCell(new Paragraph(rs.getString(5), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda5.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda5.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda5);
 
                        PdfPCell celda6 = new PdfPCell(new Paragraph(rs.getString(6), FontFactory.getFont("Arial", 6, Font.PLAIN, BaseColor.BLACK)));
                        celda6.setHorizontalAlignment(Element.ALIGN_CENTER);
                        celda6.setBackgroundColor(BaseColor.LIGHT_GRAY);
                        tabla__Ingresos.addCell(celda6);
 
                    } while (rs.next());
 
                    documento.add(tabla__Ingresos);
                }
                pst = cn.prepareStatement("select sum(dinero) as total from Ingresos where year(fecha) = ?");
                pst.setInt(1, anio);
                rs = pst.executeQuery();
                if (rs.next()) {
                    PdfPCell celdaTotalSuma = new PdfPCell(new Paragraph(rs.getString(1), FontFactory.getFont("Arial", 6, Font.BOLD, BaseColor.ORANGE)));
                    celdaTotalSuma.setHorizontalAlignment(Element.ALIGN_CENTER);
                    celdaTotalSuma.setBackgroundColor(BaseColor.BLACK);
                    tabla__Total.addCell(celdaTotalSuma);
                }
 
                documento.add(tabla__Total);
 
                cn.close();
 
        } else {
            JOptionPane.showMessageDialog(null, "la opcion no es correcta");
        }
 
        documento.close();
        JOptionPane.showMessageDialog(null, "Reporte creado");
 
    } catch (SQLException | DocumentException | IOException e) {
 
    }
}
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

problema con itext

Publicado por Max (2 intervenciones) el 27/05/2020 20:28:25
Al parecer ningun programador conoce el problema
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar