java.awt.print 如何将默认页面尺寸设置为A6

发布于 2025-01-09 15:18:58 字数 3031 浏览 1 评论 0原文

我有一个带有静态方法的 java 类,用于打印实现自定义接口(称为 MyPrintablePanel)的 JPanel。

问题是,当出现打印对话框(页面设置对话框)时,默认纸张尺寸为“Letter”。我希望默认纸张尺寸为A6,这样我就不必每次打印时手动选择A6。

代码如下:

package designer.office.utils;

import designer.office.printables.MyPrintablePanel;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.print.attribute.Size2DSyntax;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import javax.swing.JPanel;

/**
 * A class to print a JPanel
 */
public class MyPrinter implements Printable {
    
    final MyPrintablePanel comp;

    public MyPrinter(MyPrintablePanel comp){
        this.comp = comp;
    }
    
    public static void printJPanel(MyPrintablePanel jPanel){

        PrinterJob pjob = PrinterJob.getPrinterJob();
        MediaSize isoA6Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A6);
        float[] size = isoA6Size.getSize(Size2DSyntax.INCH);
        Paper paper = new Paper();
        paper.setSize(size[0] * 72.0, size[1] * 72.0);
        paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
        PageFormat preformat = new PageFormat();
        preformat.setPaper(paper);
        
        PageFormat postformat = pjob.pageDialog(preformat);
        //If user does not hit cancel then print.
        if (preformat != postformat) {
            //Set print component
            pjob.setPrintable(new MyPrinter(jPanel), postformat);
            if (pjob.printDialog()) {
                try {
                    pjob.print();
                } catch (PrinterException ex) {
                    System.out.println("Failed to print: " + ex.getMessage());
                }
            }
        }
    }

    @Override
    public int print(Graphics g, PageFormat format, int page_index) 
            throws PrinterException {
        if (page_index > this.comp.getNumOfPages() - 1) {
            return Printable.NO_SUCH_PAGE;
        }

        // get the bounds of the component
        Dimension dim = comp.getSize();
        double cHeight = dim.getHeight()/this.comp.getNumOfPages();
        double cWidth = dim.getWidth();

        // get the bounds of the printable area
        double pHeight = format.getImageableHeight();
        double pWidth = format.getImageableWidth();

        double pXStart = format.getImageableX();
        double pYStart = format.getImageableY();

        double xRatio = pWidth / cWidth;
        double yRatio = pHeight / cHeight;


        Graphics2D g2 = (Graphics2D) g;
        g2.translate(pXStart, pYStart - page_index * pHeight);
        g2.scale(xRatio, yRatio);
        comp.paint(g2);

        return Printable.PAGE_EXISTS;
    }
}

我正在使用 HP Laser Jet Professional P1102 打印机进行打印(或者更确切地说,我的客户正在打印),其纸盒中装有 A6 纸。

I have a java class with a static method that prints a JPanel which implements a custom interface (called MyPrintablePanel).

The problem is, when the print dialog appears (Page setup dialog box), the default paper size is "Letter". I want the default paper size to be A6 so that I don't have to manually choose A6 each time I print.

Here is the code:

package designer.office.utils;

import designer.office.printables.MyPrintablePanel;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.print.attribute.Size2DSyntax;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import javax.swing.JPanel;

/**
 * A class to print a JPanel
 */
public class MyPrinter implements Printable {
    
    final MyPrintablePanel comp;

    public MyPrinter(MyPrintablePanel comp){
        this.comp = comp;
    }
    
    public static void printJPanel(MyPrintablePanel jPanel){

        PrinterJob pjob = PrinterJob.getPrinterJob();
        MediaSize isoA6Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A6);
        float[] size = isoA6Size.getSize(Size2DSyntax.INCH);
        Paper paper = new Paper();
        paper.setSize(size[0] * 72.0, size[1] * 72.0);
        paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
        PageFormat preformat = new PageFormat();
        preformat.setPaper(paper);
        
        PageFormat postformat = pjob.pageDialog(preformat);
        //If user does not hit cancel then print.
        if (preformat != postformat) {
            //Set print component
            pjob.setPrintable(new MyPrinter(jPanel), postformat);
            if (pjob.printDialog()) {
                try {
                    pjob.print();
                } catch (PrinterException ex) {
                    System.out.println("Failed to print: " + ex.getMessage());
                }
            }
        }
    }

    @Override
    public int print(Graphics g, PageFormat format, int page_index) 
            throws PrinterException {
        if (page_index > this.comp.getNumOfPages() - 1) {
            return Printable.NO_SUCH_PAGE;
        }

        // get the bounds of the component
        Dimension dim = comp.getSize();
        double cHeight = dim.getHeight()/this.comp.getNumOfPages();
        double cWidth = dim.getWidth();

        // get the bounds of the printable area
        double pHeight = format.getImageableHeight();
        double pWidth = format.getImageableWidth();

        double pXStart = format.getImageableX();
        double pYStart = format.getImageableY();

        double xRatio = pWidth / cWidth;
        double yRatio = pHeight / cHeight;


        Graphics2D g2 = (Graphics2D) g;
        g2.translate(pXStart, pYStart - page_index * pHeight);
        g2.scale(xRatio, yRatio);
        comp.paint(g2);

        return Printable.PAGE_EXISTS;
    }
}

I'm printing (or rather my client is printing) from a HP Laser Jet Professional P1102 printer with A6 paper in its tray.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文