|
Ola pessoal javanes,
Pesquisei em varias paginas e nao encontrei um
metodo p/ imprimir um numero indefinido de paginas. Alguem tem algum exemplo
simples?
import java.awt.*;
import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; import java.awt.print.*; import java.awt.image.BufferedImage; public class Transacoes extends JPanel implements
MouseListener, Printable {
private static Vector data;
private MyTextArea textArea; private String Cod_Usuario; private Lojas gbLojas; private ListarRadioBox rbListar; private JLabel m_title = new
JLabel("Relat�rio");
private int m_maxNumPage = 1; private int nCol = 0; private int asd = 1; private boolean counting = true; private int old_m_maxNumPage = 1;
private int old_nCol = 0; private int old_asd = 1; private boolean old_counting = true; private int old_Pageindex = 0; private boolean onlyFooter =
false;
private String resultado; private Thread threadListar; private int i = 0;
file://private boolean fim = false; public void Imprimir()
{
Thread runner = new Thread() { public void run() { getParent().getParent().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); try { PrinterJob prnJob = PrinterJob.getPrinterJob(); PageFormat pageFormat = prnJob.defaultPage(); pageFormat.setOrientation(PageFormat.PORTRAIT); Book book = new Book(); int m_wPage = (int)(pageFormat.getWidth()); int m_hPage = (int)(pageFormat.getHeight()); int pageIndex =
0;
while (true) { BufferedImage img = new BufferedImage(m_wPage, m_hPage, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(Color.white); g.fillRect(0, 0, m_wPage, m_hPage); if (Transacoes.this.print(g, pageFormat, pageIndex) != Printable.PAGE_EXISTS){ i=0; break; }
book.append(Transacoes.this,
pageFormat);
pageIndex++; }
prnJob.setPageable(book);
file://prnJob.setPrintable(Transacoes.this); if
(!prnJob.printDialog())
return; file://PageFormat pageFormat = new PageFormat (); file://pageFormat = prnJob.pageDialog (pageFormat);
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
prnJob.print(); } catch (Exception ex) { ex.printStackTrace(); } finally { getParent().getParent().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } } }; runner.start(); i=0; } public int print(Graphics pg, PageFormat
pageFormat, int pageIndex) throws PrinterException {
Paper papel = new
Paper();
papel.setSize(pageFormat.getHeight(), pageFormat.getWidth()); pageFormat.setPaper(papel); pg.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY()); String string =
textArea.getText();
string += "\n"; int tamanho = string.length(), inicio=i, altura=0, tamDados, linha=0; byte[] Byte = string.getBytes(); while (i<tamanho){ tamDados=0; while(Byte[i] != 10){ i++; tamDados++; System.out.println("i=>"+i); } pg.drawBytes(Byte,inicio,tamDados,0,altura); byte[] vetor = new byte[1]; vetor[0]=(byte)i; String nada = new String(vetor); pg.drawString(nada,210,altura); i++; inicio = i; altura += 15; linha++;
if((i==tamanho)||(linha==34)){
return PAGE_EXISTS; } } linha=0; return NO_SUCH_PAGE; } } /* if (pageIndex == 0) { m_maxNumPage = 1; counting = true; asd = 1; nCol = 0; old_Pageindex = 0; } if (old_Pageindex == pageIndex)
{
file://restaurar valores antigos, do (pageIndex-1) m_maxNumPage = old_m_maxNumPage; nCol = old_nCol; asd = old_asd; counting = old_counting; } old_Pageindex = pageIndex; old_m_maxNumPage = m_maxNumPage; old_nCol = nCol; old_asd = asd; old_counting = counting; if (pageIndex >=
m_maxNumPage)
return NO_SUCH_PAGE;
pg.translate((int)pageFormat.getImageableX(),
(int)pageFormat.getImageableY());
int wPage = 0; int hPage = 0; if (pageFormat.getOrientation() == pageFormat.PORTRAIT) { wPage = (int)pageFormat.getImageableWidth(); hPage = (int)pageFormat.getImageableHeight() + 5; } else { wPage = (int)pageFormat.getImageableWidth(); hPage = (int)pageFormat.getImageableHeight() + 5; pg.setClip(0,0,wPage,hPage); } int y = 0;
pg.setFont(m_title.getFont()); file://pg.setColor(Color.blue); file://pg.fillRect(0, 0, wPage, hPage); pg.setColor(Color.black); Font fn = pg.getFont(); FontMetrics fm = pg.getFontMetrics(); y += fm.getAscent(); if (!((onlyFooter) && (pageIndex == (m_maxNumPage-1)))) pg.drawString(m_title.getText(), (int)(wPage - m_title.getMinimumSize().width)/2, y); y += 20; // space between title and table headers Font headerFont =
tbTransacoes.getFont().deriveFont(Font.BOLD);
pg.setFont(headerFont); fm = pg.getFontMetrics(); TableColumnModel colModel =
tbTransacoes.getColumnModel();
int nColumns = colModel.getColumnCount(); int x[][] = new int[nColumns][2]; x[nCol][0] =
0;
x[nCol][1] = 0; int h =
fm.getAscent();
y += h; // add ascent of header font because of baseline // positioning (see figure 2.10) int nRow, i = 0;
if (!((onlyFooter) &&
(pageIndex == (m_maxNumPage-1)))) {
// Desenha os headers for (i = nCol; i<nColumns; i++) { TableColumn tk = colModel.getColumn(i); int width = tk.getWidth(); x[i][1] = width; if (x[i][0] + x[i][1] > wPage) { nColumns = i - nCol; if (counting) asd++; break; } if (i+1<nColumns) { x[i+1][0] = x[i][0] + x[i][1]; } String title = (String)tk.getIdentifier(); pg.setColor(Color.gray); pg.fill3DRect(x[i][0], y - fm.getAscent() , x[i][1], fm.getHeight(), true); pg.setColor(Color.black); pg.drawString(title, x[i][0] + (int)(x[i][1] - fm.stringWidth(title))/2, y); } //// fim de Desenha os headers if (i ==
colModel.getColumnCount()) {
nColumns -= nCol; counting = false; } }
pg.setFont(tbTransacoes.getFont());
fm = pg.getFontMetrics(); int header =
y;
h = fm.getHeight(); int rowH = Math.max(h, 10); int rowPerPage = (hPage-header)/rowH - 3; m_maxNumPage = asd * Math.max((int)Math.ceil(tbTransacoes.getRowCount()/(double)rowPerPage), 1); TableModel tblModel =
tbTransacoes.getModel();
int iniRow = (pageIndex/asd)*rowPerPage; int endRow = Math.min(tbTransacoes.getRowCount(), iniRow+rowPerPage); int numLinhasUltimaPag =
tbTransacoes.getRowCount() - (rowPerPage * ((m_maxNumPage/asd) -
1));
if (y + (numLinhasUltimaPag*h) + (2*h) + pnlTotal.getHeight() > hPage - h ) { m_maxNumPage++; onlyFooter = true; } if (!((onlyFooter) &&
(pageIndex == (m_maxNumPage-1)))) {
// Desenha as linhas (campo de dados) for (nRow = iniRow; nRow < endRow; nRow++) { if (nRow % 2 == 0) pg.setColor(Color.lightGray); else pg.setColor(Color.white); y +=
h;
pg.fillRect(x[nCol][0], y - fm.getAscent(), x[nCol + nColumns - 1][0] + x[nCol + nColumns-1][1], fm.getHeight()); for (i = nCol; i < nColumns+nCol; i++) { Object obj = tbTransacoes.prepareRenderer( tbTransacoes.getCellRenderer(nRow, i), nRow, i); String str; try { str = obj.toString(); } catch(NullPointerException e) { str = " "; } pg.setColor(Color.black); pg.drawString(str, x[i][0] + (int)(x[i][1] - fm.stringWidth(str))/2, y); } } // fim de Desenha as linhas if (i ==
colModel.getColumnCount())
nCol = 0; else nCol += nColumns;
pg.setColor(Color.black);
JLabel str = new JLabel("P�gina " + (pageIndex/asd + 1) + " de " + m_maxNumPage / asd);
pg.drawString(str.getText(), (int)(wPage - str.getMinimumSize().width)/2, hPage
- str.getMinimumSize().height + h);
/////////////////////////////
} if (pageIndex ==
(m_maxNumPage-1)) {
if (!onlyFooter) y += (2*h); else y = 10;
pg.translate(0,y);
pnlTotal.print(pg); } /////////////////////////////@#$#@%$#@% System.gc(); // roda o coletor de lixo return PAGE_EXISTS; } */
Desde j� agrade�o.
Valeu pela aten��o!
John Tau,
Software Express Inform�tica. Rua Cerro Cor�, 1208 sala 28
55-11-3024-5333 ramal 5257 |
- [java-list] impressao em java John Tau
- [java-list] impressao em java Marco Antonio
