Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)
The web page you load in browser can be printed using one of the following ways:
(您可以使用以下方式之一打印在浏览器中加载的网页:)
1. Using the window.print() JavaScript function. This function can be invoked from JavaScript code on the loaded web page (e.g. when user clicks some button, JavaScript code invokes this function to print the web page). (1.使用JavaScript函数window.print(),可以从加载的网页上的JavaScript代码中调用此功能(例如,当用户单击某个按钮时,JavaScript代码会调用此功能以打印网页)。)
JButton print = new JButton("Print"); print.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { browser.print(); } });
Both methods prints currently loaded web page using default print settings. If you need to print web page with custom print settings, you must register your own implementation of PrintHandler interface where you can override default print settings or cancel printing. For example:(两种方法均使用默认打印设置来打印当前加载的网页。如果您需要使用自定义打印设置来打印网页,则必须注册自己的PrintHandler接口实现,在此您可以覆盖默认打印设置或取消打印。例如:)
import com.teamdev.jxbrowser.chromium.*; import com.teamdev.jxbrowser.chromium.PrintJob; import com.teamdev.jxbrowser.chromium.events.PrintJobEvent; import com.teamdev.jxbrowser.chromium.events.PrintJobListener; import com.teamdev.jxbrowser.chromium.swing.BrowserView; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.List; /** * The sample demonstrates how to print currently loaded web page with custom print settings. */ public class PrintSample { public static void main(String[] args) { final Browser browser = new Browser(); BrowserView view = new BrowserView(browser); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JButton print = new JButton("Print"); print.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { browser.print(); } }); frame.add(print, BorderLayout.NORTH); frame.add(view, BorderLayout.CENTER); frame.setSize(700, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); browser.setPrintHandler(new PrintHandler() { @Override public PrintStatus onPrint(PrintJob printJob) { PrintSettings printSettings = printJob.getPrintSettings(); printSettings.setPrinterName("Microsoft XPS Document Writer"); printSettings.setLandscape(false); printSettings.setPrintBackgrounds(false); printSettings.setColorModel(ColorModel.COLOR); printSettings.setDuplexMode(DuplexMode.SIMPLEX); printSettings.setDisplayHeaderFooter(true); printSettings.setCopies(1); printSettings.setPaperSize(PaperSize.ISO_A4); List<PageRange> ranges = new ArrayList<PageRange>(); ranges.add(new PageRange(0, 3)); printSettings.setPageRanges(ranges); printJob.addPrintJobListener(new PrintJobListener() { @Override public void onPrintingDone(PrintJobEvent event) { System.out.println("Printing is finished successfully: " + event.isSuccess()); } }); return PrintStatus.CONTINUE; } }); browser.loadURL("http://www.teamdev.com"); } }