• Sábado 21 de Septiembre de 2024, 22:02

Autor Tema:  Imprimir En Java  (Leído 1507 veces)

Dudin

  • Miembro activo
  • **
  • Mensajes: 55
    • Ver Perfil
Imprimir En Java
« en: Lunes 20 de Junio de 2005, 13:52 »
0
hola, tengo unos reports creados con ireport y queria saber como puedo hacer para que cuando pulse un boton me imprima el reporte hecho, pero sin que se visualice.
Gracias.

maldicion

  • Miembro activo
  • **
  • Mensajes: 47
    • Ver Perfil
Re: Imprimir En Java
« Respuesta #1 en: Jueves 23 de Junio de 2005, 02:50 »
0
Hola: no se si te sirva, pero la rutina de impresora que tengo es:

 submitButton.addActionListener( new ActionListener(){
   public void actionPerformed( ActionEvent event ) {
    PrinterJob pj=PrinterJob.getPrinterJob();
    pj.setPrintable(ReporteProveedor.this);
    pj.printDialog();
    try{
     pj.print();
    }catch (Exception PrintException) {}
   }});

en esta rutina, se puede ver como se llama a la rutina de impresora.

 public int print(Graphics g, PageFormat pageFormat,int pageIndex) throws PrinterException {
  Graphics2D  g2 = (Graphics2D) g;
  g2.setColor(Color.black);
  int fontHeight=g2.getFontMetrics().getHeight();
  int fontDesent=g2.getFontMetrics().getDescent();
  double pageHeight = pageFormat.getImageableHeight()-fontHeight;
  double pageWidth = pageFormat.getImageableWidth();
  double tableWidth = (double) resultTable.getColumnModel().getTotalColumnWidth();
  double scale = 1;
  if (tableWidth >= pageWidth) {
    scale =  pageWidth / tableWidth;
  }
  double headerHeightOnPage= resultTable.getTableHeader( ).getHeight()*scale;
  double tableWidthOnPage=tableWidth*scale;
  double oneRowHeight=(resultTable.getRowHeight()+resultTable.getRowMargin())*scale;
  int numRowsOnAPage=(int)((pageHeight-headerHeightOnPage)/oneRowHeight);
  double pageHeightForTable=oneRowHeight*numRowsOnAPage;
  int totalNumPages= (int)Math.ceil(((double)resultTable.getRowCount())/numRowsOnAPage);
  if(pageIndex>=totalNumPages) {
    return NO_SUCH_PAGE;
  }
  g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
  g2.drawString("Page: "+(pageIndex+1),(int)pageWidth/2-35, (int)(pageHeight+fontHeight-fontDesent));
  g2.translate(0f,headerHeightOnPage);
  g2.translate(0f,-pageIndex*pageHeightForTable);
  if (pageIndex + 1 == totalNumPages) {
    int lastRowPrinted = numRowsOnAPage * pageIndex;
    int numRowsLeft = resultTable.getRowCount() - lastRowPrinted;
    g2.setClip(0, (int)(pageHeightForTable * pageIndex),(int) Math.ceil(tableWidthOnPage),(int) Math.ceil(oneRowHeight * numRowsLeft));
  }
  else{    
    g2.setClip(0, (int)(pageHeightForTable*pageIndex), (int) Math.ceil(tableWidthOnPage),(int) Math.ceil(pageHeightForTable));        
  }
  g2.scale(scale,scale);
  resultTable.paint(g2);
  g2.scale(1/scale,1/scale);
  g2.translate(0f,pageIndex*pageHeightForTable);
  g2.translate(0f, -headerHeightOnPage);
  g2.setClip(0, 0,(int) Math.ceil(tableWidthOnPage), (int)Math.ceil(headerHeightOnPage));
  g2.scale(scale,scale);
  resultTable.getTableHeader().paint(g2);
    return Printable.PAGE_EXISTS;
 }

en esta puedes ver como es que se da el formato a la página.
Espero te sirva.

Dudin

  • Miembro activo
  • **
  • Mensajes: 55
    • Ver Perfil
Re: Imprimir En Java
« Respuesta #2 en: Jueves 23 de Junio de 2005, 17:07 »
0
gracias por tu respuesta, me ha sido de gran ayuda.