2011-08-22 24 views
1

也許有人可以給予幫助,並告訴如何創建和打印表格 這樣的: enter image description here 使用java。 此外,它應該填充所需的信息。如何使用java創建和打印表格

+1

它看起來像一個PDF文件,嘗試[itext](http://itextpdf.com)。這是一個創建PDF的Java庫。 – Augusto

+0

什麼也沒有,只是我不知道從什麼開始 所以我要求任何建議 –

+0

IReport或與另一個Java報告庫 – mKorbel

回答

0

如果使用展開,請遵循以下步驟:

對於A4設置:

使用大約一個JFrame。 750像素。 X 960 px。

在窗口中使用JLabels,JTextFields和JTextAreas來設計模板。 也可以在窗口的任何位置添加打印按鈕(以啓動打印命令)。

現在,當所有的設計完成後,在按鍵操作事件的代碼窗口,只需 地址:

<Button Name>.setVisible(false); 
<PanelName>.print(); 

第一個將隱藏按鈕,第二個將實際上的打印對話框您呈現。

此外,使用Netbeans IDE來節省設計時間。它在設計,編譯和測試場地中節省時間。

請回復任何懷疑,希望信息是有幫助的。

0

有點晚了,但我會在這裏把這個留給參考: //相關代碼只

import java.awt.print 
    public void FilePrintClicked(){ 


    PrinterJob job = PrinterJob.getPrinterJob(); 

    PageFormat format = job.defaultPage(); 
    format.setOrientation(PageFormat.LANDSCAPE); 

    job.setPrintable(this, format); 

    try{ 
     if(job.printDialog()) job.print(); 
    } 
    catch(Exception e){e.printStackTrace();} 

} 

public int print(Graphics g, PageFormat format, int pagenum) { 

    if (pagenum > 0){ 
     return Printable.NO_SUCH_PAGE; 
    } 

    g.translate((int)format.getImageableX(), (int)format.getImageableY()); 

    float pageWidth = (float)format.getImageableWidth(); 
    float pageHeight = (float)format.getImageableHeight(); 

    float imageHeight = (float)this.getHeight(); 
    float imageWidth = (float)this.getWidth(); 

    float scaleFactor = Math.min((float)pageWidth/(float)imageWidth, (float)pageHeight/(float)imageHeight); 

    int scaledWidth = (int)(((float)imageWidth)*scaleFactor); 

    int scaledHeight = (int)(((float)imageHeight)*scaleFactor); 

    BufferedImage canvas = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB); 
    Graphics2D gg = canvas.createGraphics(); 
    this.paint(gg); 
    Image img = canvas ; 

    g.drawImage(img, 0, 0, scaledWidth, scaledHeight, null); 

    return Printable.PAGE_EXISTS; 

} 

注意:你的課需要實現Printable 這有點髒,但是從我學習Java的時候開始,它的代碼很老舊,我沒有仔細檢查它,因爲我在這裏發佈了它,但它在我的應用程序中工作,所以.....