/** * Writes out the information and PDF file to disk, and closes the stream. Returns a negative * Result if there was an error creating the document, and a positive result if the document was * successfully written and closed. */ public Result finish() { if (_open) { fillNullCells(); try { _d.add(_table); } catch (DocumentException e) { return new Result(false, "The document was not successfully constructed."); } _d.close(); _open = false; } return new Result(true, "The document is closed."); }
/** * Constructor. Takes in a string representing where the file should be written to disk. * * @param fname * @throws DocumentException * @throws FileNotFoundException */ public BarcodePdf(String fname) throws FileNotFoundException, DocumentException { _d = new Document(); _writer = PdfWriter.getInstance(_d, new FileOutputStream(fname)); _d.setMargins(25, 25, 25, 25); _d.open(); _open = true; _rawContent = _writer.getDirectContent(); _table = new PdfPTable(4); _table.setHorizontalAlignment(Element.ALIGN_CENTER); _itemsAdded = 0; }
public void generaPdf() { Document documento = new Document(); FileOutputStream ficheroPdf; PdfPTable tabla_emple, tabla_proyec, tabla_relacional; empleados = consultas.recogerEmpleados(); ProjectX = consultas.recogerProyectos(); ArrayList<Empleado> relacional; try { ficheroPdf = new FileOutputStream("ejemplo.PDF"); PdfWriter.getInstance(documento, ficheroPdf).setInitialLeading(20); } catch (Exception ex) { System.out.println(ex.toString()); } try { // abrimos el documento para editarlo documento.open(); // aqui agregamos todo el contenido del PDF... documento.add( new Paragraph( "Este informe es una manera de imprimir la informacion, en forma de tablas, de la base de datos")); documento.add(new Paragraph(" ")); // ***************TABLA DE LOS EMPLEADOS************** documento.add(new Paragraph("Empleados de la compañia:")); documento.add(new Paragraph(" ")); // inicio la tabla con las 4 columnas de los empleados tabla_emple = new PdfPTable(4); // cada 4 addCell, es una fila nueva tabla_emple.addCell("NIF"); tabla_emple.addCell("Nombre"); tabla_emple.addCell("Apellidos"); tabla_emple.addCell("Fecha de Nacimiento"); for (Empleado e : empleados) { tabla_emple.addCell(e.getNif()); tabla_emple.addCell(e.getNombre()); tabla_emple.addCell(e.getApellido()); tabla_emple.addCell(e.getFechaNacimiento()); } documento.add(tabla_emple); documento.add(new Paragraph(" ")); // *********TABLA DE LOS PROYECTOS************ documento.add(new Paragraph("Proyectos actuales en la compañia:")); documento.add(new Paragraph(" ")); tabla_proyec = new PdfPTable(5); tabla_proyec.addCell("Titulo"); tabla_proyec.addCell("Fecha de Inicio"); tabla_proyec.addCell("Fecha de Entrega"); tabla_proyec.addCell("Descripcion"); tabla_proyec.addCell("Nº Maximo de empleados"); for (Proyecto p : ProjectX) { tabla_proyec.addCell(p.getTitulo()); tabla_proyec.addCell(p.getFechaInicio()); tabla_proyec.addCell(p.getFechaFin()); tabla_proyec.addCell(p.getDescripcion()); tabla_proyec.addCell(String.valueOf(p.getMaxEmple())); } documento.add(tabla_proyec); documento.add(new Paragraph(" ")); // TABLA RELACIONAL documento.add(new Paragraph("Tabla relacional de empleados por proyecto")); documento.add(new Paragraph(" ")); tabla_relacional = new PdfPTable(4); for (Proyecto p : ProjectX) { Paragraph titulos = new Paragraph(p.getTitulo()); titulos.setAlignment(1); PdfPCell celda = new PdfPCell(titulos); celda.setColspan(4); celda.setRowspan(2); tabla_relacional.addCell(celda); tabla_relacional.addCell("NIF"); tabla_relacional.addCell("Nombre"); tabla_relacional.addCell("Apellidos"); tabla_relacional.addCell("Fecha de Nacimiento"); relacional = consultas.recogerEmpleProy(p.getId()); for (Empleado er : relacional) { tabla_relacional.addCell(er.getNif()); tabla_relacional.addCell(er.getNombre()); tabla_relacional.addCell(er.getApellido()); tabla_relacional.addCell(er.getFechaNacimiento()); } Paragraph fin = new Paragraph(" "); titulos.setAlignment(1); PdfPCell celda2 = new PdfPCell(fin); celda2.setColspan(4); celda2.setRowspan(2); } documento.add(tabla_relacional); documento.add(new Paragraph(" ")); documento.add(new Paragraph("Desarrollador del programa: Jairo Gallardo Zorrilla")); documento.add(new Paragraph("Cliente: Manuel Torres")); documento.add(new Paragraph("")); // cerramos el documento y se genera JOptionPane.showMessageDialog(null, "El archivo pdf se ha generado correctamente"); documento.close(); } catch (Exception ex) { System.out.println(ex.toString()); } }