java PrinterJob不打印以适合纸张 [英] java PrinterJob not printing to fit paper

查看:147
本文介绍了java PrinterJob不打印以适合纸张的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用默认打印机打印jpeg文件时卡住了。在我的程序中,当我从文件夹中选择图像时,我需要使用打印机默认设置(纸张尺寸,边距,方向)进行打印。

i am stuck currently when printing a jpeg file with the default printer. In my program when i select an image from a folder, i need to print it using the printer default settings (paper size, margins, orientation).

目前我得到了这个:

PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
final BufferedImage image = ImageIO.read(new File("car.jpg"));

PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintService(printService);
printJob.setPrintable(new Printable(){
  @Override
  public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException{
      if (pageIndex == 0) {
          graphics.drawImage(image, 0, 0, (int)pageFormat.getWidth(), (int)pageFormat.getHeight(), null);
          return PAGE_EXISTS;
      else return NO_SUCH_PAGE;
  }
}

printJob.print();

我的打印机现在大小的默认设置是:10 x 15厘米(4 x 6英寸)
但是当我设置程序打印给定图像时,它只显示一小部分纸。

The default settings for my printer right now for size is: 10 x 15 cm (4 x 6 in) but when i set my program to print the given image, it displays only a small section of the paper.

请帮帮我。

编辑

感谢大家的帮助,我设法找到了另一位用户在无边框上发布的答案打印

thanks everyone for their help, i managed to find the answer posted by another user at Borderless printing

推荐答案

确保你首先,翻译 Graphics 上下文以适应可想象区域...

Make sure that you are, first, translating the Graphics context to fit within inthe imagable area...

g2d.translate((int) pageFormat.getImageableX(),
              (int) pageFormat.getImageableY());

接下来,请确保您使用的是 imageableWidth imageableHeight PageFormat

Next, make sure you are using the imageableWidth and imageableHeight of the PageFormat

double width = pageFormat.getImageableWidth();
double height = pageFormat.getImageableHeight();

而不是宽度 / height 属性。其中许多内容都是从不同的上下文中翻译出来的......

and not the width/height properties. Many of these things get translated from different contexts...

graphics.drawImage(image, 0, 0, (int)width, (int)height, null);

getImageableWidth / Height 返回页面大小在页面方向的上下文中

The getImageableWidth/Height returns the page size within the context of the page orientation

打印几乎假设dpi为72(不要强调,打印API可以处理更高的分辨率,但核心API假设72dpi)

Printing pretty much assumes a dpi of 72 (don't stress, the printing API can handle much higher resolutions, but the core API assumes 72dpi)

这意味着10x15cm的页面应转换为 283.46456664x425.19684996 像素。您可以使用 System.out.println 验证此信息,并将 getImageableWidth / Height 的结果转储到控制台。

This means that a page of 10x15cm should translate to 283.46456664x425.19684996 pixels. You can verify this information by using a System.out.println and dumping the results of getImageableWidth/Height to the console.

如果你得到不同的设置,Java可能会覆盖默认的页面属性

If you're getting different settings, it's possible that Java has overridden the default page properties

For示例...

  • Fit image into the printing area
  • Setting print size of a jLabel and put a jRadiobutton on the print

你有两个选择......

You have two choices...

显示 PrintDialog 并确保选择了正确的页面设置

Show the PrintDialog and ensure that the correct page settings are selected

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));
aset.add(new MediaPrintableArea(0, 0, 150, 100, MediaPrintableArea.MM));

PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(new PrintTask()); // You Printable here

if (pj.printDialog(aset)) {
    try {
        pj.print(aset);
    } catch (PrinterException ex) {
        ex.printStackTrace();
    }
}



或者你可以......



只需手动手动设置纸张/页面值...

Or you could...

Just manually set the paper/page values manually...

public static void main(String[] args) {
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageFormat pf = pj.defaultPage();
    Paper paper = pf.getPaper();
    // 10x15mm
    double width = cmsToPixel(10, 72);
    double height = cmsToPixel(15, 72);
    paper.setSize(width, height);
    // 10 mm border...
    paper.setImageableArea(
                    cmsToPixel(0.1, 72),
                    cmsToPixel(0.1, 72),
                    width - cmsToPixel(0.1, 72),
                    height - cmsToPixel(0.1, 72));
    // Orientation
    pf.setOrientation(PageFormat.PORTRAIT);
    pf.setPaper(paper);
    PageFormat validatePage = pj.validatePage(pf);
    pj.setPrintable(new Printable() {
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            // Your code here
            return NO_SUCH_PAGE;
        }

    },  validatePage);
    try {
        pj.print();
    } catch (PrinterException ex) {
        ex.printStackTrace();
    }
}

// The number of CMs per Inch
public static final double CM_PER_INCH = 0.393700787d;
// The number of Inches per CMs
public static final double INCH_PER_CM = 2.545d;
// The number of Inches per mm's
public static final double INCH_PER_MM = 25.45d;

/**
 * Converts the given pixels to cm's based on the supplied DPI
 *
 * @param pixels
 * @param dpi
 * @return
 */
public static double pixelsToCms(double pixels, double dpi) {
    return inchesToCms(pixels / dpi);
}

/**
 * Converts the given cm's to pixels based on the supplied DPI
 *
 * @param cms
 * @param dpi
 * @return
 */
public static double cmsToPixel(double cms, double dpi) {
    return cmToInches(cms) * dpi;
}

/**
 * Converts the given cm's to inches
 *
 * @param cms
 * @return
 */
public static double cmToInches(double cms) {
    return cms * CM_PER_INCH;
}

/**
 * Converts the given inches to cm's
 *
 * @param inch
 * @return
 */
public static double inchesToCms(double inch) {
    return inch * INCH_PER_CM;
}

这篇关于java PrinterJob不打印以适合纸张的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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