【教程】将面板和其他Swing组件绘制到PDF文档

jPDFWriter是一个Java?类库,可以帮助用户无需安装任何第三方驱动或软件即可直接从Java程序中创建PDF文件。该教程演示如何使用免费库jPDFWriter将JPanel和其他Swing组件打印或绘制到PDF文档。

关联产品
  • jPDFWriter

java代码示例:

public class PrintPanelToPDF implements Printable{    public static void main (String [] args)    {    try    {     // create a PDF Printer Job     PDFPrinterJob printer = (PDFPrinterJob)PDFPrinterJob.getPrinterJob ();     // set the printable object     printer.setPrintable (new PrintPanelToPDF());     // set number of copies to 1     printer.setCopies (1);     // print and save the document     printer.print("C:\test\mydoc.pdf");     // output done message     System.out.println("Done!");    }    catch (Throwable t)    {     t.printStackTrace();    }    }    public int print (Graphics g, PageFormat pf, int pageIndex)    {    if (pageIndex == 0)    {          // translate the graphics for margins      g.translate(100, 100);      // create a panel with a label and a textfield      JPanel myPanel = new javax.swing.JPanel();      myPanel.setName("myPanel");      myPanel.setBorder(new javax.swing.border.EtchedBorder());      myPanel.setBounds(new java.awt.Rectangle(0, 0, 300, 300));      // create a label      JLabel myLabel = new JLabel("MyLabel");      myLabel.setText("Label");      myLabel.setLocation(10, 10);      myLabel.setSize(50, 20);      myLabel.setVisible(true);      // add label to panel      myPanel.add(myLabel);      // create a text field      JTextField myTextField = new JTextField();      myTextField.setText("My Text");      myTextField.setLocation(65, 10);      myTextField.setSize(50, 20);      myTextField.setVisible(true);      // add text field to panel      myPanel.add(myTextField);     // you can add any swing component here     // .....     // revalidate and repaint the panel     myPanel.revalidate();         myPanel.repaint();     // print the panel to the graphics on page 0     myPanel.print (g);     return Printable.PAGE_EXISTS;    }    else    {     return Printable.NO_SUCH_PAGE;    }    }}

 

标签:PDF

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2017年4月16日
下一篇 2017年4月16日

相关推荐

发表回复

登录后才能评论