如何打印自定义纸张尺寸(检查8“x 4”)? [英] How can I print a custom paper size (cheques 8" x 4")?

查看:429
本文介绍了如何打印自定义纸张尺寸(检查8“x 4”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Java小程序打印自定义纸张大小。我已经设置了纸张大小,但是它被忽略了。



我也尝试使用book方法,因为我已经看到了有关这方面的一些信息,但是当我使用它只是打印一个空白页,似乎仍然是A4(我打算检查显然小得多(8×4))。



我试图从JEditorPane打印HTML,如果这有什么区别的话。



如果您有任何想法,我会非常感激,我正在撕掉我的头发与此同时。



我还应该补充一点,就Java而言,我是一个初学者。



这是我到目前为止:

更新:
现在我已经获得了页面大小,但似乎无法以获得我正在加载的HTML页面以适合页面大小。



更新:
现在我只是无法让applet在浏览器中运行。它从eclipse起作用,而不是浏览器。我还需要从参数中传递URL。



以下是我正在使用和更新的Java代码的HTML applet标记:

 <!DOCTYPE html> 
< html>
< head>< title>打印检查< / title>< / head>
< body>
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 {
$ b $ / **
*
* /
private static final long serialVersionUID = 8065834484717197790L;
私有静态JEditorPane编辑器;
$ b $ 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;
双倍高度= 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);

尝试{
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( ×)。
append(paper.getImageableY())。append( - ).append(paper
.getImageableWidth())。append(x)。append(paper.getImageableHeight());
return sb.toString();
}

受保护的静态字符串转储(PageFormat pf){
Paper paper = pf.getPaper();
返回转储(纸);


public static class Page implements Printable {
$ b $ public int print(Graphics graphics,PageFormat pageFormat,int pageIndex){
if(pageIndex> ; = 1)返回Printable.NO_SUCH_PAGE;

Graphics2D g2d =(Graphics2D)图形;
g2d.translate((int)pageFormat.getImageableX(),(int)pageFormat.getImageableY());

editor.setSize((int)pageFormat.getImageableWidth(),(int)pageFormat.getImageableHeight());
editor.print(g2d);

返回Printable.PAGE_EXISTS;
}
}
}

预先感谢您的帮助您可以提供。

解决方案

打印旨在以每英寸像素为单位工作。基础打印API使用DPI为72.



您需要相应地转换您的测量结果...

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

使用示例进行更新

g2d.setClip(0,0,(int)pageFormat.getImageableWidth(),(int)pageFormat.getImageableHeight()); 一般来说,不需要,除此之外,你已经使用了错误的宽度和高度值。可成像的参数考虑了边距,但是你还没有翻译图像,这将更有可能减少你必须打印的区域的底部左边部分...



我只是避免使用剪辑



$ b

  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;
双倍高度= 4d * 72d;
double margin = 1d * 72d;
paper.setSize(width,height);
paper.setImageableArea(
保证金,
保证金,
宽度 - (保证金* 2),
高度 - (保证金* 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);

尝试{
pj.print();
} catch(PrinterException ex){
ex.printStackTrace();




保护静态字符串转储(Paper paper){
StringBuilder sb = new StringBuilder(64);
sb.append(paper.getWidth())。append(x)。append(paper.getHeight())
.append(/)。append(paper.getImageableX()) .append( ×)。
append(paper.getImageableY())。append( - ).append(paper
.getImageableWidth())。append(x)。append(paper.getImageableHeight());
return sb.toString();
}

受保护的静态字符串转储(PageFormat pf){
Paper paper = pf.getPaper();
返回转储(纸);


public static class Page implements Printable {
$ b $ public int print(Graphics graphics,PageFormat pageFormat,int pageIndex){
if(pageIndex> ; = 1){
return Printable.NO_SUCH_PAGE;
}

Graphics2D g2d =(Graphics2D)图形;
//小心剪辑...
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);

返回Printable.PAGE_EXISTS;
}
}
}

UPDATED




  public class TestPrinting01 {

私有静态JEditorPane编辑器;

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;
双倍高度= 4d * 72d;
double margin = 1d * 72d;
paper.setSize(width,height);
paper.setImageableArea(
保证金,
保证金,
宽度 - (保证金* 2),
高度 - (保证金* 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);

尝试{
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( ×)。
append(paper.getImageableY())。append( - ).append(paper
.getImageableWidth())。append(x)。append(paper.getImageableHeight());
return sb.toString();
}

受保护的静态字符串转储(PageFormat pf){
Paper paper = pf.getPaper();
返回转储(纸);


public static class Page implements Printable {
$ b $ public int print(Graphics graphics,PageFormat pageFormat,int pageIndex){
if(pageIndex> ; = 1){
return Printable.NO_SUCH_PAGE;
}

Graphics2D g2d =(Graphics2D)图形;
//小心剪辑...
// 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));

返回Printable.PAGE_EXISTS;
}
}
}


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.

解决方案

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

You need to convert your measurements accordingly...

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

UPDATED with example

g2d.setClip(0, 0, (int)pageFormat.getImageableWidth(), (int)pageFormat.getImageableHeight()); is ill adviced, dangerous and generally, not required, besides which, you've used the wrong width and height values. The imageable parameters take into account the margins, but you've not translated the graphics, which will more then likely cut of the bottom, left portion of the area you do have to print to...

I'd just avoid using clipping

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 become responsible for it's layout.

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“x 4”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆