Created
October 12, 2012 06:59
-
-
Save dagvadorj/3877702 to your computer and use it in GitHub Desktop.
Printing applet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.print.*; | |
import java.io.*; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.text.MessageFormat; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.swing.JEditorPane; | |
public class Applet extends java.applet.Applet { | |
@Override | |
public void paint(Graphics g) { | |
g.drawString("printing applet", 20, 20); | |
} | |
public String printReview(String id, String url) { | |
JEditorPane editorPane = null; | |
try { | |
editorPane = new JEditorPane(url + "/ReviewServlet?id=" + id); | |
} catch (IOException ex) { | |
Logger.getLogger(Applet.class.getName()).log(Level.SEVERE, null, ex); | |
return "fail"; | |
} | |
// Get the print job | |
PrinterJob job = PrinterJob.getPrinterJob(); | |
// Remove unnecessary margins and white spaces | |
PageFormat pageFormat = job.defaultPage(); | |
Paper paper = pageFormat.getPaper(); | |
paper.setImageableArea(0, 0, pageFormat.getWidth(), pageFormat.getHeight()); | |
pageFormat.setPaper(paper); | |
job.validatePage(pageFormat); | |
// Set printable | |
Printable printable = editorPane.getPrintable(new MessageFormat(""), new MessageFormat("")); | |
job.setPrintable(printable, pageFormat); | |
try { | |
job.print(); | |
} catch (PrinterException ex) { | |
Logger.getLogger(Applet.class.getName()).log(Level.SEVERE, null, ex); | |
return "fail"; | |
} | |
return "success"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment