如何使用java创建和打印表单

如何使用java创建和打印表单,java,forms,printing,Java,Forms,Printing,也许有人可以伸出援助之手,告诉我们如何创建和打印表单 这样地: 使用java。 此外,它还应该填充所需的信息。

也许有人可以伸出援助之手,告诉我们如何创建和打印表单 这样地: 使用java。
此外,它还应该填充所需的信息。


我想你需要一点谷歌搜索——这看起来是一项非常琐碎的任务。

如果你正在使用Swing,请按照以下步骤操作:

对于A4设置:

使用约750像素的JF机架。x960像素

在窗口中,使用jlabel、JTextFields和JTextAreas来设计模板。 在窗口的任何位置添加打印按钮(以启动打印命令)

现在,当所有设计完成后,在button action事件的代码窗口中 加:

.setVisible(false);
.print();
第一个将隐藏按钮,第二个将实际显示一个打印对话框

此外,使用Netbeans IDE可以节省设计时间。它在设计、编译和测试过程中节省了大量时间


如有任何疑问,请回复,希望信息有帮助。

如果您需要在web应用程序中执行此操作,则应使用javascript进行打印。但是您可以使用Java呈现页面


如果您使用swing:

进行此操作,则有点晚了,但我会将此内容留在这里以供参考: //仅适用于相关代码

 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;

}
注意:您的类需要实现可打印
它有点脏,但它是我学习Java时编写的相当旧的代码,我在这里发布时没有仔细检查,但它在我的应用程序中工作,所以…

它看起来确实像一个pdf文件,试试看。这是一个用于创建PDF的java库。没什么,只是我不知道从何开始,所以我要求您提供任何建议iReport或其他java Reporting库
 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;

}