【问题标题】:java PrinterJob not printing to fit paperjava PrinterJob 不打印以适合纸张
【发布时间】:2014-11-19 23:38:57
【问题描述】:

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

目前我得到了这个:

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 英寸) 但是当我将程序设置为打印给定的图像时,它只显示纸张的一小部分。

请帮帮我。

编辑

感谢大家的帮助,我设法找到了另一个用户在Borderless printing发布的答案

【问题讨论】:

    标签: java printing


    【解决方案1】:

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

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

    接下来,确保您使用的是PageFormatimageableWidthimageableHeight

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

    而不是 width/height 属性。其中许多东西都是从不同的上下文中翻译过来的……

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

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

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

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

    如果您获得不同的设置,则可能是 Java 覆盖了默认页面属性

    例如...

    你有两个选择...

    你可以...

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

    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();
        }
    }
    

    或者你可以...

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

    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;
    }
    

    【讨论】:

    • 嗨,我尝试了你所说的,它以同样的方式打印图像,它只是将图像缩小到纸张尺寸的一半(10x15cm),我显示了 getImageableWidth/height 的结果,它给出了 0.5
    • 那么很有可能Java正在改变PageFormat
    • 那我该怎么办?我需要该程序能够使图像适合打印机上定义的纸张尺寸。
    • 感谢您的帮助,我尝试了使用 PrintRequestAttributeSet 的第一种方法,但没有成功,它在纸上打印了一个小图像,关于第二种方法,我不能使用那个方法,因为正如我所说,该程序应该能够适应用户在打印机上定义的纸张尺寸给定的图像。
    • 那么第一种方法应该可以工作,因为您正在指定打印机应该使用的值...
    【解决方案2】:

    看起来您正在使用基于 PageFormat 的尺寸打印图像,而不是实际图像的尺寸,您的 drawImage() 方法应该看起来像这样

    graphics.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null)
    

    【讨论】:

    • 我试过了,结果是一样的 :(,图像只覆盖纸张大小的一半...我需要图像以适应纸张尺寸。图像为 1200x800,纸张为 ( 10x15cm)
    • 啊,好吧,我的解决方案会以全尺寸打印图像,如果你想按比例缩小它看起来上面的答案应该可以工作,或者你必须决定一些规模将高度和宽度除以的因子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2012-04-16
    相关资源
    最近更新 更多