设置要在Java代码中打印的页边距

设置要在Java代码中打印的页边距,java,swing,Java,Swing,我正在尝试打印一个JPanel…我有工作代码。但问题是,它正在从四个侧面打印JPanel和broad magin。。。请有人查一下我的密码。并建议我,我如何才能改变我的代码,将适合a4大小的纸张与最低限度的利润。这是激活操作的代码 btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { PrinterJob pjob = Print

我正在尝试打印一个JPanel…我有工作代码。但问题是,它正在从四个侧面打印JPanel和broad magin。。。请有人查一下我的密码。并建议我,我如何才能改变我的代码,将适合a4大小的纸张与最低限度的利润。这是激活操作的代码

btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        PrinterJob pjob = PrinterJob.getPrinterJob();
        PageFormat preformat = pjob.defaultPage();
        preformat.setOrientation(PageFormat.PORTRAIT);
        PageFormat postformat = pjob.pageDialog(preformat);

        if (preformat != postformat) {
            RepairBill r=new RepairBill();
            r.setVisible(false);
            pjob.setPrintable(new Printer(r.r), postformat); //r.r is the object for JPanel
            if (pjob.printDialog()) {
                try {
                    pjob.print();
                } catch (PrinterException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }
        }
    });
打印机类代码

public static class Printer implements Printable {
    final Component comp;

    public Printer(Component comp) {
        this.comp = comp;
    }

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

        // get the bounds of the component
        Dimension dim = comp.getSize();
        double cHeight = dim.getHeight();
        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);
        g2.scale(xRatio, yRatio);
        comp.paint(g2);
        return Printable.PAGE_EXISTS;
    }
}

我添加了边距:水平边距为1英寸,垂直边距为0.5英寸

检查页边距是否仍可打印(1 mm的页边距不可行)

我没有提及页面大小,因为这是一个物理设备问题,例如,无论某人有A4还是美国字母。人们可以在PrintJob中设置它,查看API和网络中的javadoc

现在按比例进行缩放

    // get the bounds of the component
    Dimension dim = comp.getSize();
    double cHeight = dim.getHeight();
    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 paperHeight = format.getHeight();
    double paperWidth = format.getWidth();

    final int INCH = 72;
    double vertMargin = 0.5 * INCH;
    double horMargin = 1 * INCH;

    // Check whether margins are enough, are printable.
    if (paperWidth - 2 * horMargin < pWidth) {
        pWidth = paperWidth - 2 * horMargin;
        pXStart = horMargin
    }
    if (paperHeight - 2 * vertMargin < pHeight) {
        pHeight = paperHeight - 2 * vertMargin;
        pYStart = vertMargin;
    }

    // Determine scaling
    double xRatio = pWidth / cWidth;
    double yRatio = pHeight / cHeight;
    double ratio = Math.min(xRatio, yRation); // Proportional

    Graphics2D g2 = (Graphics2D) g;
    g2.translate(pXStart, pYStart);
    g2.scale(ratio, ratio);
    comp.paint(g2);
//获取组件的边界
尺寸标注=组件getSize();
double cHeight=dim.getHeight();
double cWidth=dim.getWidth();
//获取可打印区域的边界
double pHeight=format.getImageableHeight();
double pWidth=format.getImageableWidth();
double pXStart=format.getImageableX();
double pYStart=format.getImageTabley();
double paperHeight=format.getHeight();
double paperWidth=format.getWidth();
最终英寸=72;
双垂直边缘=0.5*英寸;
双水平边距=1*英寸;
//检查页边距是否足够,是否可打印。
if(纸张宽度-2*水平边距<宽度){
pWidth=纸张宽度-2*水平边距;
pXStart=horMargin
}
if(纸张高度-2*vertMargin
代码中心(
translate
)和
scale
s(不成比例!)。由于单位为1/72nds英寸,可以设置绝对边距,但它至少应为getImageableX/Y,并通过Math.min(X比率,Y比率)在两个方向上进行缩放。抱歉,我对Java打印没有足够的了解。你能告诉我,我应该在这个代码里修改什么吗?我把它编出来了。正如你所看到的,没有大的区别。对不起,先生。但是我复制了上面的代码。。但它并没有给我适当的输出,你可以给出
g2.scale(xRatio,yRatio)一次尝试,可能组件垂直大于一个页面。对不起,没有成功。先生,我刚刚编辑了我的帖子。请检查我的输出的屏幕截图。这与我复制了您的代码并更改了g2.scale(xRatio,yRatio)后得到的输出相同;我不能做更多。尝试使用
Graphics.drawLine、drawRect、drawString重新开始绘制网格线。然后看看如何达到你的目标。