2016-07-26 82 views
0

我正在打印机上打印PNG图像。图像打印在页面的中心,并且不会填满整个页面。我试图增加图像的大小,但它总是在页面的中心。任何想法如何使其适合页面?打印图像时适合页面

psStream = new URL(url).openStream(); 
       if (psStream == null) { 
        return "Unable to fetch image"; 
       } 
       DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG; 
       Doc myDoc = new SimpleDoc(psStream, flavor, null); 
       PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
       PrintServiceAttributeSet attributes = new HashPrintServiceAttributeSet(); 
       attributes.add(new PrinterName(printData.printer, Locale.getDefault())); 
       final PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, attributes); 
       if (printServices.length == 0) { 
        return "Could not find printer " + printData.printer; 
       } else { 
        myPrinter = printServices[0]; 
        DocPrintJob job = myPrinter.createPrintJob(); 

        try { 
         job.print(myDoc, aset); 
         return null; 
        } catch (Exception e) { 
         e.printStackTrace(); 
         return "Could not print : " + e.getMessage(); 
        } 

       } 
+0

重复http://stackoverflow.com/questions/27029166/java-printerjob-not-printing-to-fit-paper – SomeDude

回答

0

实质上,您必须调整图像大小以适合页面尺寸。另外,打印机驱动程序默认定义页面边框区域。因此,要使用整个页面,您必须删除这些边界。但是,有时候不可能删除边框,因为它是由打印机驱动程序控制的,所以您可以控制您所控制的驱动程序。

public void print(String imageFileName) throws IOException, PrinterException { 
    final PrintService printService = PrintServiceLookup.lookupDefaultPrintService(); 
    final BufferedImage image = ImageIO.read(new File(imageFileName)); 
    final PrinterJob printJob = PrinterJob.getPrinterJob(); 
    printJob.setJobName("MyApp: " + imageFileName); 
    printJob.setPrintService(printService); 
    printJob.setPrintable(new Printable() { 
     @Override 
     public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { 
      if (pageIndex == 0) { 
       final Paper paper = pageFormat.getPaper(); 
       paper.setImageableArea(0.0, 0.0, pageFormat.getPaper().getWidth(), pageFormat.getPaper().getHeight()); 
       pageFormat.setPaper(paper); 
       graphics.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY()); 
       graphics.drawImage(image, 0, 0, (int) pageFormat.getPaper().getWidth(), (int) pageFormat.getPaper().getHeight(), null); 
       return PAGE_EXISTS; 
      } else { 
       return NO_SUCH_PAGE; 
      } 
     } 
    }); 
    printJob.print(); 
} 

我想你不想打印拉伸的图像,所以你必须适当地缩放图像。还有另一篇文章似乎是相同的,并展示如何缩放图像:Fit image into the printing area

0

下面是我用来打印Java Swing JPanel的一些代码。您可以修改它以从png打印BufferedImage。

我保持图像的比例,所以它只能水平或垂直填充页面,但不能同时填充页面。

package com.ggl.sudoku.solver.controller; 

import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.awt.print.PageFormat; 
import java.awt.print.Printable; 
import java.awt.print.PrinterException; 
import java.awt.print.PrinterJob; 

import javax.swing.JPanel; 

import com.ggl.sudoku.solver.view.SudokuFrame; 

public class PrintActionListener implements Runnable { 

    private SudokuFrame frame; 

    public PrintActionListener(SudokuFrame frame) { 
     this.frame = frame; 
    } 

    @Override 
    public void run() { 
     final BufferedImage image = createPanelImage(); 

     PrinterJob printJob = PrinterJob.getPrinterJob(); 
     printJob.setPrintable(new ImagePrintable(printJob, image)); 

     if (printJob.printDialog()) { 
      try { 
       printJob.print(); 
      } catch (PrinterException prt) { 
       prt.printStackTrace(); 
      } 
     } 
    } 

    private BufferedImage createPanelImage() { 
     JPanel panel = frame.getSudokuPanel(); 
     BufferedImage image = new BufferedImage(panel.getWidth(), 
       panel.getHeight(), BufferedImage.TYPE_INT_RGB); 
     Graphics2D g = image.createGraphics(); 
     panel.paint(g); 
     g.dispose(); 
     return image; 
    } 

    public class ImagePrintable implements Printable { 

     private double   x, y, width; 

     private int    orientation; 

     private BufferedImage image; 

     public ImagePrintable(PrinterJob printJob, BufferedImage image) { 
      PageFormat pageFormat = printJob.defaultPage(); 
      this.x = pageFormat.getImageableX(); 
      this.y = pageFormat.getImageableY(); 
      this.width = pageFormat.getImageableWidth(); 
      this.orientation = pageFormat.getOrientation(); 
      this.image = image; 
     } 

     @Override 
     public int print(Graphics g, PageFormat pageFormat, int pageIndex) 
       throws PrinterException { 
      if (pageIndex == 0) { 
       int pWidth = 0; 
       int pHeight = 0; 
       if (orientation == PageFormat.PORTRAIT) { 
        pWidth = (int) Math.min(width, (double) image.getWidth()); 
        pHeight = pWidth * image.getHeight()/image.getWidth(); 
       } else { 
        pHeight = (int) Math.min(width, (double) image.getHeight()); 
        pWidth = pHeight * image.getWidth()/image.getHeight(); 
       } 
       g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null); 
       return PAGE_EXISTS; 
      } else { 
       return NO_SUCH_PAGE; 
      } 
     } 

    } 

}