I am trying to print a custom paper size from a Java applet. I have set the paper size but it is being ignored.
I have also tried using the book method as I have seen something about this helping to get it working but when I use that it just prints a blank page and still seems to be about A4 (I'm looking to print cheques which are obviously much smaller (8" x 4")).
I am trying to print HTML from a JEditorPane if that makes any difference.
If you have any ideas I would be very grateful, I'm tearing my hair out with this one.
I should also add that I am very much a beginner when it comes to Java.
Here is what I have so far:
Updated: I have now got the page size right but can't seem to get the HTML page I'm loading to fit or line up with the page size.
Update: Now I just can't get the applet to run in the browser. It works from eclipse just not the browser. I will also need to pass the URL from a parameter.
Here is the HTML applet tag I'm using and updated Java code:
<!DOCTYPE html>
<html>
<head><title>Printing Cheque</title></head>
<body>
<applet width=100 height=100 code="HTMLPrinter"
archive="cheque_print.jar">
</applet>
</body>
</html>
package com.yunatech.pns.chequeprint;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JEditorPane;
public class HTMLPrinter extends Applet {
/**
*
*/
private static final long serialVersionUID = 8065834484717197790L;
private static JEditorPane editor;
public HTMLPrinter() {
try {
editor = new JEditorPane();
editor.setPage("http://localhost/print_test/test.html");
PrinterJob pj = PrinterJob.getPrinterJob();
if (pj.printDialog()) {
PageFormat pf = pj.defaultPage();
Paper paper = pf.getPaper();
double width = 8d * 72d;
double height = 4d * 72d;
double margin = 1d * 72d;
paper.setSize(width, height);
paper.setImageableArea(
margin,
0,
width - (margin * 2),
height);
System.out.println("Before- " + dump(paper));
pf.setOrientation(PageFormat.PORTRAIT);
pf.setPaper(paper);
System.out.println("After- " + dump(paper));
System.out.println("After- " + dump(pf));
dump(pf);
PageFormat validatePage = pj.validatePage(pf);
System.out.println("Valid- " + dump(validatePage));
Book pBook = new Book();
pBook.append(new Page(), pf);
pj.setPageable(pBook);
try {
pj.print();
} catch (PrinterException ex) {
ex.printStackTrace();
}
}
} catch (Exception exp) {
exp.printStackTrace();
}
}
protected static String dump(Paper paper) {
StringBuilder sb = new StringBuilder(64);
sb.append(paper.getWidth()).append("x").append(paper.getHeight())
.append("/").append(paper.getImageableX()).append("x").
append(paper.getImageableY()).append(" - ").append(paper
.getImageableWidth()).append("x").append(paper.getImageableHeight());
return sb.toString();
}
protected static String dump(PageFormat pf) {
Paper paper = pf.getPaper();
return dump(paper);
}
public static class Page implements Printable {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
if (pageIndex >= 1) return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D)graphics;
g2d.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY());
editor.setSize((int)pageFormat.getImageableWidth(), (int)pageFormat.getImageableHeight());
editor.print(g2d);
return Printable.PAGE_EXISTS;
}
}
}
Thanks in advance for any help you can offer.