use of com.teamdev.jxbrowser.engine.Engine in project JxBrowser-Examples by TeamDev-IP.
the class PrintFromJava method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JButton print = new JButton("Print");
print.addActionListener(e -> browser.mainFrame().ifPresent(Frame::print));
JFrame frame = new JFrame("Print From Java");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.add(print, BorderLayout.NORTH);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.navigation().loadUrl("https://www.google.com");
});
}
use of com.teamdev.jxbrowser.engine.Engine in project JxBrowser-Examples by TeamDev-IP.
the class PrintFromJavaScript method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("Print From JavaScript");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.mainFrame().ifPresent(mainFrame -> {
mainFrame.loadHtml("<html><body><a href='#' onclick='window.print();'>" + "Print</a></body></html>");
});
});
}
use of com.teamdev.jxbrowser.engine.Engine in project JxBrowser-Examples by TeamDev-IP.
the class PrintToPdf method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(OFF_SCREEN);
Browser browser = engine.newBrowser();
browser.set(PrintCallback.class, (params, tell) -> tell.print());
browser.set(PrintHtmlCallback.class, (params, tell) -> {
PdfPrinter<PdfPrinter.HtmlSettings> pdfPrinter = params.printers().pdfPrinter();
PrintJob<HtmlSettings> printJob = pdfPrinter.printJob();
printJob.settings().pdfFilePath(Paths.get("google.pdf").toAbsolutePath()).enablePrintingBackgrounds().orientation(PORTRAIT).apply();
printJob.on(PrintCompleted.class, event -> {
if (event.isSuccess()) {
System.out.println("Printing is completed successfully.");
} else {
System.out.println("Printing has failed.");
}
});
tell.proceed(pdfPrinter);
});
browser.navigation().loadUrlAndWait("https://google.com");
browser.mainFrame().ifPresent(Frame::print);
}
use of com.teamdev.jxbrowser.engine.Engine in project JxBrowser-Examples by TeamDev-IP.
the class SaveWebPage method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("Save Web Page");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
browser.navigation().loadUrlAndWait("https://www.google.com");
Path file = Paths.get("index.html");
Path dir = Paths.get("resources_dir");
if (browser.saveWebPage(file, dir, SavePageType.COMPLETE_HTML)) {
System.out.println("The web page has been saved to " + file.toAbsolutePath() + "\n" + "The resources has been saved to " + dir.toAbsolutePath() + " directory");
} else {
System.err.println("Failed to save the web page to " + file);
}
}
use of com.teamdev.jxbrowser.engine.Engine in project JxBrowser-Examples by TeamDev-IP.
the class JarProtocol method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(EngineOptions.newBuilder(HARDWARE_ACCELERATED).addScheme(Scheme.JAR, new InterceptJarRequestCallback()).build());
Browser browser = engine.newBrowser();
invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("JAR Protocol Handler");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
// Load the index.html file located inside a JAR archive added to this Java app classpath.
URL resource = JarProtocol.class.getResource("/resources/index.html");
if (resource != null) {
browser.navigation().loadUrl(resource.toString());
}
}
Aggregations