在netbeans中使用java打印页面时,页面格式化不起作用

44u64gxh  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(338)

我正在尝试打印自定义大小的页面。但它不起作用。我做了一个可变的宽度和高度,我希望我的页面。
查看我的代码并提出一些更改建议

PrinterJob job = PrinterJob.getPrinterJob();
    job.setJobName("Print Data");

    job.setPrintable(new Printable(){
        public int print(Graphics pg,PageFormat pf, int pageNum){
            //Paper paper = pf.getPaper();
            Paper paper = new Paper();
            double width = 8d * 22d;
            double height = 4d * 22d;
            double margin = 0.2d * 22d;
            paper.setSize(width, height);
            paper.setImageableArea(
                    margin,
                    margin,
                    width - (margin * 2),
                    height - (margin * 2));
            job.setPrintable(this, pf);

            if(pageNum > 0){
                return Printable.NO_SUCH_PAGE;
            }

            Graphics2D g2 = (Graphics2D)pg;

            g2.translate(pf.getImageableX(), pf.getImageableY());

            System.out.println("width1 = " + pf.getWidth());
            System.out.println("height1 = " + pf.getHeight());

            g2.setColor(Color.BLACK);
            g2.draw(new Rectangle2D.Double(0, 0, pf.getWidth(), pf.getHeight()));

            jPanel3.printAll(g2);

            return Printable.PAGE_EXISTS; 
        }
    });

    System.out.println("1");
    boolean ok = job.printDialog();

    if(ok){
        try{
            System.out.println("2");
            job.print();
            System.out.println("3");
        }
        catch (PrinterException ex){
            ex.printStackTrace();
        }
    }

我被这个问题困住了。不管我穿什么尺码。我得到iso a4尺寸的印刷品。我想他们的密码有问题。

shstlldc

shstlldc1#

有很多可能的问题,但我要解决一个大问题
这个。。。

job.setPrintable(new Printable(){
    public int print(Graphics pg,PageFormat pf, int pageNum){
        //Paper paper = pf.getPaper();
        Paper paper = new Paper();
        double width = 8d * 22d;
        double height = 4d * 22d;
        double margin = 0.2d * 22d;
        paper.setSize(width, height);
        paper.setImageableArea(
                margin,
                margin,
                width - (margin * 2),
                height - (margin * 2));
        job.setPrintable(this, pf);

是个可怕的主意。就像在绘制组件时,不修改状态一样,可能会产生意想不到的后果。
相反,在这种情况下,您应该建立 Paper 提前调整大小。然后应将其应用于 PageFormat 传给了 PrintJob 通过 setPrintable(Printable, PageFormat) 方法。
也就是说,它看起来更像。。。

PrinterJob job = PrinterJob.getPrinterJob();
job.setJobName("Print Data");
//Paper paper = pf.getPaper();
PageFormat pageFormat = job.defaultPage();
Paper paper = new Paper();
double width = 8d * 22d;
double height = 4d * 22d;
double margin = 0.2d * 22d;
paper.setSize(width, height);
paper.setImageableArea(
        margin,
        margin,
        width - (margin * 2),
        height - (margin * 2));

pageFormat.setPaper(paper);

job.setPrintable(new Printable() {
    @Override
    public int print(Graphics pg, PageFormat pf, int pageNum) {
        if (pageNum > 0) {
            return Printable.NO_SUCH_PAGE;
        }
        Graphics2D g2 = (Graphics2D) pg;

        g2.translate(pf.getImageableX(), pf.getImageableY());

        System.out.println("width1 = " + pf.getWidth());
        System.out.println("height1 = " + pf.getHeight());

        g2.setColor(Color.BLACK);
        // Consider using pf.getImageableWidth() and pf.getImageableHeight() instead
        g2.draw(new Rectangle2D.Double(0, 0, pf.getWidth(), pf.getHeight()));

        // Maybe consider drawing your border first, as `printAll` 
        // might fill in the whole page
        jPanel3.printAll(g2);

        return Printable.PAGE_EXISTS;
    }
}, pageFormat);

System.out.println("1");
boolean ok = job.printDialog();

if (ok) {
    try {
        System.out.println("2");
        job.print();
        System.out.println("3");
    } catch (PrinterException ex) {
        ex.printStackTrace();
    }
}

我说可能,因为有一堆验证和其他系统垃圾,可能最终还是不是你想要的。
我建议你看看。。。
java打印到标签打印机
更改jtextpane上的打印边距
将printerjob对象适配到BuffereImage的特定打印格式
如何在300 dpi打印机上用java设计图像
java printerjob打印不适合纸张
如何打印自定义纸张尺寸(支票8“x 4”)?
使用java在4 x 6纸张上打印1800 x 1200图像
作为一些基本的例子,所有人都在做类似的事情——改变纸张尺寸:d

相关问题