原創(chuàng)|使用教程|編輯:王香|2017-05-19 15:31:41.000|閱讀 341 次
概述:jPDFWriter是一個(gè)Java?類庫(kù),可以幫助用戶無(wú)需安裝任何第三方驅(qū)動(dòng)或軟件即可直接從Java程序中創(chuàng)建PDF文件。該教程演示如何使用免費(fèi)庫(kù)jPDFWriter將JPanel和其他Swing組件打印或繪制到PDF文檔。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
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; } } }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn