开发者

how to create and print form using java

maybe someone can give hand of help and tell h开发者_JAVA技巧ow to create and print form like this:

how to create and print form using java

using java. Also, it should be filled with needed information.


java.awt.print - Java 2D printing, since JDK 1.2 javax.print - aka the Java Print Service (JPS) API, since JDK 1.4

from http://java.sun.com/javase/technologies/desktop/printing/

I think you need a bit of googling - it looks like a very trivial task.


If you are using Swing, the follow the procedure below:

For A4 setting:

Use a JFrame of approx. 750 px. X 960 px.

In the Window use JLabels, JTextFields and JTextAreas to Design the template. Also do add a print button anywhere on the window (to initiate the print command).

Now when all designing is complete, in the code window of the button action event, simply add:

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

First one will hide the Button, second will actually present you with a print dialog.

Additionally, use Netbeans IDE to save time in designing. It is a great time saver in the designing, compiling and testing grounds.

Please revert back for any doubts, Hope the information is helpful.


If you need to do it in a web application, the printing should be done in javascript. But you may render the page using Java. http://shyarmal.blogspot.com/2011/08/printing-example-with-java-ee.html

If you are doing it using swing: http://shyarmal.blogspot.com/2011/08/printing-with-jeditorpane.html


A little late, but I'll leave this here for reference: //pertinent code only

 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;

}

Note: Your class needs to implement Printable It's a little dirty, but it's rather old code from when I was learning Java and I didn't double-check it as I posted it here, but it's working in my application so.....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜