mira este es el motodo ke yo uso para hacer un pdf con los datos de muchas tablas ke tengo en la base de datos, intenta adaptarla para tu caso, tendrias ke añadir a tu proyexto el .jar de ireport,
private void printPropio() {
Document document = new Document(PageSize.A4);
PdfWriter writer;
try {
writer =
PdfWriter.getInstance(document, new FileOutputStream(textRuta.getText() +
"\" +
textNombrePDF.getText() +
".pdf"));
System.out.println(textRuta.getText() + "\" + textNombrePDF.getText() +
".pdf");
document.open();
Object o[] = recogerValoresLista();
int luz = o.length;
for (int i = 0; i < luz; i++) {
float[] widths1 = { 1f, 2f, 1.1f, 1f };
PdfPTable table = new PdfPTable(widths1);
PdfPCell cell = new PdfPCell(new Paragraph("GRAFICO " + o[i]));
cell.setColspan(4);
cell.setBorder(0);
table.addCell(cell);
DescripcionGraficosDAO cdao = new DescripcionGraficosDAO();
//este es el rs que necesitamos para llenar la tabla
ResultSet rs =
(ResultSet)cdao.getDescripcionGraficosByIdGraf(Integer.parseInt(o[i].toString()));
cell = new PdfPCell(new Paragraph("HORA SALIDA"));
cell.setBackgroundColor(new Color(193, 205, 205));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("DESCRIPCION"));
cell.setBackgroundColor(new Color(193, 205, 205));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("TIPO RUTA"));
cell.setBackgroundColor(new Color(193, 205, 205));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("S.A.E"));
cell.setBackgroundColor(new Color(193, 205, 205));
table.addCell(cell);
while (rs.next()) {
table.addCell(rs.getObject(1).toString());
table.addCell(rs.getObject(2).toString());
table.addCell(rs.getObject(3).toString());
table.addCell(rs.getObject(4).toString());
}
//PARA LA FILA EN BLANCO ANTES DEL RESUMEN DE TIEMPOS
cell = new PdfPCell(new Paragraph(""));
cell.setColspan(4);
cell.setBorder(0);
table.addCell(cell);
table.addCell(cell);
//aki los tiempos de cada grafico!!!!! (falta da!!!)
//aki las cabezeras
GraficosDAO gdao = new GraficosDAO();
ResultSet rs2 =
(ResultSet)gdao.getTiemposById(Integer.parseInt(o[i].toString()));
rs2.next();
cell = new PdfPCell(new Paragraph("TIEMPO TOTAL"));
cell.setBackgroundColor(new Color(230, 230, 250));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("TIEMPO PARADO"));
cell.setBackgroundColor(new Color(230, 230, 250));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("TIEMPO VACIO"));
cell.setBackgroundColor(new Color(230, 230, 250));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("TIEMPO EXTRA"));
cell.setBackgroundColor(new Color(230, 230, 250));
table.addCell(cell);
//aki los tiempos
table.addCell(rs2.getObject(1).toString());
table.addCell(rs2.getObject(2).toString());
table.addCell(rs2.getObject(3).toString());
table.addCell(rs2.getObject(4).toString());
//PARA LA FILA EN BLANCO ANTES DEL SIGUIENTE GRAFICO
cell = new PdfPCell(new Paragraph(""));
cell.setColspan(4);
cell.setBorder(0);
table.addCell(cell);
table.addCell(cell);
//AÑADIR TABLA AL GRAFICO
document.add(table);
}
//despues de todos los graficos un resumen con los tiempos totales de la seleccion de graficos
float[] widths1 = { 1f, 1f, 1f, 1f };
PdfPTable table = new PdfPTable(widths1);
PdfPCell cell = new PdfPCell(new Paragraph("RESUMEN:"));
cell.setColspan(4);
cell.setBorder(0);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("TIEMPO TOTAL"));
cell.setBackgroundColor(new Color(0, 191, 255));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("TIEMPO PARADO"));
cell.setBackgroundColor(new Color(0, 191, 255));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("TIEMPO VACIO"));
cell.setBackgroundColor(new Color(0, 191, 255));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("TIEMPO EXTRA"));
cell.setBackgroundColor(new Color(0, 191, 255));
table.addCell(cell);
//aki los valores totales del documento
GraficosDAO gdao = new GraficosDAO();
ResultSet rs3 = (ResultSet)gdao.getTiemposResumen(o);
rs3.next();
table.addCell(rs3.getObject(1).toString());
table.addCell(rs3.getObject(2).toString());
table.addCell(rs3.getObject(3).toString());
table.addCell(rs3.getObject(4).toString());
document.add(table);
//cerrar documento
document.close();
} catch (Exception e) {
System.out.println(e);
}
}