How to print my own paper size (check 8 "x 4")?

I am trying to print my own paper size from a Java applet. I set the paper size, but it is ignored.

I also tried using the book method, as I saw something about it, helping it work, but when I use it, it just prints a blank page and still seems to be around A4 (I am looking for a printout of checks that are obviously much smaller (8 "x 4")).

I am trying to print HTML from JEditorPane if that matters.

If you have any ideas, I would be very grateful, I will tear my hair with this.

I should also add that I am starting very when it comes to Java.

Here is what I still have:

Updated: I now got the page size correctly, but I can't seem to get the HTML page that I am loading to fit or fit the page size.

Update: Now I just can not start the applet in the browser. It works from eclipse, not from the browser. I will also need to pass the url from the parameter.

Here is the HTML applet tag that I use and update the 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.

+4
source share
1 answer

Printing is designed to work in pixels per inch. The basic print API uses DPI 72.

You need to convert your measurements accordingly ...

 double paperWidth = 8 * 72d; double paperHeight = 4 * 72d; double margin = 1 * 72d; 

UPDATED with an example

g2d.setClip(0, 0, (int)pageFormat.getImageableWidth(), (int)pageFormat.getImageableHeight()); not recommended, dangerous and not required at all, in addition, you used the wrong width and height values. The image options take the fields into account, but you haven’t translated the graphic, which most likely cuts out the bottom left of the area you need to print in order to ...

I wouldn’t just use cropping

enter image description here

 public class TestPrinting01 { public static void main(String[] args) { 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, margin, width - (margin * 2), height - (margin * 2)); System.out.println("Before- " + dump(paper)); pf.setOrientation(PageFormat.LANDSCAPE); 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(); } } } 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; // Be careful of clips... g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY()); double width = pageFormat.getImageableWidth(); double height = pageFormat.getImageableHeight(); g2d.drawRect(0, 0, (int)pageFormat.getImageableWidth() - 1, (int)pageFormat.getImageableHeight() - 1); FontMetrics fm = g2d.getFontMetrics(); String text = "top"; g2d.drawString(text, 0, fm.getAscent()); text = "bottom"; double x = width - fm.stringWidth(text); double y = (height - fm.getHeight()) + fm.getAscent(); g2d.drawString(text, (int)x, (int)y); return Printable.PAGE_EXISTS; } } } 

UPDATED

When printing components, you are responsible for its layout.

enter image description here

 public class TestPrinting01 { private static JEditorPane editor; public static void main(String[] args) { try { editor = new JEditorPane(); editor.setPage(new File("C:/hold/search.htm").toURI().toURL()); 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, margin, width - (margin * 2), height - (margin * 2)); System.out.println("Before- " + dump(paper)); pf.setOrientation(PageFormat.LANDSCAPE); 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; // Be careful of clips... // g2d.setClip(0, 0, (int) pageFormat.getWidth(), (int) pageFormat.getHeight()); g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY()); double width = pageFormat.getImageableWidth(); double height = pageFormat.getImageableHeight(); System.out.println("width = " + width); System.out.println("height = " + height); editor.setLocation(0, 0); editor.setSize((int)width, (int)height); editor.printAll(g2d); g2d.setColor(Color.BLACK); g2d.draw(new Rectangle2D.Double(0, 0, width, height)); return Printable.PAGE_EXISTS; } } } 
+8
source

All Articles